Tuesday, February 24, 2009

Don't want to create new version or want to edit fields like Author, Modified, etc
When you have versioning enabled on document library or list and when you do not want to create a new version even you are updating an item in the list or document library at that time you can use this method because Update will create version for the item.

Many times i also found that in workflow when you want to update any item on which workflow is running and if you use Update method on the SPListItem, it throws Error in terms on Error Starting in Workflow. When i use this UpdateOverwriteVersion, it didn't give me.

With the help of this method, you can also set certain properties like Author, Editor and Modified. Remember you need to change both Author as well as Editor on these properties to take effect.

Example :

using (SPSite objSite = new SPSite("http://server/"))
{
using (SPWeb objWeb = objSite.OpenWeb())
{
SPList objDocLib = objWeb.Lists["Shared Documents"];
SPListItem objItem = objDocLib.Items[0];
objItem ["Created"] = new DateTime(2008, 5, 5);
objItem ["Author"] = "{author}";
objItem ["Editor"] = "{editor}";
objItem ["Modified"] = new DateTime(2008, 5, 6);
objItem.UpdateOverwriteVersion();
}
}

No comments: