Managing Version-Controlled Items on a DeltaV Server
Using version-control features you can check-in/check-out files and track file versions. Note that versioning only manages file versions and does not protect item from being updated by other users. To protect items locking must be used.
Probing Version-Control Support
To detect if the item supports version control feature probe the VersionControl bit in Features enumeration:
WebDavSession session = new WebDavSession(license); IFile file = await session.GetFileAsync("http://server:8580/Sales.docx"); bool versionControlSupported = (file.SupportedFeaturesAsync().Result.Features & Features.VersionControl) != 0;
Enabling Version Control
Usually by default all items in the WebDAV repository are not under version control. The item actually behaves like a Class 2 item. To start creating versions, you must call IFile.PutUnderVersionControl passing true as a parameter:
string license = "<?xml version='1.0' encoding='u..."; WebDavSession session = new WebDavSession(license); session.Credentials = new NetworkCredential("User1", "pwd"); IFile file = await session.GetFileAsync("http://server/sales.docx"); await file.PutUnderVersionControlAsync(true);
During the call to PutUnderVersionControl, server will create an initial version. The item will be in a checked-in state. If the item is under version control it always has at last one version in its versions list.
To disable version control call PutUnderVersionControl passing false as a parameter. Server will usually delete all item versions during this call.
Note that IT Hit WebDAV Server Engine has Engine.AutoPutUnderVersionControl property indicating if items must be put under version control explicitly. If this property is set to true the item will be automatically put under version control before the update.
Checking if Item is Being Version-Controlled
To check if the item is under version control use IFile.VersionControlled property:
if (file.VersionControlled) MessageBox.Show("The item is being version controlled."); else MessageBox.Show("The item is not under version control.");
Listing Item Versions
Each file version implements IVersion interface that is inherited from IHierarchyItem and IItemContent interfaces. To get a list of versions call IFile.GetVersions:
IFile file = await session.GetFileAsync("http://server/sales.docx"); IVersion[] versions = await file.GetVersionsAsync(); foreach (IVersion version in versions) { MessageBox.Show("Version Name: " + version.VersionName); MessageBox.Show("Comment: " + await version.GetCommentAsync()); MessageBox.Show("Author: " + await version.GetCreatorDisplayNameAsync()); MessageBox.Show("Created: " + version.CreationDate); }
You can read version content and request properties as you usually do for files, but you cannot update content or properties (the only exceptions are comments and author, see example below).
IVersion version = await session.GetVersionAsync("http://server/sales.docx?version=2"); using (StreamReader sr = new StreamReader(await version.GetReadStreamAsync())) { string versionContents = sr.ReadToEnd(); MessageBox.Show(versionContents); }
Updating Version-Сontrolled Item
To allow modifications on a version-controlled item, it must be first checked-out. If you would like to protect the item from being modified by other users you will also lock the item:
IFile file = await session.GetFileAsync("http://server/sales.docx"); LockInfo lockInfo = await file.LockAsync(LockScope.Exclusive, false, "John Walter", TimeSpan.MaxValue); await file.CheckOutAsync(lockInfo.LockToken.LockToken); await file.UpdatePropertiesAsync( new Property[] { new Property(new PropertyName("amount", "server"), "2") }, null, lockInfo.LockToken.LockToken); IVersion version = await file.CheckInAsync(lockInfo.LockToken.LockToken); await file.UnlockAsync(lockInfo.LockToken.LockToken); await version.SetCommentAndAuthorAsync("Props updated.", "John");
Note that IFile.CheckOut only allows modifications while the new version is created during the call to IFile.CheckIn.
To find out if the item is in a check-out state use IFile.CheckedOut property:
if (file.CheckedOut) MessageBox.Show("The item is checked-out."); else MessageBox.Show("The item is checked-in.");
Uncheck-out
If after making some item modifications you decide to cancel your updates you can call IFile.UnCheckOut. It will restore the pre-checkout state of the item:
IFile file = await session.GetFileAsync("http://server/sales.docx"); LockInfo lockInfo = await file.LockAsync(LockScope.Exclusive, false, "John Walter", TimeSpan.MaxValue); await file.CheckOutAsync(lockInfo.LockToken.LockToken); //Modify file content or properties here. //.... await file.UnCheckOutAsync(lockInfo.LockToken.LockToken); await file.UnlockAsync(lockInfo.LockToken.LockToken);
Reverting Item Content and Properties to a Specific Version
To revert item state to a specific version use IFile.UpdateToVersion:
IFile file = await session.GetFileAsync("http://server/sales.docx"); LockInfo lockInfo = await file.LockAsync(LockScope.Exclusive, false, "John Walter", TimeSpan.MaxValue); IVersion version = await session.OpenVersionAsync(new Uri("http://server/ sales.docx?version=2")); await file.UpdateToVersionAsync(version, lockInfo.LockToken.LockToken); await file.UnlockAsync(lockInfo.LockToken.LockToken);