Friday, December 10, 2010

Sharepoint List Item Version History

if (docListItem != null)
                        {
                            // Create new DataTable.
                            DataTable dtHistory = new DataTable();
                            try
                            {

                                //starts History section
                                // Declare DataColumn and DataRow variables.
                                DataColumn LastModifiedBy;
                                DataColumn LastModifiedOn;
                                DataColumn Comments;
                                DataColumn Version;
                                DataRow row;

                                // Create new DataColumn, set DataType, ColumnName
                                // and add to DataTable.   
                                //column.DataType = System.Type.GetType("System.Int32");
                                LastModifiedBy = new DataColumn();
                                LastModifiedBy.ColumnName = "LastModifiedBy";
                                dtHistory.Columns.Add(LastModifiedBy);

                                LastModifiedOn = new DataColumn();
                                LastModifiedOn.ColumnName = "LastModifiedOn";
                                dtHistory.Columns.Add(LastModifiedOn);

                                Comments = new DataColumn();
                                Comments.ColumnName = "Comments";
                                dtHistory.Columns.Add(Comments);

                                Version = new DataColumn();
                                Version.ColumnName = "Version";
                                dtHistory.Columns.Add(Version);

                                SPFieldUserValue ModifiedBy = new SPFieldUserValue(web, docListItem["Editor"].ToString());

                                row = dtHistory.NewRow();
                                row["LastModifiedBy"] = ModifiedBy.LookupValue;
                                row["LastModifiedOn"] = docListItem["Modified"];


                                if (docListItem["_CheckinComment"] != null) //Check In Comment
                                {
                                    if (docListItem["_CheckinComment"].ToString().Length >= 3)
                                        row["Comments"] = docListItem["_CheckinComment"].ToString().Remove(0, 3);
                                }

                                row["Version"] = docListItem["_UIVersionString"];
                                dtHistory.Rows.Add(row);
// it gets latest record
                                for (int i = 0; i < docListItem.File.Versions.Count; i++)
                                {
                                    row = dtHistory.NewRow();
                                    row["LastModifiedBy"] = docListItem.File.Versions[i].CreatedBy.Name;
                                    row["LastModifiedOn"] = docListItem.File.Versions[i].Created.ToLocalTime().ToString();
                                    row["Comments"] = docListItem.File.Versions[i].CheckInComment;

                                    //Response.Write("2nd loop: " + docListItem.File.Versions[i].CheckInComment);

                                    row["Version"] = docListItem.File.Versions[i].VersionLabel;
                                    dtHistory.Rows.Add(row);
                                }

                                //grdHistory.DataSource = docListItem.Versions;

                                dtHistory.DefaultView.Sort = "Version Desc";
                                grdHistory.DataSource = dtHistory;
                                grdHistory.DataBind();

                            }
                            catch (Exception ex)
                            {
                                Response.Write(ex.Message);
                            }
                            finally
                            {
                                dtHistory.Dispose();
                            }


                            //Activity log section
                            SPAuditQuery wssQuery;
                            SPAuditEntryCollection auditCol;
                            wssQuery = new SPAuditQuery(ElevatedWeb.Site);
                            wssQuery.RestrictToListItem(docListItem = documentListCollection[0]);
                            auditCol = ElevatedWeb.Site.Audit.GetEntries(wssQuery);

                            DataTable dtActivity = new DataTable("ActivityLog");
                            DataColumn colLog = new DataColumn();
                            colLog.ColumnName = "Log";
                            DataRow drow;
                            dtActivity.Columns.Add(colLog);

                            foreach (SPAuditEntry entry in auditCol)
                            {
                                if (entry.Event == SPAuditEventType.CheckOut || entry.Event == SPAuditEventType.CheckIn || entry.Event == SPAuditEventType.FileFragmentWrite)
                                {

                                    drow = dtActivity.NewRow();
                                    // if (ParseVersionNumber(entry.EventData) != "N/A")
                                    // {
                                    drow[0] = entry.Occurred.ToLocalTime() + " : " + entry.Event + " by " + GetUserNameById(entry.UserId, site);
                                    dtActivity.Rows.Add(drow);
                                    //}
                                }
                            }
                            grdActivitylog.DataSource = dtActivity;
                            grdActivitylog.DataBind();
                            dtActivity.Dispose();

                        }
                    }
                               

No comments:

Post a Comment

Followers