Field * was not found when updateing SchemaXML in an SPField

Posted on Updated on

 

I was trying to update a schemaxml to take the label or name of the field from a resource file programatically.

I had done this in the past with fields via XML, but not by code.

The code I was trying to execute is this one.

As you can see the managerField2.Update is commented, and that’s the line where I was having the exception.  Field was not found.

The problem is that when you modify the SchemaXml property Sharepoint triggers a database update and you don’t need to do an update again.

This problem was detected in 2005, is it a bug? I don’t know, in my opinion updates to the database should be only when you call the update method.

I found the solution here.

http://support.microsoft.com/kb/555293

string managerFieldName = currentweb.Fields.Add(SponsoringCommon.Constants.FIELDS_SPONSORINGMANAGER_NAME, SPFieldType.User, false);
SPFieldUser managerField = currentweb.Fields.GetFieldByInternalName(managerFieldName) as SPFieldUser;
managerField.Group = SponsoringCommon.Constants.DEFAULT_TAXONOMY_TERMGROUP_NATIONALELOTERIJSPONSORING;
managerField.Update();
currentweb.Update();

//Add new resource label to SponsoringManager field
SPField managerfield2 = currentweb.Fields[managerField.Id];
string schemaXmlWithResourceTokens = managerfield2.SchemaXmlWithResourceTokens;
int startIndex = schemaXmlWithResourceTokens.IndexOf("\"", schemaXmlWithResourceTokens.IndexOf("DisplayName=\"")) + 1;
int endIndex = schemaXmlWithResourceTokens.IndexOf("\"", startIndex);
int substringLength = endIndex - startIndex;
string value = schemaXmlWithResourceTokens.Substring(startIndex, substringLength);
schemaXmlWithResourceTokens = schemaXmlWithResourceTokens.Replace(value, "$Resources:SPNLSponsoring,Field_SponsoringManager_Name");
managerfield2.SchemaXml = schemaXmlWithResourceTokens;
//managerfield2.Update();
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

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