Sharepoint Updating / Changing Lookup Fields from code

unfortunately the only way to do it correctly is to change the SchemaXml itself for most changes like ListID or ColumnID ect.

here is a Sample:

public static void FixLookup(SPFieldLookup lookupField, Guid newListGuid)
   if (lookupField.LookupList != newListGuid.ToString())
     Log.Application.Info("update schema");
     int startIndexOfList = lookupField.SchemaXml.IndexOf("List");
     int startIndex = lookupField.SchemaXml.IndexOf("\"", startIndexOfList) + 1;
     int endIndex = lookupField.SchemaXml.IndexOf("\"", startIndex);
     int countIndex = endIndex - startIndex;
     string oldGuid = lookupField.SchemaXml.Substring(startIndex, countIndex);
     lookupField.SchemaXml = lookupField.SchemaXml.Replace(oldGuid, newListGuid.ToString());

   if (lookupField.AllowMultipleValues)
     /// there is a bug either in sp or more likely in the way genesis creates the lookup that without this fix its not working
     Log.Application.Info("toggle AllowMultipleValues");
     lookupField.AllowMultipleValues = false;
     lookupField.AllowMultipleValues = true;


FixLookup(spLookupField, spList.ID);


Popular posts from this blog

Asp.Net Ending Response options, Response.End() vs CompleteRequest()

c# Service Play Sound with NAudio example by Moshe

JS/JQ simulate Enter event