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.LookupWebId = lookupWebId;
Can anyone help ?