RSS
Add to Technorati Favorites
Follow me on Twitter


Becky Bertram's Blog > Posts > Reattaching LookupList Property to a new List
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!

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.

Thanks!!!!

Just thank you so much for that post! :D

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!
 

Comment Title *


Your Name


Body *


Your E-mail Address


Your Website

Type the Web address: (Click here to test)  

Type the description: 

Post Date *

Post Title *