Je suis désolé pour les francophones, mais je dois maintenant m'exprimer en anglais car j'ai mis le code sur
Codeplex
This feature is an extension of the one from Chris O'Brien (
SharePoint 2007 - create lookup fields as a feature), but offer you the ability to define one or more fields and the associated lists within one xml file.Moreover you'll can use these fields in one or more content type. When you'll activate the feature, everything will be build !
Let me explain a bit:
First, if you want to have a full explanation of managing lookup column dynamically, you can start with a look on the blog of Chris O'Brien :
http://sharepointnutsandbolts.blogspot.com/2007/04/creating-list-based-site-columns-as.html
For summarizing, a lookup field is like a choice field but the content comes from a list in the site collection. The main difficulty is that the link between the field and the list is a GUID coming from the
instance of the list. This means the
GUID is only known when the list has been created. So you can't fix the link in your site column definition.
ie:
is not possible because you don't know the list GUID. Well, of course you can cheat a bit if you define a list in your site and you retrieve the actual GUID of the instance. But what about moving a site from a test environment to a production environement ? Or from a site collection to another...? The GUID change each time !
The solution proposed by Chris is great. But you must still define a list before activating the feature. And because I'm a little bit lazy, I've tried to improve his solution to one more generic.
Let's suppose you want to have a lookup field which contain a list of keywords. These keywords come from a SharePoint list, so you can extend it as you want.
Create a new XML file and insert the following code inside. Of course you can have more than one field into this XML file, but I'll use only one for the demo.
As you can, this is a classic field definition. Except for one attribute:
List. This attribute has two words,
Keyword and
Mots-Clés. The first one is the internal name of the list template, the second one is the name of the list we'll see in the site. In our case, we gonna create a list named
Mots-Clés based on the template internal name
Keyword. For a standard Customlist, the internal name is
custlist (have a look on
12\TEMPLATE\FEATURES\CustomList\ListTemplates)
Why all of this?
Because when the feature will be activated, a program will search for a list named
Mots-Clés in the site collection. If this list doesn't exist, then the program creates it, based on the list template and the list name you define in the
List attribute.
Here is the Feature.xml file:
In this feature, adapt the event receiver with the name of your assembly. The LookupFields.xml is the file where I put all lookup columns definitions.
You can notice one particular property:
ColumnDefinitionPath. This is the path where the lookup columns definitions is located. This path will used in the code to retrieve the XML data.
I've also added a dependency, because the list template feature must be activated before activating this lookup feature.
The tips here is because you defined properly a field, with all the usual behaviors (DisplayName, Show where you want, Readonly...) and a GUID, you can use these fields inside a ContentType!
Good news, you have now the opportunity to have a content type with usual field types, PLUS one or more lookup type!
The code is available on
Codeplex: SharePoint 2007 - Create field lookup and associated list as a feature.
There is a folder named
Solution which contains the wsp
Now, you have to add the solution with
stsadm -o addsolution -filename lookupsolution.wsp
Then, go to
Central Admin / Operation /Solution Management and deploy the solution to the site(s) you want.
Then, go to your site settings for activating the collection features, first
TEST Keywords then
Site Column Lookup Initialization
The source code is in
_App_code/Receivers.
Hope this helps you.