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

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

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

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem