Wednesday, February 10, 2010

How to create Filtered Lookup Field by Content Type

How to create Filtered Lookup Field by Content Type

Creating Filtered Lookup Field in Sharepoint is not directly supported.

In order to filter lookup field you must install custom field from some open source project or etc. In scenairo in which we wont to filter lookup field by content type it is not necessary. Is is possible to do it with basic lookup field and showfield attribute of this lookup field.

In this blog article i will show you on example how to create filtered lookup field by content type.

You should follow these steps:
  1. Create a Blank Site Definition
  2. Create source List for Lookup Field based on two or more Content Types
  3. Create List with Filtered Lookup Fields
  4. Correct Lookup Fields at runtime
  5. Deploy Site definition
  6. Create site from Site definition
  7. Test Filtered Lookup Fields

1) Create a Blank Site Definition

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

creating blank site definition

2) Create source List for Lookup Field based on two or more Content Types

- 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

creating first content type
- to create second content type again click to new item
- fill the name of second content type and click ok
- for both choose the Base content type: Item and click OK

creating second content type
- custom content types edit as shown on picture
- add reference to title node in order to hide it with attribute hidden
- uncomment field prepared by visual studio and change its required flag to true

first content type definition
- xml code of first content type:








- do the same with second Content Type

second content type definition
- xml code of second content type:









- the next step is creating List Definition and List Instance from two content types
- right click Project in Solution Explorer
- in context menu choose: Add -> New Item
- in tree list navigate to Sharepoint group and choose List Definition notice that in this scenairo we dont use option List definition from content type, we are adding content types to list definition manually
- fill the Name of List Definition and click OK

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

creating source list definition
- next step is to associate two content types with list manually
- locate and open for edit file schema.xml of created list in solution explorer
- clear subnodes of ContentTypes node and Fields node
- add attribute EnableContentTypes to List node
- add two ContentTypeRef nodes under contentTypes node as shown on picture
- notice that id of contenttypere is based on content type id from its xml definition
- under the node Fields copy all nodes that are in custom content types

source list definition schema
- xml code of source list definition in schema.xml file:











3) Create List with Filtered Lookup Fields

- 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 filtered lookup field 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
- copy FieldRef and Field node, change guids of second field and rename it
- change type of fieds from Type="Text" to Type="Lookup"
- add attribute List and attribute ShowField as shown in picture
- your content type definition should look following:

lookup field content type definition
- xml code of Lookup Field list Content Type:











- when you have added lookup fields 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 field list definition
- choose the Base content type: SourceListCT
- check Create List Instance and click OK

creating lookup field list definition

4) Correct Lookup Fields 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");
CorrectLookupList(_web, "SourceList instance", "LookupFieldList instance",
"LookupFieldListCTSecond Field", "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee");

- method CorrectLookupList:

site provisioning handler
- code of this method to copy and paste:

///
/// 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:18060/
- 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 Filtered Lookup fields

- click View all Site Content to show created lists

sharepoint site
- create some items in source list from diferrent content types

creating items for filtered lookup field
- sourcelist main view should be alterned to display fields of content types

adjusting list view
- created items to demonstrate Filtered Lookup Field

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

Filtered Lookup Field
Filtered Lookup Field is successfuly created. Field is filtered by Content Type.

Back to Top