Getting Started with IT Hit WebDAV AJAX Library
Download the ITHitWebDAVClient.js here and add <script> tag referencing ITHitWebDAVClient.js to your web page:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>IT Hit WebDAV AJAX Library</title>
<script type="text/javascript" src="ITHitWebDAVClient.js" ></script>
</head>
<body>
...
</body>
</html>
The ITHitWebDAVClient.js is fully functional and does not have any limitations. However the trial period is limited to 1 month. After the trial period expires the API will stop working.
Cross-domain and Cross-port Requests
Cross-domain and cross-port requests are supported only in Internet Explorer. If your code is located on a page loaded from domain http://www. domain1.com it will not be able to access WebDAV server located at http://www. domain2.com. In IE however you can perform cross-domain requests but the browser will pop “This page is accessing information that is not under its control. This possesses a security risk. Do you want to continue?” message each time the remote server is called. Cross-port requests in IE does not pop any warning message.
Logging
The library provides logging functionality that logs most of the information transferred between client and server such as WebDAV XML and HTTP headers. To setup logging call ITHit.Logger.AddListener method passing function reference that will receive logging messages:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>IT Hit WebDAV AJAX Library</title>
<script type="text/javascript" src="ITHitWebDAVClient.js" ></script>
<script type="text/javascript">
function logInfo(message)
{
document.getElementById("log").value += message;
}
ITHit.Logger.AddListener(logInfo, ITHit.LogLevel.Debug);
function run()
{
var session = new ITHit.WebDAV.Client.WebDavSession();
var item = session.OpenFolder("http://server:8080/");
}
</script>
</head>
<body onload="run()">
<textarea id="log" style="width: 600px; height: 400px" wrap=off></textarea>
</body>
</html>
The second parameter passed to AddListener method specifies the amount of information logged. During the development you will usually set log level to All or Debug, while deploying you can set it to Error or Fatal.
If any exception occurs it is being logged and if you experience any problems the first thing to do is to examine the log.
Getting a List of Child Items
<html>
<head>
<title>IT Hit WebDAV Client API for AJAX</title>
<script type="text/javascript" src="ITHitWebDAVClient.js" ></script>
<script type="text/javascript">
function run()
{
var session = new ITHit.WebDAV.Client.WebDavSession();
var folder;
try
{
folder = session.OpenFolder("http://server:8080/folder");
}
catch(e)
{
if(e instanceof ITHit.WebDAV.Client.Exceptions.NotFoundException)
alert("Folder not found.");
else
throw e;
}
if(folder!=null)
{
var items = folder.GetChildren(false);
for(var i=0; i<items.length; i++)
{
document.getElementById("container").innerHTML += items[i].DisplayName + "<br/>";
}
}
}
</script>
</head>
<body onload="run()">
<div id="container"></div>
</body>
</html>
If boolean parameter passed to WebDavSession.GetChildren is set to false only children of this folder are requested. Otherwise the entire subtree is requested.
Creating a Folder
var session = new ITHit.WebDAV.Client.WebDavSession();
var folder = session.OpenFolder("http://server:8765/");
var newFolder;
try
{
newFolder = folder.CreateFolder("My Folder");
}
catch(e)
{
if(e instanceof ITHit.WebDAV.Client.Exceptions.MethodNotAllowedException)
alert("Folder already exists.");
else
throw e;
}
if(newFolder!=null)
document.getElementById("container").innerHTML = newFolder.CreationDate;
Creating a File and Uploading Content
var session = new ITHit.WebDAV.Client.WebDavSession();
var folder = session.OpenFolder("http://server/sales/");
var resource = folder.CreateResource("file.txt");
resource.WriteContent('File content.');
Reading File Content
var session = new ITHit.WebDAV.Client.WebDavSession();
var resource = session.OpenResource("http://server/sales/file.txt");
var content = resource.ReadContent();
To download only a part of a file you can specify 2 parameters in ReadContent call. First parameter is the starting byte (zero-based) at witch to start content download, the second – amount of bytes to be downloaded:
var content = resource.ReadContent(5, 200);
The library will add Range header to the request in this case.
Reading Properties
var oNS = ITHit.WebDAV.Client;
var session = new oNS.WebDavSession();
var item = session.OpenResource("http://server/folder/img5.gif");
var properties = item.GetAllProperties();
for(var i=0; i<properties.length; i++)
{
document.getElementById("container").innerHTML +=
properties[i].Name.NamespaceUri + " " +
properties[i].Name.Name + " " +
properties[i].StringValue() + "<br/>";
}
Adding and Updating Custom Properties
var oNS = ITHit.WebDAV.Client;
var session = new oNS.WebDavSession();
var item = session.OpenResource("http://server/image005.gif");
var propsToAddAndUpdate = new Array();
propsToAddAndUpdate[0] = new oNS.Property(new oNS.PropertyName("Ammount", "Sales"), "1200");
propsToAddAndUpdate[1] = new oNS.Property(new oNS.PropertyName("ManagerApproved", "Sales"), "Yes");
propsToAddAndUpdate[2] = new oNS.Property(new oNS.PropertyName("Branch", "Sales"), "EMEA Region");
item.UpdateProperties(propsToAddAndUpdate, null);
The first parameter passed to UpdateProperties is an array of custom properties to be created or updated. The second parameter – array of properties to be deleted.
Deleting Custom Properties
var oNS = ITHit.WebDAV.Client;
var session = new oNS.WebDavSession();
var item = session.OpenResource("http://server/folder/img5.gif");
var propsToDelete = new Array();
propsToDelete[0] = new oNS.PropertyName("Ammount", "Sales");
propsToDelete[1] = new oNS.PropertyName("ManagerApproved", "Sales");
propsToDelete[2] = new oNS.PropertyName("Branch", "Sales");
item.UpdateProperties(null, propsToDelete);
Analyzing Which Properties Failed to Add/Update/Delete
try
{
item.UpdateProperties(propsToAddAndUpdate, null);
}
catch (e)
{
if(e instanceof oNS.Exceptions.PropertyException)
{
document.getElementById("container").innerHTML +=
e.Message + " " + e.Status.Code + " " +
e.Status.Description + "<br/>";
// Find which properties failed to add/update/delete
for(var i=0; i<e.Multistatus.Responses.length; i++)
{
var propInfo = e.Multistatus.Responses[i];
document.getElementById("container").innerHTML +=
propInfo.PropertyName.NamespaceUri + ":" +
propInfo.PropertyName.Name + " " +
propInfo.Status.Code + " " +
propInfo.Status.Description + "<br/>";
}
}
else
throw e; // unexpected error
}
Locking Items
var oNS = ITHit.WebDAV.Client;
var session = new oNS.WebDavSession();
var item = session.OpenResource("http://server/Storage/img5.gif");
var destFolder = session.OpenFolder("http://server/Sales/");
var lockInfo = item.Lock(oNS.LockScope.Shared, false, "User 1", -1);
item.CopyTo(destFolder, "Copy of " + item.DisplayName, false, false);
item.Unlock(lockInfo.LockToken.LockToken);
Getting the List of Locks for the Item
var oNS = ITHit.WebDAV.Client;
var session = new oNS.WebDavSession();
var item = session.OpenResource("http://server/Storage/img5.gif");
var lockInfo = item.Lock(oNS.LockScope.Shared, false, "User 1", -1); // infinite lock
var lockInfo = item.Lock(oNS.LockScope.Shared, false, "User 2", 60); // 1 minute lock
var activeLocks = item.GetActiveLocks();
for(var i=0; i<activeLocks.length; i++)
{
var lockInfo = activeLocks[i];
var timout = lockInfo.TimeOut == -1 ? "Infinite" : lockInfo.TimeOut;
document.getElementById("container").innerHTML += lockInfo.Owner
+ " " + lockInfo.LockToken.Href
+ " " + lockInfo.LockToken.LockToken
+ " " + lockInfo.LockScope
+ " " + lockInfo.Deep
+ " " + timout + "<br/>";
}
Checking if Child Item Exists
var oNS = ITHit.WebDAV.Client;
var session = new oNS.WebDavSession();
var folder = session.OpenFolder("http://server/testfldr/");
var isItemExists = folder.ItemExists("image00.gif");
if(isItemExists)
alert("Item exists");
else
alert("Item not found");
Copying Items
var oNS = ITHit.WebDAV.Client;
var session = new oNS.WebDavSession();
var srcFolder = session.OpenFolder("http://server/Sales/");
var dstFolder = session.OpenFolder("http://server/Products/");
try
{
srcFolder.CopyTo(dstFolder, "Test1", true, false);
}
catch (e)
{
if(e instanceof oNS.Exceptions.PreconditionFailedException)
{
document.getElementById("container").innerHTML +=
"The item with such name exists and 'overwrite' was 'false'.";
}
else if(e instanceof oNS.Exceptions.WebDavHttpException)
{
document.getElementById("container").innerHTML +=
e.Message + " " + e.Status.Code + " " +
e.Status.Description + "<br/>";
// Find which items failed to copy.
for(var i=0; i<e.Multistatus.Responses.length; i++)
{
var resp = e.Multistatus.Responses[i];
document.getElementById("container").innerHTML +=
resp.Href + " " + resp.Status.Code + " " +
resp.Status.Description + "<br/>";
}
}
else
throw e; // unexpected error
}
Moving Items
var oNS = ITHit.WebDAV.Client;
var session = new oNS.WebDavSession();
var srcFolder = session.OpenFolder("http://server/Sales/");
var dstFolder = session.OpenFolder("http://server/Products/");
try
{
srcFolder.MoveTo(dstFolder, srcFolder.DisplayName, false);
}
catch (e)
{
if(e instanceof oNS.Exceptions.PreconditionFailedException)
{
document.getElementById("container").innerHTML +=
"The item with such name exists and 'overwrite' was 'false'.";
}
else if(e instanceof oNS.Exceptions.WebDavHttpException)
{
document.getElementById("container").innerHTML +=
e.Message + " " + e.Status.Code + " " +
e.Status.Description + "<br/>";
// Find which items failed to move.
for(var i=0; i<e.Multistatus.Responses.length; i++)
{
var resp = e.Multistatus.Responses[i];
document.getElementById("container").innerHTML +=
resp.Href + " " + resp.Status.Code + " " +
resp.Status.Description + "<br/>";
}
}
else
throw e; // unexpected error
}
Deleting Items
var oNS = ITHit.WebDAV.Client;
var session = new oNS.WebDavSession();
var item = session.OpenFolder("http://server:8080/Sales/");
try
{
item.Delete();
}
catch (e)
{
if(e instanceof oNS.Exceptions.WebDavHttpException)
{
document.getElementById("container").innerHTML +=
e.Message + " " + e.Status.Code + " " +
e.Status.Description + "<br/>";
// Find which items failed to delete.
for(var i=0; i<e.Multistatus.Responses.length; i++)
{
&nbs |