Pages

Search This Blog

Monday, November 28, 2011

Query SPListItemVersionCollection to get version data on a specific date

 You can use object model to get the required data.

    SPSite oSite = new SPSite("Site URL");
            SPWeb oWeb = oSite.OpenWeb();
            SPList oList = oWeb.Lists["Shared Documents"];

            SPListItem oListItem = oList.Items.GetItemById(1);
            DateTime inputDate = new DateTime(2010, 6, 11);
            SPListItemVersionCollection oVersionInfo = oListItem.Versions;
            foreach (SPListItemVersion oVersion in oVersionInfo)
            {
                if (Convert.ToDateTime(oVersion["Modified"]).ToShortDateString() == inputDate.ToShortDateString())
                {
//write your logic here
                }
            }

Alternatively, you can enable Auditing on your site collection/list and after that you can use SPAuditQuery to get auditing report.
Enable Audit logs on Site Collection:
oSite.Audit.AuditFlags = SPAuditMaskType.All;
oSite.Audit.Update();

Get Audit report using SPAuditQuery :
            SPAuditEntryCollection objSPAuditEntryCollection;
            SPAuditQuery objSPAuditQuery = new SPAuditQuery(oSite);
            objSPAuditQuery.RestrictToListItem(oListItem);
            objSPAuditEntryCollection = oSite.Audit.GetEntries(objSPAuditQuery);
            foreach (SPAuditEntry objSPAuditEntry in objSPAuditEntryCollection)
            {
                // get all audit entry…
            } 

No comments:

Post a Comment