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.


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";

  • 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

Take a look at this article:

Declaratively adding a lookup field to a list schema using the name of the referenced list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy