Becky Bertram's Blog
Perspectives from a SharePoint developer




This blog has moved. You can find the new blog on Savvy Technical Solution's Web site, at www.savtechsol.com. You will be redirected to the following page in 10 seconds:

Becky Bertram's Blog > Posts > Reattaching LookupList Property to a new List
March 04
Reattaching LookupList Property to a new List

As you might have discovered, the SPFieldLookup.LookupList property is Read Only, and cannot be set. I have a scenario where I'm trying to migrate a sub-site from one site collection to another, and the lookup list GUID will not be the same in the new site collection. I tried setting the LookupList property to the new GUID, but no dice. I got error. I knew there had to be a way around, and I found it. You have to do a string.Replace() on the SPField.SchemaXml, like so:

SPWeb web = [Whatever web your new list is in];
SPList newListToAttachTo = web.Lists["My New Lookup List"];
SPField myField = myList.Fields["My Field"];
string xml = myField.SchemaXml;
//Replace old LookupList ID with new one
xml = xml.Replace("c4ba59f4-b3e0-48f5-8f8c-6af7dd11b566", newListToAttachTo.ID.ToString());
//Replace old LookupWebId with new one
xml = xml.Replace("19c2dac4-c09f-4611-a0c6-1208ca641565", web.ID.ToString());
myField.SchemaXml = xml;
myField.Update();

Voila! Your lookup columns will now work. (If you need your code to be more dynamic, do a string search for the LookupList attribute and the LookupWebId attribute, and replace the GUID dynamically if necessary.)

As a side note, if you're using the stsadm -o export process to export a particular sub-web, if you take a look at the contents of the export file, you'll see an XML file called LookupListMap.xml. This is the holy grail for reattaching lookup list values to a different location in a new site. For every single lookup list item, there's a URL property you can set, so when the import runs, the lookup list property will point to that URL.

Comments

THANK YOU!

Thanks! Your post helped me a LOT! I lost the lookup list proper guid due to some bug in MOSS migration API and your post saved me a whole day of work!
 on 5/19/2009 9:45 AM

Runtime

In case anyone else needs this I've created a small command line utility that guides you through the process. All you need is to run it on the server that hosts the SharePoint site. Download available here: http://gomiunik.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=32667  Thanks again.
 on 9/8/2009 9:26 AM

Thanks!!!!

Just thank you so much for that post! :D
 on 9/14/2009 11:18 AM

Help!

Hello Becky,  I don't know if you can give a hand here...did you have this particular problem in sharepoint lookups and site columns? I'm having a problem acessing DIP panel in word documents, and i thouht this was the problem. can you give me a tip?  Thanks!
 on 10/1/2009 3:11 PM

Thanks

We had defined a lookup field with the list attribute containing the url of the list. This worked when we created a list definition and list instance of the content type (all in the same feature), but the field had issues in the content type and field galleries. This resulted for instance in the problem that we couldn't create web templates. We would get errors in ULS like:  SPSolutionExporter: System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).      at System.Guid..ctor(String g)      at Microsoft.SharePoint.SPSolutionExporter.GetFieldSchemaXml(SPField field, SPWeb web, Boolean isFieldFromWeb, Boolean removeSealedAttribute)      at Microsoft.SharePoint.SPSolutionExporter.ExportField(SPField field, SPWeb web)      at Microsoft.SharePoint.SPSolutionExporter.ExportFields(SPFieldCollection fields, String partitionName)  Or:  SPSolutionExporter: System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).      at System.Guid..ctor(String g)      at Microsoft.SharePoint.SPSolutionExporter.GetFieldSchemaXml(SPField field, SPWeb web, Boolean isFieldFromWeb, Boolean removeSealedAttribute)      at Microsoft.SharePoint.SPSolutionExporter.ExportField(SPField field, SPWeb web)      at Microsoft.SharePoint.SPSolutionExporter.ExportFields(SPFieldCollection fields, String partitionName)   Eventually we found out that when using this code (retrieving the eventual GUID of the list and replacing the List attribute in the schema XML as described here, resolved this issue.  So thanks!  (included the information so google might pick up on it for others to profit from this)
 on 6/23/2011 11:49 AM

Code does not commit guid

Good day Becky,

Thank you for posting this code.

I dont know why but for some reason the code does not commit the guid. I am running it as admin and have all the correct rights.

I can see the change but when I refresh it goes back to what it was.


Is there something I am missing?
 on 10/30/2012 9:03 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Comment Title *


Body *


Your Name


Your E-mail Address


Your Website

Type the Web address: (Click here to test)  

Type the description: 

Are you spamming my blog? *


This field is here in an attempt to stop spammers from entering comments. Enter a number, any number

Attachments