Solved

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

Posted on 2014-04-16
1
621 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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There is one common problem that all we SharePoint developers share: custom solution deployment. This topic can't be covered fully in this short article, so all I want to do in this one is to review it from a development-to-operations perspectiv…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

14 Experts available now in Live!

Get 1:1 Help Now