Tuesday, June 12, 2012

Read SPListItemVersion in SharePoint 2010

SPListItemVersion Class return the collection of versions for a list item. If versioning is enabled on a list, each time you modify an item a version is created.

Enable SharePoint List versioning using c#:


using (SPSite spsite = new SPSite("http://contoso.com"))
{
SPWeb spweb = spsite.OpenWeb();
SPList list = spweb.Lists["ListName"];
list.EnableVersioning = true;
list.Update();
}



Retrieve SPListItem versions using c#:


public  class PageVersions
{
public string PageVersion { get; set; }
public SPUser PageVersionOwner { get; set; }
public DateTime PageVersionDate { get; set; }
}

public class GetVersionsClass
{

public PageVersions CalculateVersionPoints(SPListItem item,int prevDates)
{
PageVersions pv = new PageVersions();

SPListItemVersionCollection oVersionInfo = item.Versions;

foreach (SPListItemVersion oVersion in oVersionInfo)
{
if (oVersion.Level == SPFileLevel.Published)
{



if (oVersion.Created.ToLocalTime().CompareTo(DateTime.Now.AddDays(prevDates)) >= 0)
{
pv.PageVersion = oVersion.VersionId.ToString();
pv.PageVersionDate = oVersion.Created;
pv.PageVersionOwner = oVersion.CreatedBy.User;

}

}
}

return pv;
}
}


No comments:

Post a Comment