Saturday, October 1, 2011

Programmatically get custom list and document library Version History

private void GetVersionHistory()
{
try
{
using (SPSite site = new SPSite(SPContext.Current.Site.Url.ToString()))
{
using (Microsoft.SharePoint.SPWeb web = site.OpenWeb())
{
Guid id = new Guid(this.Request.QueryString["ListId"].ToString());
SPList objList = web.Lists[id];
DataTable dtPhoto = new DataTable("dtPhoto");
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;

if (objList.BaseType == SPBaseType.DocumentLibrary)
{
char[] sep = { '' };
char[] sepHash = { '#' };
SPListItem item = objList.GetItemById(Convert.ToInt32( this.Request.QueryString["ItemId"].ToString()));
string strname = item["Name"].ToString();
foreach (SPFile file in objList.RootFolder.Files)
{
if (file.Name == strname)
{
dtPhoto.Columns.Add("Version");
dtPhoto.Columns.Add("Modified");

dtPhoto.Columns.Add("Modified By");
dtPhoto.Columns.Add("Comments");
DataRow dtrow;

dtrow = dtPhoto.NewRow();
dtrow["Version"] = file.UIVersionLabel;
dtrow["Modified"] = item["Modified"].ToString();

string[] str1 = item["Modified By"].ToString().ToString().Split(sepHash);

dtrow["Modified By"] = ( str1[1].ToString());
dtrow["Comments"] = file.CheckInComment;

dtPhoto.Rows.Add(dtrow);

SPFileVersionCollection filecollection = file.Versions;
if (filecollection.Count > 0)
{



foreach (SPFileVersion v in filecollection)
{

dtrow = dtPhoto.NewRow();
dtrow["Version"] = v.VersionLabel;
dtrow["Modified"] = v.Created;
string[] str = v.CreatedBy.ToString().Split(sep);

dtrow["Modified By"] =GetUserName( str[1].ToString());
dtrow["Comments"] = v.CheckInComment;

dtPhoto.Rows.Add(dtrow);

}
if (dtPhoto != null && dtPhoto.Rows.Count > 0)
{
gvVersion.DataSource = dtPhoto;
gvVersion.DataBind();


}
else
{
trversion.Visible = false;
gvVersion.Visible = false;

}
return;
}
}
}
}
else
{

#region


SPQuery objQuery = new SPQuery();
objQuery.Query = "<Where><Eq><FieldRef Name=\"ID\"/><Value Type=\"Counter\">" + this.Request.QueryString["ItemId"].ToString() + "</Value></Eq></Where>";
SPListItemCollection objItemColl = objList.GetItems(objQuery);


foreach (SPListItem objItem in objItemColl)
{
SPListItemVersionCollection objVerisionColl = objItem.Versions;
if (objVerisionColl.Count > 0)
{
dtPhoto.Columns.Add("Version");
dtPhoto.Columns.Add("Modified");
dtPhoto.Columns.Add("Modified By");
dtPhoto.Columns.Add("Comments");

DataRow dtrow;


//Navigate to each version of the ListItem
foreach (SPListItemVersion objVersion in objVerisionColl)
{
SPListItem objLstItm = objVersion.ListItem;
char[] sep = { ',' };
char[] sep1 = { ';' };
dtrow = dtPhoto.NewRow();
dtrow["Version"] = objVersion.VersionLabel;
dtrow["Modified"] = objLstItm.Versions.GetVersionFromLabel(objVersion.VersionLabel)["Modified"].ToString();
string[] strtest = objLstItm.Versions.GetVersionFromLabel(objVersion.VersionLabel)["Modified By"].ToString().Split(sep);
string[] str11 = strtest[0].Split(sep1);
string s = str11[1].ToString().Replace("#", "");
dtrow["Modified By"] = s;
dtrow["Comments"] = "";

dtPhoto.Rows.Add(dtrow);
}
}
}
#endregion

}


if (dtPhoto != null && dtPhoto.Rows.Count > 0)
{
gvVersion.DataSource = dtPhoto;
gvVersion.DataBind();


}
else
{
trversion.Visible = false;
gvVersion.Visible = false;

}



}
}
}
catch (Exception)
{


}
}