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.
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();