Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-04-16
1
Medium Priority
?
647 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 32

Accepted Solution

by:
Jamie McAllister MVP earned 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Loops Section Overview
Suggested Courses

971 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