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

1 comment:

  1. Hi,
    I have a document Library with name Test under ABC Site.I want to delete all versions with CSOM or jquery code.CAn you plz update me on complete code

    ReplyDelete