Locking Items with JavaScript WebDAV Library
Locking prevents item from being modified by other users and supported by any Class 2 WebDAV server.
Probing Locks Support
Item can support exclusive, shared locks or do not support any locks. If you set exclusive lock other users will not be able to set any locks. If you set shared lock other users will be able to set shared lock on the item. To find out what locks are supported by the item use HierarchyItem.SupportedLocks property.
var oNS = ITHit.WebDAV.Client;
var session = new oNS.WebDavSession();
var item = session.OpenItem("https://server:8100/file.exe");
var supportedLock = item.SupportedLocks;
if(supportedLock.length==0)
document.getElementById("container").innerHTML += "Locks are not supported.";
for (var i = 0; i < supportedLock.length; i++)
{
if (supportedLock[i] == oNS.LockScope.Exclusive)
document.getElementById("container").innerHTML += "Item supports exclusive locks. ";
if (supportedLock[i] == oNS.LockScope.Shared)
document.getElementById("container").innerHTML += "<br/> Item supports shared locks.";
}
Locking Items
While locking you can pass to server following additional information about the lock:
- Specify if the lock is shared or exclusive.
- Specify if the lock applied only to this item or to the entire subtree.
- Name of the user that applies lock.
- Time when lock expires. To specify that lock should never expire pass -1.
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);
If the lock was successfully applied the server will return lock token. The lock token is a string (usually GUID) uniquely identifying the lock. You will pass this lock token back to server when unlocking the item. To modify the locked item you must pass the lock token with an update request. All API methods that modify item content or properties provide overloaded instance to pass the lock token:
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 Item Locks
Using the API you can also request a list of lock tokens applied to the item:
var oNS = ITHit.WebDAV.Client;
var session = new oNS.WebDavSession();
var item = session.OpenResource("http://server/General.doc");
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
item.Refresh(); // reopen item from server to read locks
var activeLocks = item.ActiveLocks;
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/>";
}
|