Solved

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

Posted on 2014-04-16
1
628 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

When installing SharePoint 2010 RTM I came across a strange error, I was getting timeouts during the installation. I searched the web and found the best solution to be found here (http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010genera…
We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now