Posted by: R Manimaran | October 21, 2010

The object has been updated by another user since it was last fetched.

The object has been updated by another user since it was last fetched.

In SharePoint I have created a Site Column and attach that site column using SharPoint Object model.

I have created a Separate method and I called it in Feature reaceiver’s FeatureActivated event.

My Method goes like this..

public void LinkFieldToContentType(SPWeb web, string contentType, SPField field)
{
try
{

SPContentType ct = web.ContentTypes[contentType];
ct.FieldLinks.Add(new SPFieldLink(field));
ct.Update(true);

}
catch (Exception ex)
{

Logger.WriteToLog(“Error Occurred while adding Content Type.”+ex.Message);
}

}

Here I am passing SPWeb from the FeaureActivated event’s SPFeatureReceiverProperties properties.

Passing the web creates an error “The object has been updated by another user since it was last fetched.” When the line ct.Update(true); get executed

I have modified the code something like this., which works. So it just want a new Instance of SPWeb

From FeatureActivated I call the below method like this..

AddtoContentType(properties, newColumnInternalName,DocumentContentType);

Here Properties is SPFeatureReceiverProperties properties

using (SPWeb web = (SPWeb) properties.Feature.Parent)

{

if (siteColName != string.Empty)

{

SPFieldLink oFieldLink = new SPFieldLink(web.Fields.GetFieldByInternalName(siteColName));

SPContentType oContentType = web.ContentTypes[contentTypeName];

//Check the Field is not available already

if (oContentType != null && !oContentType.Fields.ContainsField(oFieldLink.DisplayName))

{

oContentType.FieldLinks.Add(oFieldLink);

oContentType.Update(true);

}

}

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: