Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
642 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

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…
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 video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 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