Navigation Bar

Wednesday 18 March 2015

Deploy Content Type with Lookup field


In this post I am going to show how we can deploy content type declaratively (through xml) with fields using known GUIDs.

One of the challenge with the look up field is that it binds to Lookup list via ListId (i.e. GUID). There are couple of ways you can achieve this. One is to use SharePoint Object Model to create fields and content types via Feature code.

First you need to create the lookup list via site scope feature. You might be thinking why I am creating a list in site scope feature? The reason is simple I just want to create the lookup list in root web of the site collection and that will be accessible to site columns.

Here is the project structure shown in image:




The LookupListProvisioner feature receiver will create a list that can be used as look up field. Here is the code:



Next, you need to create site column using SiteColumnsProvisioner feature receiver using below code:



As you can see in the code, we are using existing field GUID that can be used in Content Type. In ContentTypeProvisioner feature, I have used Elements.xml file to create the content type like shown below:



As you can see in above code, I have used pre-defined ContentTypeId and Field Guid. I have written a tool, that generates xml for fields and content types from SharePoint environment, can be found at http://sharepointxml.codeplex.com/
That’s it for now. Happy coding J