Wednesday, February 10, 2010

How to create Sharepoint Lookup Field

How to create Sharepoint Lookup Field

Creating Lookup Field on Sharepoint site can be done in several ways. Lets briefly describe them:
  1. Creating Sharepoint Lookup Field with web UI
  2. Creating Lookup Field programatically
  3. Creating Lookup Field through XML definition

First way is great and simle for end users. But for software developers who want to develop sharepoint solution in a development environment and then deploy it to the customer environment in the easiest way remains the last two ways. Only defining all Sharepoint features in XML should provide simple way to update Sharepoint site. The need of simple updating Sharepoint site is common in developer sphere.

In this article i will describe who to create Sharepoint Lookup Field throug XML definition of custom Content Type from Visual Studio with VSeWSS.

You should follow these steps:
  1. Create a Blank Site Definition
  2. Create source List for Sharepoint Lookup Field
  3. Create List with Lookup Field
  4. Correct Lookup Field at runtime
  5. Deploy site definition
  6. Create site from Site definition
  7. Test Lookup Field

1) Create a Blank Site Definition

- run Visual Studio with VSeWSS
- in Menu choose: File -> New -> Project
- in tree list navigate to the Sharepoint group and choose Blank Site Definition
- fill the Name and Location of project and click OK

Creating a Blank Site definition

2) Create source List for Sharepoint Lookup Field

- right click on Project in Solution Explorer
- in context menu choose: Add -> New Item
- in tree list navigate to the Sharepoint group and choose Content Type
- fill the Name of Content Type and click OK
- choose the Base content type: Item and click OK

creating source content type
- the next step is creating List Definition and List Instance
- right click on Project in Solution Explorer
- in context menu choose: Add -> New Item
- in tree list navigate to the Sharepoint group and choose List Definition from Content Type
- fill the Name of List Definition and click OK

creating source list definition
- choose the Base content type: SourceListCT
- check Create List Instance and click OK

creating source list definition

3) Create List with Lookup Field

- right click Project in Solution Explorer
- in context menu choose: Add -> New Item
- in tree list navigate to Sharepoint group and choose Content Type
- fill the Name of Content Type and click OK
- choose the Base content type: Item and click OK

creating lookup content type
- now you have to add custom field to xml definition of content type
- open SourceListCT.xml file to edit from Solution Explorer
- uncomment FieldRef and Field node which is creating automatically by Visual Studio
- change type of filed from Type="Text" to Type="Lookup"
- add attribute List and attribute ShowField as shown in picture
- your content type definition should look following:

lookup content type definition
- xml code for Lookup Content Type:










- when you have added lookup field to content type definition and saved the xml file the next step is creating List Definition and List Instance
- right click Project in Solution Explorer
- in context menu choose: Add -> New Item
- in tree list navigate to Sharepoint group and choose List Definition from Content Type
- fill the Name of List Definition and click OK

creating lookup list definition
- choose the Base content type: SourceListCT
- check Create List Instance and click OK

creating lookup list definition

4) Correct Lookup Field at runtime

- the attribute List of lookup field should contains the correct guid of SourceList, but the guid for sourceList are assigned in time of creation the list instance that is the reason why must be guid get from created list and set to lookup field at runtime
- open SiteProvisioning.cs code file from Solution Explorer
- to OnActivated method add CorrectLookupLists method call
- add general CorrectLookupLists method to SiteProvisioningClass
- your SiteProvisioning.cs file should look following:

site provisioning handler
- code in OnActivated method:

CorrectLookupList(_web, "SourceList instance", "LookupFieldList instance",
"LookupFieldListCTFirst Field", "11111111-2222-3333-4444-555555555555");

- method CorrectLookupList:

site provisioning handler
- code of CorrectLookupList method:

///
/// Corrects the List attribute of lookup field
///

/// Instance of current spweb
/// Name of source list for lookup field
/// Name of list with lookup field
/// Lookup field display name
/// Temporary guid assigned to List attribute
private void CorrectLookupList(SPWeb _web, string _sourceListName,
string _lookupFieldListName, string _lookupFieldName, string _tempGuid)
{
SPList _sourceList = _web.Lists[_sourceListName];
SPList _lookupFieldList = _web.Lists[_lookupFieldListName];
_lookupFieldList.Fields[_lookupFieldName].SchemaXml =
_lookupFieldList.Fields[_lookupFieldName].SchemaXml.Replace(_tempGuid,
_sourceList.ID.ToString());
_lookupFieldList.Update();
}

5) Deploy Site definition

- right click project in Solution Explorer and in menu choose Properties
- on Debug tab set Start action to Start browser with URL and define url address of sharepoint cetral administration site, in my case: http://localhost:28020/
- save project properties
- right click project again and choose: Deploy
- in status bar you should see: Deploy succeeded

6) Create site from Site definition

- go to your central administration and choose tab Application Management
- in group Sharepoint Site Management click Create site collection
- fill title of site and optionally description
- click on the development tab and choose BlankSiteDefinitionDemo
- fill administrator user name and click OK
- after while you should see status: Top-Level Site Successfully Created
- now click the new created site link
- when you made change to code of project you should also call from command prompt iisreset

Creating site from Site definition

7) Test Lookup Field

- click View all Site Content to show created lists

sharepoint site
- create some items in source list

item for lookup field
- when you click new in lookupfieldlist, you should see dropdown list with sourcelist items

Lookup Field

As you can see, Sharepoint Lookup Field is successfuly created and connection between source list and list with Lookup Field is correct.

Back to Top