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)
{
   Log.Application.Info("FixLookup");
   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());
     lookupField.Update(true);
   }

   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.Update(true);
     lookupField.AllowMultipleValues = true;
     lookupField.Update(true);
  }
}


Usage

FixLookup(spLookupField, spList.ID);

Comments

Popular posts from this blog

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

c# Service Play Sound with NAudio example by Moshe

MatDatePicker Errors