30 April 2011

Gotchas while Updating date field while restoring item from recycle bin Moss 2010

Recently I came across a scenario in which I need to restore a document which is send to recycle bin by a retention process. And I need to reset the date field that is reference for retention, so that it will again fall in the retention cycle.
So How should I know in the event receiver that the ‘ItemAdding” is happening when the item is newly added or item is being restored. I will know this by checking the ListitemID property inside the “ItemAdding” event , If ListItemId is non zero then it is fired from restore. But ‘ItemAdding” event will not allow you to change any property while the item is getting restored.
So I need to relay on the ‘Itemadded” Event. So here how should I check that The event is fired from restore or while ‘ItemAdding” initially. I checked the current date with the created date of the list item for this. If they are different then the event is being fired from the restore.(the business logic is that you need to set min 1 day retention period).And ‘Itemadded” event is the only place I can reset my datefield refered by the retention process.
I faced another issue here, if your server is in a different time zone than the client you cannot update this date field (need to use ISO format) with datetime.now. You need to use datetime.utcnow.
But if the server is in the same time zone You can either use DateTime.Nowor DateTime.UtcNowto update the date field.
So I done similar below to deploy the code in the server that reside in a different time zone.
public override void ItemAdded(SPItemEventProperties properties)
        {
                base.ItemAdded(properties);
                EventFiringEnabled = false;
                SPListItem currentItem = properties.ListItem;
                DateTime created = (DateTime)currentItem[CREATED];
                //Compare the Created Date and The Present datetime. If Created date is not same as current datetime, it means the docuemnt was restored.
               //While restoring retention date is set to the current utc date, so that it will fall back to recycle bin  in the next retention stage
                int recycle = DateTime.Compare(created.Date, System.DateTime.Now.Date);
                if (recycle != 0)
               { //Update The UploadedDate to Current Date Time
                    DateTime today = DateTime.UtcNow.Date;
                    Util.LogInfo("UTC", SPUtility.CreateISO8601DateTimeFromSystemDateTime(today));
                    currentItem[RET_REF_DATE_STAT] = SPUtility.CreateISO8601DateTimeFromSystemDateTime (today);
currentItem.Update();
}
 }

No comments:

Post a Comment