Locking WebDAV Items

Locking prevents the item from being modified by other users.

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 IHierarchyItem.SupportedLock property.

string license ="<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IHierarchyItem item = session.OpenItem("http://server:8580/LargeFile.msi");
 
if(item.SupportedLock.Length==0)
    Console.WriteLine("Locks are not supported.");
foreach(LockScope lockScope in item.SupportedLock)
{
    if(lockScope == LockScope.Exclusive)
        Console.WriteLine("Item supports exclusive locks.");
    if(lockScope == LockScope.Shared)
        Console.WriteLine("Item supports shared locks.");
}

Locking Items

While locking you can pass to the 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 the lock. 
  • Time when the lock expires. To specify that lock should never expire pass TimeSpan.MaxValue.
IHierarchyItem item = session.OpenFile(new Uri("http://server:8580/Sales.txt"));
LockInfo lockInfo = item.Lock(LockScope.Shared, false, "User 1", new TimeSpan(0, 20, 0)); 

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 the 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:

IHierarchyItem item = session.OpenFile(new Uri("http://server:8580/Sales.txt"));
IFolder destFolder = session.OpenFolder(new Uri("http://server:8580/Info/"));
 
LockInfo lockInfo = item.Lock(LockScope.Shared, false, "User 1", new TimeSpan(0, 20, 0));
item.CopyTo(destFolder, "Copy of " + item.DisplayName, false, false);
item.Unlock(lockInfo.LockToken.LockToken);
 

Using the API you can also request a list of lock tokens applied to the item:

IHierarchyItem item = session.OpenFolder(new Uri("http://server:8580/Products/"));
foreach (LockInfo lockInfo in item.ActiveLocks)
{
    string timout = lockInfo.TimeOut == TimeSpan.MaxValue ? "Infinite" : lockInfo.TimeOut.TotalSeconds.ToString();
    Console.WriteLine(lockInfo.Owner
        + " " + lockInfo.LockToken.Href
        + " " + lockInfo.LockToken.LockToken
        + " " + lockInfo.LockScope
        + " " + lockInfo.Deep
        + " " + timout);
}