Solved

Failed to programmatically create a lookup field from Site Collection A to Site Collection B

Posted on 2014-04-16
1
633 Views
Last Modified: 2014-04-17
Hello,

I am using SharePoint 2013 Standard Edition.

I have created a site collection, say, ROOT, where I use a Feature to install several site columns and content types that use these columns. In addition, the Feature creates several lists and associates them with some of my content types.

The content types have a few lookup fields to lists created in that site collection and everything works great.

Also, I must programmatically create new Site Collections under my ROOT site collection (underneath location "SITES"), i.e. sites/SiteColA, sites/SiteColB, etc.

In every newly created Site Collection under location "sites", I must install some of the content types already installed on my ROOT site collection and associate them with two document libraries in the root web site of the newly created Site Collection.

In every new site collection, I am trying to create the necessary site columns and the associated content types. The problem is that the content types I create in each Site Collection have a few lookup fields that MUST point to certain lists that have been created in the ROOT site collection.

No matter what I try, the creation of the lookup field from Site Collection A to ROOT site collection ALWAYS fails with exception "Value does not fall within the expected range.".

Note: The site collections get created in DIFFERENT content databases, but they are on the same web application.

This is the code that I use to add a lookup field from a site collection to ANOTHER site collection:

public static SPFieldLookup CreateLookupField(this SPWeb web, string fldInternalName, string fieldDisplayName, string group, bool required, bool allowMultipleValues, SPList lookupList, string lookupInternalNameField, Guid lookupWebId)
        {
            // THIS IS THE LINE WHERE THE EXCEPTION IS RAISED !
            web.Fields.AddLookup(fldInternalName, lookupList.ID, lookupList.ParentWeb.ID, required);
            SPFieldLookup lookup = (SPFieldLookup)web.Fields[fldInternalName];
            if (fieldDisplayName != fldInternalName)
            {
                lookup.Title = fieldDisplayName;
            }
            lookup.AllowMultipleValues = allowMultipleValues;
            lookup.LookupField = lookupList.Fields.GetFieldByInternalName(lookupInternalNameField).Title;
            lookup.Group = group;
            lookup.Update(true);
            lookup.LookupWebId = lookupWebId;
            return lookup;
        }

Open in new window


Can anyone help ?
0
Comment
Question by:devshed
1 Comment
 
LVL 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
ID: 40006526
According to this blog they have to be in the same Content Database for this to work;

http://stefan-stanev-sharepoint-blog.blogspot.ch/2009/04/how-to-use-lookup-field-across.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Work Over Net is a new and very powerful collaboration product. With its new easy interface it is becoming very competitive to other similar products like webex and office interactive. WON 2010 have the standard business tools needed for multi-offic…
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question