Thursday, August 9, 2012

Delete and Restore File Versions Programmatically Using Web Service

In my last post I talked about "How to get file versions programatically". In this post, I want to extend the same with other things we can do with "Versions.asmx".

For this exercise add reference of webservice in your application like:
http://<SiteURL>/_vti_bin/versions.asmx

Enter namespace name for service reference. In my case I entered 'VersionService'.
Instantiate SoapClient wherever appropriate in your code.

VersionService.VersionsSoapClient versionServiceClient = new VersionsSoapClient();


Now, we are ready to write code for different operations we can do.

Delete All Versions of File

To delete all the versions but current one call this method in your code.

                 /// <summary>
        /// Returns XElement with metadata of current version of file
        /// </summary>
        /// <param name="FileUrl">Document library name/file name with extension</param>
        /// <returns>XElement for current version</returns>
        public XElement DeleteAllFileVersions(string FileUrl)
        {
            try
            {
                XElement versionElement = versionServiceClient.DeleteAllVersions(FileUrl);
                return versionElement;
            }
            catch
            {
                return null;
            }
        }


Usage:
XElement currentVersionElement = DeleteAllFileVersions("myDocLib/myDocument.docx");

Delete Specific Version of File

To delete specific file version use following method in your code.

            /// <summary>
        /// Deletes specific version of file
        /// </summary>
        /// <param name="FileUrl">Document library name/file name with extension</param>
       /// <param name="FileVersion">Version to delete</param>
        /// <returns>XElement with metadata of remaining versions of file</returns>
        public XElement DeleteSpecificFileVersion(string FileUrl, string FileVersion)
        {
            try
            {
                XElement versionElement = versionServiceClient.DeleteVersion(FileUrl, FileVersion);
                return versionElement;
            }
            catch
            {
                return null;
            }
        }


Usage:
XElement versionElement = DeleteSpecificFileVersion("myDocLib/myDocument.docx","5");

Restore Specific Version of File

To restore specific version use following method.

                 /// <summary>
        /// Restores specific version of file
        /// </summary>
        /// <param name="FileUrl">Document library name/file name with extension</param>
       /// <param name="FileVersion">Version to restore</param>
        /// <returns>XElement with metadata of remaining versions of file</returns>
        public XElement RestoreSpecificFileVersion(string FileUrl, string FileVersion)
        {
            try
            {
                XElement versionElement = versionServiceClient.RestoreVersion(FileUrl, FileVersion);
                return versionElement;
            }
            catch
            {
                return null;
            }
        }


Usage:
XElement versionElement = RestoreSpecificFileVersion("myDocLib/myDocument.docx","9");

As I mentioned in my last post, we can use these methods in Silverlight application and through javascript too.

Wait for next VERSION of my versioning related post :)

Tuesday, August 7, 2012

SharePoint - Get file versions programmatically

In SharePoint, we can enable versioning for a document library. SharePoint will take care of maintaining versions and also it allows to view, restore and delete previous versions. This feature helps a lot in document management. To enable versioning on a document library go to 'Library Settings ->Versioning Settings'. You will get following settings page where you can easily enable versioning.




You can see there are very good options provided OOB which help us in maintaining major and minor versions and also check out option.

You can get more information related to versioning here.

This is about versioning and settings. But, as a developer we need more. There are many scenarios when we want to get version history of the document programmatically. Here is very simple solution - web service. We can use 'versions.asmx' to get version history of file.
We can consume this web service from JavaScript and Silverlight application also. Here we will consider C# code.

Create windows form or silverlight application and add service reference as:
http://<SiteURL>/_vti_bin/versions.asmx

Enter namespace name for service reference. In my case I entered 'VersionService'.
Instantiate SoapClient wherever appropriate in your code.

VersionService.VersionsSoapClient versionServiceClient = new VersionsSoapClient();

Now, let's write a method which will return version history in xml format.


        /// <summary>
        /// Returns XElement with version history of file using web service
        /// </summary>
        /// <param name="FileUrl">Relative url of file</param>
        /// <returns>XElement for version</returns>
        public XElement GetFileVersions(string FileUrl)
        {
            try
            {
                XElement versionElement = versionServiceClient.GetVersions(FileUrl);
                return versionElement;
            }
            catch
            {
                return null;
            }
        }

'GetVersions' method returns version history in xml format as XElement.
Isn't it easy to get versions of a file?

FileUrl parameter should be like "document library name/file name with extension".

Now, let's do more with this method. Consider the scenario where you want current version of file. I have written a method which will use Linq to XML query and return current version of file.

        /// <summary>
        /// Returns current version of file
        /// </summary>
        /// <param name="FileUrl">Server relative url of file</param>
        /// <returns></returns>
        public string GetCurrentFileVersion(string FileUrl)
        {
            string CurrentFileVersion = string.Empty;
            XElement versionElement = GetFileVersions(FileUrl);
            if (versionElement != null)
            {
                //Get child element with name 'result' and whose attribute named 'version' contains '@'
                var resultElements = versionElement.Elements();
                var rows = from row in resultElements
                           where row.Name.LocalName == "result" &&
                           row.Attribute("version").Value.Contains("@")
                           select row;
                foreach (var row in rows)
                {
                    CurrentFileVersion = row.Attribute("version").Value;
                }
                CurrentFileVersion = CurrentFileVersion.Replace("@", string.Empty);
            }
            return CurrentFileVersion;
        }

For my code I required version in string format. You can covert it to integer.
Simple way to get file version history!!