Re-attach a SharePoint list lookup column to the source list. RRS feed

  • Question

  • When I create a site using a template that I created, a lookup column in a list no longer reference the source list.  The data source, i.e. "Get data from" field, for the lookup column is blank.

    How do I re-attach the lookup column to the source list, or perhaps even a new source list?  Things worth noting:

    • On the server where the template was created, the source list for the lookup column resides in the parent site of the site I am trying to duplicate.  I do not know how the original developer linked the lookup column to the list.  It is my undertstanding that a lookup column must use a list at the same site level.
    • I copied the source list to the site where the list with the lookup column resides.
    • No matter what I try, there does not seem to be an option to re-connect the data source (aka "Get data from" in the column edit page).

    In my experience, redfining a data source for a lookup column (aka drop-down box) is a simple thing to do.  I cannot find a way to do this in SharePoint, not even in Designer.

    Thursday, June 10, 2010 9:17 PM

All replies

  • It is my undertstanding that a lookup column must use a list at the same site level.

    this is normally the case, at least this is the only option possible with the SharePoint UI, but programmatically you can specify a lookup list in another site of the same site collection. this can be achieved using the SPFieldLookup class (the field class of the lookup field type) and its properties: LookupList and LookupWebId (in the former you specify the ID of the lookup list and in the latter - the ID of the web containing the lookup list)

    but there is a catch here - once these two properties are set for a lookup field you cannot change them (the property setters raise an exception)

    still there is a work-around to that - you can modify the SchemaXml property of the SPFieldLookup instance - this property contains the schema XML of the lookup field. a sample lookup field schema would look like:

    <Field Type="Lookup" DisplayName="look1" Required="FALSE" List="{2e577a47-eae2-431c-a142-b34386f53882}" WebId="936fac38-baed-4bc5-8525-178d5670413d" ShowField="Title" UnlimitedLengthInDocumentLibrary="FALSE" ID="{05ea70c7-24b4-47ce-992a-4f3e03b9e6dc}" SourceID="{8de2bd36-e998-4a53-9351-c496f4d361a6}" StaticName="look1" Name="look1" ColName="int3" RowOrdinal="0" />

    what you basically need to do here is to change the values of the List and WebId attributes of the field schema setting the correct lookup list ID and the ID of its parent web - this way you will be able to fix your lookup field.

    Thursday, June 10, 2010 9:56 PM
  • I'm looking for a much simpler answer than that.  I was hoping there was a work around in either the SP interface itself or a tag property I could alter in Designer.  I do not know where to find the "SchemaXml property of the SPFieldLookup instance".
    Monday, June 14, 2010 1:33 PM
  • No, you can't update the SchemaXml property with the lookup field using SharePoint designer, but you can use a custom tool for that - e.g. the SharePoint Manager 2007 utility -

    In the tool you need to expand the object model hierarchy till you locate your field and then in the property grid in the right pane you can modify the SchemaXml property (then just click to another property item in the property grid so that the property change gets applied).

    Note that using the same tool you can also check the ID-s of the lookup list and its containing web so that you know how to set these in the SchemaXml property.

    Also - save the old schema to a file before changing it, you can easily break the field and the list if you change something wrong.

    Monday, June 14, 2010 3:13 PM
  • How do I save the schema to a file?
    Monday, June 14, 2010 4:00 PM
  • simply select the value from the property grid, then copy it to the clipboard (ctrl + C) and then paste the value to notepad.
    Monday, June 14, 2010 5:07 PM
  • You mean the individual property then, not a full XML schema file, correct?

    Tuesday, June 15, 2010 4:23 AM
  • yes, only the SchemaXml proprety - you just copy its original value, then change the schema with the new attribute values and then check if your list and lookup field work properly - if everything is OK you don't need to do anything else, but if it doesn't, you paste back the original value of the SchemaXml property in the property grid.
    Tuesday, June 15, 2010 7:53 AM
  • Using the tool you recommended, when trying to change the LookupList property, I consistently get the error "Cannot change the lookup list of the lookup field."

    I have a screen shot but do not see an option to post an image here.


    Tuesday, June 15, 2010 5:36 PM
  • Hello,

    you cannot change the LookupList and LookupWebId properties of the SPFieldLookup once they are set

    but as I mentioned you can change the SchemaXml property which contains the XML of the field's schema - this is the only way you can fix the lookup field.

    Thursday, June 24, 2010 2:59 PM
  • Once I have created a list using a template(which has schema.xml), than after having its data in it, How can I change the schema.xml for a created list?
    Wednesday, January 5, 2011 1:37 AM
  • This is way old, but I'm updating with my solution as there have been no code examples thus far. This is in my featureactivated event. I first check if the lookup still matches the intended lookup list, and if not I grab and update the SchemaXml for the field:

                        // Re-bind the lookup column if necessary
                        SPFieldLookup lookUp = (SPFieldLookup)taskList.Fields.GetField("RI Item");
                        // if the lookup column is looking for a list that no longer exists
                        if (!lookUp.LookupList.Equals(riList.ID))
                            // Re-bind it by updating the schema xml of the column
                            XmlDocument xml = new XmlDocument();
                            XmlAttribute attr = xml.CreateAttribute("List");
                            attr.Value = riList.ID.ToString("B");
                            lookUp.SchemaXml = xml.InnerXml;

    Wednesday, June 26, 2013 7:43 PM