Skip to main content
 
Go Search
Home
Categories
Bloggers
Cross-Web lookup columns
By: Raja Ayyapusetty | Posted: March 20, 2008 at 6:38 PM

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.


  Comments   Add Comment   Share It  
  Your Name:
  Your Email: **will not be displayed
  Comment Title:
* Comments:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
   
  
  
* Your Name:
* Your Email: **will not be displayed
* Recipient's Email:
* Subject:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
  
  
  
awesome
By: Kailash Dhondiyal | Posted: December 18, 2009 at 4:14 AM
Thanx for your code snippet i tried it and it works like a charm....
 

 About Raja Ayyapusetty

Senior ConsultantRaja Ayyapusetty is a senior consultant for PointBridge. He has over three years of experience in Microsoft .NET technologies with a primary focus in designing and developing 2003/2007 SharePoint solu... [more]

 Tag Cloud

 External Links

 ‭(Hidden)‬ Admin Links