Tuesday, February 2, 2010

Update Item properties after document is checked in without checking out again

There might be cases when you would want to update an Item's/Document's property after it has been checked in. You would not want to checkout the item again as you want to maintain the version number that was generated by UI checkin. For example in my case I wanted to update a field when the document was published with major version and approved by approval workflow.

You could not do in ItemUpdating because some fo the fields are not exposed in Item.AfterProperties collection and also it would result in an infinite loop.

Option left is ItemUpdated event, but in this even the document is already being checked in and published with major version. So how can you do it without checking out the document again. This is what you can do:

SPSecurity.RunWithElevatedPrivileges(delegate
{
if (file.Item.ModerationInformation.Status == SPModerationStatusType.Approved)
{

file.Item["DocumentNumber"] = "XYZ";
file.Item.SystemUpdate(false);
}
}

Doing Item.SystemUpdate(false) does not increments the version number and does not update modified date or modified by and running under elevated privilegs it does not look to check out the document.

Hope it will be helpful to some one.
Dogpile
Powered By Blogger