Skip to main content

Cloud

Cross-Web lookup columns

If you add a lookup column to a list/library from UI, it allows you only to pull information from a list within the same Web. However, if you need a lookup column to pull from a list in any web in the same site collection, you can do so through code. AddFieldAsXml method of SPFieldCollection, as shown below, does the trick for us:

string fieldXML = "<Field DisplayName=’MyLookUpColumn’ Type=’Lookup’ Required=’FALSE’ List=’" + listThatHoldsLookupData.ID + "’ WebId=’" + webThatHoldsLookUpDataList.ID + "’ Name=’MylookUpColumn’ ShowField=’" + Internal Name of data source column + "’ />";
list.Fields.AddFieldAsXml(fieldXML, true, SPAddFieldOptions.AddFieldToDefaultView);

After adding cross-web lookup column as above, you can change the source column from UI but not the source List. Make sure that you are providing the internal name of the data source column for ShowField attribute.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Raja Ayyapusetty

Raja is a software consultant with over 9 years of experience in designing, developing and testing complex solutions using Microsoft technologies. Throughout his career he has worked primarily on SharePoint and .NET technologies. Raja has worked with SharePoint since the 2003 version and has deep understanding of its features and capabilities. Raja is also a certified Sitecore developer.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram