2

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.

Stu Pegg
  • 4,623
  • 7
  • 48
  • 91

2 Answers2

4

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();
James Love
  • 25,512
  • 2
  • 45
  • 77
  • 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. –  Feb 08 '11 at 20:15
  • This might help: http://blog.mastykarz.nl/sharepoint-programmatically-provisioning-lookup-fields/ – James Love Feb 08 '11 at 20:50
0

Take a look at this article:

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

Stu Pegg
  • 4,623
  • 7
  • 48
  • 91