MOSS: Enable versioning from C#
Add a web reference to the Lists.asmx web service. Call it whatever you want, I used “PDLists”. The following code will access a document library or list, and enable versioning. Simple, but effective.
I use this code in a command line application, saves a lot of time from not logging into a site, finding the list or library, opening settings, opening versioning properties, and then enabling.
public static void EnableVersioning(string URL, string ListName)
{
PDLists.Lists EnableVersions = new PDLists.Lists();
EnableVersions.Credentials = CredentialCache.DefaultCredentials;
EnableVersions.Url = URL + “lists.asmx”;
XmlNode _response = null;
XmlNode ndList = EnableVersions.GetList(ListName);
XmlNode ndVersion = ndList.Attributes["Version"];
XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndProperties = xmlDoc.CreateNode(XmlNodeType.Element, “List”, “”);
XmlAttribute ndVersionAttrib = (XmlAttribute)xmlDoc.CreateNode(XmlNodeType.Attribute, “EnableVersioning”, “”);
ndVersionAttrib.Value = “TRUE”;
ndProperties.Attributes.Append(ndVersionAttrib);
XmlDocument doc = new System.Xml.XmlDocument();
XmlElement batchElement = doc.CreateElement(“Batch”);
XmlNode newItemhome = EnableVersions.GetListCollection();
try
{
_response = EnableVersions.UpdateList(ndList.Attributes["ID"].Value, ndProperties, null, null, null, ndVersion.Value);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + “\n ” + ex.StackTrace);
}
Console.WriteLine(“[versioning enabled]“);
}
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader or join our newsletter and receive future articles emailed directly to you.



Comments
No comments yet.
Leave a comment