Get Sharepoint Document FileVersions using C#

Deepthi Vangapandu 1 Reputation point
2023-01-07T18:51:12.823+00:00

Hi:
My question is to retrieve document version history including the current version.
I used the below, and I am able to get the version history but in that list current version data is not included. instead current version details are loaded into the file item.

List list = ctx.Web.Lists.GetByTitle("DocumentLibraryTitle");
ListItem item = list.GetItemById(1);
ListItemVersionCollection versions = item.Versions;
Microsoft.SharePoint.Client.File file = item.File;
FileVersionCollection fileVersions = file.Versions;
ctx.Load(list); ctx.Load(item); ctx.Load(versions);
ctx.Load(file); ctx.Load(fileVersions);
ctx.ExecuteQuery();

My requirement is to load all the versions of the document into a combobox and based on the selection I need to document that versioned file.
I request someone to provide me a solution.

Microsoft 365 and Office SharePoint For business Windows
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-01-09T02:37:15.123+00:00

    Hi @Deepthi Vangapandu
    We can use FileVersion.IsCurrentVersion property to check and get current version. Please refer to the following code

    using (ClientContext clientContext = new ClientContext("http://xxx.sharepoint.com/sites/MySiteCollection"))  
    {  
    List targetList = clientcontext.Web.Lists.GetByTitle("Documents");  
    ListItem oItem = targetList.GetItemById(13);  
    CamlQuery oQuery = new CamlQuery();  
       
    oQuery.ViewXml = @"<View><Query><Where>  
    <Eq>  
    <FieldRef Name='LinkFilename' />  
    <Value Type='Text'>document.docx</Value>  
    </Eq>  
    </Where></Query></View>";  
       
    ListItemCollection oItems = targetList.GetItems(oQuery);  
    clientcontext.Load(oItems);  
    clientcontext.ExecuteQuery();  
       
    oItem = oItems.FirstOrDefault();  
       
    FileVersionCollection oFileVersionCollection = oItem.File.Versions;  
    clientcontext.Load(oFileVersionCollection);  
    clientcontext.ExecuteQuery();  
       
    foreach (FileVersion oFileVersion in oFileVersionCollection)  
    {  
    if (oFileVersion.IsCurrentVersion)  
    {  
    Console.WriteLine("Current Verrsion of the file: " + oFileVersion.VersionLabel);  
    }  
    }  
    }  
    

    Here is the details about FileVersion.IsCurrentVersion property
    https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-csom/ee539266(v=office.15)


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.