Share via

Do ListItemVersion.VersionId and FileVersion.Id have the same value for a particular version?

nyacharya 21 Reputation points
2024-01-08T09:02:11.2666667+00:00

Hello

We use CSOM to capture FileVersion.Id for a particular file version of a file in various libraries and retrieve it later. Following are my questions -

  • Are ListItemVersion.VersionId for the list item version of that file version and FileVersion.Id always the same?
  • If we pass FileVersion.Id to ListItem.Version.GetById to retrieve a specific ListItemVersion and then load its fileVersion, will it load the same fileVersion? see below
    var lisItemVersion = listItem.Versions.GetById(fileVersionId) ;
    clientContext.Load(listItemVersion);
    clientContext.Load(listItemVersion.FileVersion, x => x.ID, x => x.Length, x => x.CreatedBy, x => x.Url);
    OR
    Do we need to load all listItem.Versions and corresponding fileVersions and check for file version id individually?
    clientContext.Load(listItem.Versions, f => f.Include(x => x.VersionId, x => x.FileVersion.ID, x => x.FileVersion.Length, x => x.FileVersion.CreatedBy, x => x.FileVersion.Url));
    foreach (ListItemVersion x in listItem.Versions)
    {
    if (x.FileVersion.ID == fileVersionId)
    {
    return x.FileVersion;
    }
    }

Thanks

Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | SharePoint | Development

Answer accepted by question author

RaytheonXie_MSFT 40,496 Reputation points Microsoft External Staff
2024-01-10T08:49:54.21+00:00

Hi @nyacharya,

Per my research, the ListItemVersion.VersionId and FileVersion.Id will return the same value. You could refer to the following document

https://learn.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.client.listitemversion.versionid?view=sharepoint-csom#microsoft-sharepoint-client-listitemversion-versionid

https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-csom/ee542654(v=office.15)

You could use the code to get sharepoint file version

// Get File using version Id
// NOTE: Every Major check In will be incremented by "512" e.g. version 1.0 will have ID = 512, Version 2.0 will have ID = 1024
//Every Minor check in will be incremented by "1" e.g version 0.1 will have ID = 1 , version 0.2 will have ID = 2

FileVersion oFileVersion = oItem.File.Versions.GetById(512);
clientcontext.Load(oFileVersion);
clientcontext.ExecuteQuery();


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.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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