In this article, I am going show how to read the
document version details from SharePoint document.
Create data table for version History
void CreateVersionHistoryTable(){try{dtVersionCollection = new DataTable();dtVersionCollection.Columns.Add("VersionId", typeof(string));dtVersionCollection.Columns.Add("VersionLabel", typeof(string));dtVersionCollection.Columns.Add("CreatedBy", typeof(string));dtVersionCollection.Columns.Add("Created", typeof(string));dtVersionCollection.Columns.Add("ModifiedBy", typeof(string));dtVersionCollection.Columns.Add("Modified", typeof(string));}catch (Exception){throw;}}
Read document version details
void GetAllVersion(){try{CreateVersionHistoryTable();SPSite Site = new SPSite(URL);using (SPWeb web = Site.OpenWeb()){SPList list = web.Lists[DocLibraryName];SPListItem item = list.GetItemById(DocumentID);SPListItemVersionCollection Fileversioncollection = item.Versions;foreach (SPListItemVersion version in Fileversioncollection){DataRow dr = dtVersionCollection.NewRow();dr["VersionId"] = version.VersionId;dr["Created"] = version.Created;dr["Modified"] = version["Modified"];dr["VersionLabel"] = version.VersionLabel;try{SPFieldUserValue userValue = new SPFieldUserValue(web, version.CreatedBy.ToString());SPUser userObject = userValue.User;dr["CreatedBy"] = userObject;}catch (Exception){ }try{SPFieldUserValue userValue = new SPFieldUserValue(web, version["Modified By"].ToString());SPUser userObject = userValue.User;dr["ModifiedBy"] = userObject;}catch (Exception){ }dtVersionCollection.Rows.Add(dr);}}}catch (Exception){return;}}
No comments:
Post a Comment