Hi All,
In a following Post we will see how to read List Item Versions ,following Method it is returning Version Data Table.
Please find the below Code Snippet:
In a following Post we will see how to read List Item Versions ,following Method it is returning Version Data Table.
Please find the below Code Snippet:
public DataTable GetVersionHistoryFromList(Reports obj) { try { using (SPSite oSite = new SPSite(SPContext.Current.Web.Url)) { using (SPWeb oWeb = oSite.OpenWeb()) { SPList oList = oWeb.Lists.TryGetList("ListName"); SPQuery oQuery = new SPQuery(); oQuery.Query = "Thank you.."; SPListItemCollection itemColleciton = oList.GetItems(oQuery); DataTable dtVersionHistory = new DataTable(); if (itemColleciton.Count > 0) { dtVersionHistory.Columns.Add("ListColumn_Name", typeof(string)); dtVersionHistory.Columns.Add("Modified By", typeof(string));// dtVersionHistory.Columns.Add("Modified On", typeof(DateTime));// foreach (SPListItem item in itemColleciton) { SPListItemVersionCollection versionCollection = item.Versions; if (versionCollection.Count > 0) { foreach (SPListItemVersion itemVersion in versionCollection) { int versionID = itemVersion.VersionId; versionLabel = itemVersion.VersionLabel.ToString(); SPListItem oListItem = itemVersion.ListItem; DataRow drNewRow = dtVersionHistory.NewRow(); if (!string.IsNullOrEmpty(oListItem.Versions.GetVersionFromLabel(versionLabel)["ListColumn_Name"].ToString())) { drNewRow["ListColumn_Name"] = oListItem.Versions.GetVersionFromLabel(versionLabel)["ListColumn_Name"].ToString(); } //"Item Modified By" strFullName = oListItem.Versions.GetVersionFromLabel(versionLabel)["Modified By"].ToString(); strUser = strFullName.Contains("\\") ? string.Format("{0}", strFullName).Split(new char[] { '\\' })[1].ToString().Split(new char[] { ',' })[0].ToString() : string.Format("{0}", strFullName); drNewRow["Modified By"] = strUser; //"Modified" if (!string.IsNullOrEmpty(oListItem.Versions.GetVersionFromLabel(versionLabel)["Modified On"].ToString())) { drNewRow["Modified On"] = Convert.ToDateTime(oListItem.Versions.GetVersionFromLabel(versionLabel)["Modified On"]); } dtVersionHistory.Rows.Add(drNewRow); } } } } return dtVersionHistory; } } } catch (Exception ex) { //Exception Handling } }
0 comments:
Post a Comment