SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any solid (proven) method of creating Site columns (lookups) where list GUID is not known?

I think this is where feature activation kicks in; getting the lookup list by its name and then using the GUID to create the lookup.

share|improve this question

Isn't too difficult really, in the FeatureActivated method, you just create a new SPieldLookup (using SPWeb.Fields.AddLookup() for the SPSite's RootWeb), and set the properties for the SPFieldLookup as required.

First time doing it? Bit tricky if you've never made a FeatureReceiver before, let me know if you need a tutorial do that, and here's some sample code that should get you started (this will create a field that matches to "Title" of "My List") [also, this needs to be a FeatureReceiver for a Site Collection -scoped Feature];

SPWeb oWeb = ((SPSite)Feature.Parent).OpenWeb();

SPList oList = oWeb.Lists["My List"];

oWeb.Fields.AddLookup("My Lookup Column", oList.Id, false);

SPFieldLookup lkp = (SPFieldLookup)oWeb.Fields["my Lookup Column"];

lkp.LookupField = "Title";

lkp.Update();
share|improve this answer
    
Thanks James. I have used featureactivation. These fields are for site collection and the lists I will be using are also on site collection. A good tutorial or link would really help. – user2360 Feb 8 '11 at 20:15
    
This might help: blog.mastykarz.nl/… – James Love Feb 8 '11 at 20:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.