none
Programmatically get document Version History using client object model sharepoint 2010

    Question

  • Hello,

    I am trying to retrieve document version history in a list using client object model sharepoint 2010,

    somehow the below sample code is not helping me to achieve its functionality.

    public void GetVersions()
    {

    ClientContext clientContext = new ClientContext(“http://SPSite”);
    Web site = clientContext.Web;
    clientContext.Load(site);
    clientContext.ExecuteQuery();

    File file = site.GetFileByServerRelativeUrl(“/Shared Documents/mydocument.doc”);
    clientContext.Load(file);
    clientContext.ExecuteQuery();

    ListItem currentItem = file.ListItemAllFields;
    clientContext.Load(currentItem);
    clientContext.ExecuteQuery();

    FileVersionCollection versions = file.Versions;
    clientContext.Load(versions);
    clientContext.ExecuteQuery();

    if (versions != null)
    {
    foreach(FileVersion _version in versions)
    {
    Console.WriteLine(“Version : {0}”,_version.VersionLabel);
    }
    }}

    Can you please help me to fix it..

    thanks

    Kajal

    Wednesday, July 04, 2012 4:32 PM

Answers

  • It may be that the ClientContext just ignores what's behind the site collection part of the url, I have never used it that way, so I was really surprised that it worked, but good to know for later. ..

    Just try to check out the document, edit , save and check it back in. I just did that, and  the count was incremented as expected..


    Kind Regards Bjoern
    Blog


    Wednesday, July 04, 2012 8:32 PM
    Moderator

All replies

  • What output does Console.WriteLine(“Version : {0}”,_version.VersionLabel); gives you?

    can you try fixing quotes in the code


    Akrura Technologies is a leading Toronto-based SharePoint Consulting firm which offers expert Development Services for Microsoft SharePoint 2010, SharePoint 2007, SharePoint Online and Office 365. Visit Us at Akrura Technologies

    Wednesday, July 04, 2012 6:08 PM
  • I get version: version1.0, though I have version 2.0 also but it does not loop through.

    rgds

    Wednesday, July 04, 2012 6:40 PM
  • Hi

    This code works for me, testing file with 2 major versions in the history

    using (ClientContext clientContext = new ClientContext("http://SPSite"))
    {
     Web site = clientContext.Web;
     clientContext.Load(site);
     File file = site.GetFileByServerRelativeUrl("/Shared Documents/mydocument.doc");               

    clientContext.Load(file);
     FileVersionCollection versions = file.Versions;
     clientContext.Load(versions);             
     clientContext.ExecuteQuery();
     if (versions != null)
     {
      foreach(FileVersion version in versions)
      {
       Console.WriteLine("Version : {0}",version.VersionLabel);
      }
     }
    }



    Kind Regards Bjoern
    Blog




    Wednesday, July 04, 2012 6:50 PM
    Moderator
  • hello Bjoern

    my code goes like this, sorry for the bad format, test05.txt has 2 versions, 1.0 and 2.0 but the console readline returns only 1.0.

    namespace ConsoleApplication1

    {

        class Program

        {

            static void Main(string[] args)

            {

                    ClientContext clientContext = new ClientContext("http://cvgwinbasd003:28838/sites/test04/_layouts/viewlsts.aspx");

                    var credentials = new NetworkCredential("Mishas50", "**********", "Enterprise");

                    clientContext.Credentials = credentials;

                    Web rootWeb = clientContext.Web;

                    clientContext.Load(rootWeb);

                    //Web site = clientContext.Web;

                    //clientContext.Load(site);

                    File file = clientContext.Web.GetFileByServerRelativeUrl("/sites/test04/Doc1/Test05.txt");

                    clientContext.Load(file);

                    FileVersionCollection versions = file.Versions;

                    clientContext.Load(versions);

                    clientContext.ExecuteQuery();

     

                    if (versions != null)

                    {

                    foreach(FileVersion version in versions)

                    {

                    Console.Write("Version : {0}",version.VersionLabel);

                    }

                    Console.ReadLine();                            

                    }

            }

        }

    }

    Regards

    Kajal

    Wednesday, July 04, 2012 7:20 PM
  • I'm surprised if this line works..

    ClientContext clientContext = new ClientContext("http://cvgwinbasd003:28838/sites/test04/_layouts/viewlsts.aspx");

    url shouldn't contain any more than the site collection that you want to reference in the context

    ClientContext clientContext = new ClientContext("http://cvgwinbasd003:28838");

    Try testing outputting versions.Count to the console to make sure that you really have 2 versions registered.

    And there is no point in loading a  web object called rootWeb, if you're not using it

    Web rootWeb = clientContext.Web;
    clientContext.Load(rootWeb);
    File file = rootWeb.GetFileByServerRelativeUrl("/sites/test04/Doc1/Test05.txt");


    Kind Regards Bjoern
    Blog





    Wednesday, July 04, 2012 7:38 PM
    Moderator
  • Hey Bjoern,

    Not sure how come

    ClientContext clientContext = new ClientContext("http://cvgwinbasd003:28838/sites/test04/_layouts/viewlsts.aspx");

    this works for me perfectly fine. I have been doing all actions like upload/delete/create folder etc.

    I did versions.Count the value says [1]. So that may be the reason .... not sure the version history says 1.0,2.0 for the file

    but counts says 1 :((

    Regards

    Kajal

    Wednesday, July 04, 2012 8:24 PM
  • It may be that the ClientContext just ignores what's behind the site collection part of the url, I have never used it that way, so I was really surprised that it worked, but good to know for later. ..

    Just try to check out the document, edit , save and check it back in. I just did that, and  the count was incremented as expected..


    Kind Regards Bjoern
    Blog


    Wednesday, July 04, 2012 8:32 PM
    Moderator
  • As always thanks a lot :)))

    Checkout and Check in registered a new version and the count incremented by 1. Now I can see the increment, but when I check version history from sharepoint it says 1.0,2.0,3.0 where I know 3.0 is version which I got after checking in .

    The version History popup has version no, filename,modified date,by etc can we recreate that also with COM, i could not manage to get something similar any ray of hope there also.

    Wednesday, July 04, 2012 8:49 PM
  • Hi Kajal,

    I have the same issue. I have the following code:

    FileVersionCollection versions = listItem.File.Versions;

    It returns a version less the high version.  If the high version is 2.0, it return 1.0

    If you have the solution, please let me know.

    Many thanks.

     

    Thursday, May 30, 2013 9:13 PM
  • Kajal/eg0013, I had the same issue! But the following workaround did the trick. 

    http://www.captechconsulting.com/blog/amarender-peddamalku/sharepoint-2010-retrieve-document-library-version-history-using-client

    namespace GetSPDocLibVersionHistory
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Change your site url below
                using (ClientContext clientContext = new ClientContext("http://sp2010/CapTechBlog/"))
                {
                    Web site = clientContext.Web;
                    // Change your document library name
                    List docLib = site.Lists.GetByTitle("Shared Documents");
                    clientContext.Load(docLib);
    
                    CamlQuery caml = new CamlQuery();
                    ListItemCollection items = docLib.GetItems(caml);
    
                    clientContext.Load(items);
                    clientContext.ExecuteQuery();
    
                    // Iterate through all items in the document library 
                    foreach (ListItem item in items)
                    {
    
                        Console.WriteLine("File Name:: " + item.FieldValues["Title"]);
    
                        clientContext.Load(item.File);
                        FileVersionCollection versions = item.File.Versions;
    
                        clientContext.Load(versions);
                        clientContext.ExecuteQuery();
    
                        // If the file has versions, loop through all of the versions
                        if (versions != null)
                        {
                            foreach (FileVersion version in versions)
                            {
                                User usr = version.CreatedBy as User;
                                clientContext.Load(usr);
                                clientContext.ExecuteQuery();
    
                                Console.WriteLine("Version Info:: {0}, {1}, {2}, {3}", version.VersionLabel, version.Created, usr.LoginName, version.CheckInComment);
                            }
                        }
    
                        FieldUserValue modBy = item["Editor"] as FieldUserValue;
                        // Get the latest verion of the file
                        Console.WriteLine("Version Info:: {0}, {1}, {2}, {3}", item.File.UIVersionLabel, item.File.TimeLastModified, modBy.LookupValue, item.File.CheckInComment);
                        Console.WriteLine("---------------------------------------------------------------");
                    }
    
                    Console.ReadLine();
    
                }
            }
        }
    }


    Friday, November 01, 2013 2:09 PM
  • Hi Kajal,

    Below code will fix your issue

    public void GetVersions()
    {
    ClientContext clientContext = new ClientContext(“http://SPSite”);

    File file = clientContext.Web.GetFileByServerRelativeUrl(“/Shared Documents/mydocument.doc”);
    clientContext.Load(file, f => f.UIVersionLabel, f => f.Versions);
    clientContext.ExecuteQuery();

    // To fetch the Current version
    Console.WriteLine(“Version : {0}”, file.UIVersionLabel);

    // To fetch the previous versions
    foreach(FileVersion _version in versions)
    {
    Console.WriteLine(“Version : {0}”,_version.VersionLabel);
    }

    }


    • Edited by Venkadesh S Tuesday, November 12, 2013 7:08 AM
    Tuesday, November 12, 2013 7:03 AM
  • Hi eg10013,

    Below code will fix your issue

    public void GetVersions()
    {
    ClientContext clientContext = new ClientContext(“http://SPSite”);

    File file = clientContext.Web.GetFileByServerRelativeUrl(“/Shared Documents/mydocument.doc”);
    clientContext.Load(file, f => f.UIVersionLabel, f => f.Versions);
    clientContext.ExecuteQuery();

    // To fetch the Current version
    Console.WriteLine(“Version : {0}”, file.UIVersionLabel);

    // To fetch the previous versions
    foreach(FileVersion _version in versions)
    {
    Console.WriteLine(“Version : {0}”,_version.VersionLabel);
    }

    }

    Tuesday, November 12, 2013 7:08 AM