Wednesday, December 22, 2010

The object has been updated by another user since it was last fetched" when programmatically updating a child content type

We ran into the same issue and spent hours trying to resolve the error, and finally found a solution so I thought it would be helpful to someone else.

The scenario is a little different and actually simpler that what's described above by Djamel Chagour. We are adding a SPFieldLink to an existing SPContentType, the document library content type. We require a special column to be added to all document libraries and children of document libaries. Earlier we had tried the standard code snippet as shown below:

SPFieldLink fieldlink = new SPFieldLink(field);

libContentType.FieldLinks.Add(fieldlink);

libContentType.Update(true, false);

thisweb.Update();

- surrounded within using statements for SPweb and SPSite ofcourse. This was giving the error:

Microsoft.SharePoint.SPException: The object has been updated by another user since it was last fetched. at Microsoft.SharePoint.SPContentType.UpdateOnWeb(Boolean bPushdown, Boolean bThrowOnSealedOrReadOnly) at Microsoft.SharePoint.SPContentType.Update(Boolean updateChildren, Boolean throwOnSealedOrReadOnly) at EnterpriseTaxonomySharePointFeature.FeatureReceiver.FeatureActivated(SPFeatureReceiverProperties properties)

We changed the code to the following:

SPFieldLink fieldlink = new SPFieldLink(field);
libContentType.FieldLinks.Add(fieldlink);


//update parent


libContentType.Update(false, false);
thisweb.Update();
UpdateChildren(osite, libContentType);






private void UpdateChildren(SPSite site, SPContentType type)
{
using (SPWeb web = site.RootWeb)
{


//update children


Type.Update(true, false);
web.Update();
}
}

This prevents the SPException error. Hope this helps anyone else looking for a soultion.

No comments:

Post a Comment