0

I have document library which has over 100 items. Each item has over 30 to 40 major & minor versions. I want to get only major versions of particular item programmatically using C#.

foreach (SPListItem item in list.Items)
{
  foreach (SPListItemVersion itemVersion in item.Versions)
  {
    if (item.File.Versions.GetVersionFromLabel(itemVersion.VersionLabel) != null)
    {                                                        
      // Get only major versions of item here ????
    }
  }                                                    
}     
  • could you please share the whole fucntion from start as I not able to get it. – user56080 Jun 11 '16 at 17:28
  • What do you want to get ? – Ronak Patel Jun 13 '16 at 4:03
1

You can use just a small trick as mentioned below.

Sample Code :

foreach (SPListItem item in list.Items)
{
  foreach (SPListItemVersion itemVersion in item.Versions)
  {
    if (item.File.Versions.GetVersionFromLabel(itemVersion.VersionLabel) != null)
    {                                                        
      if((Convert.ToDouble(itemVersion.VersionLabel)%1) == 0)
      {
         //You can get all major versions here.
      }
    }
  }                                                    
}
1

You can split the version label and check is the second part 0

string[] versionArray = itemVersion .VersionLabel.Split('.');
int minorVersion = Convert.ToInt32(versionArray[1]);
if (minorVersion == 0)
{
    //this is a major version
}

or you can check the reminder when dividing VersionId with 512

int modulo = itemVersion.VersionId % 512;
if (modulo == 0)
{
    //this is a major version
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.