Sunday 17 February 2013

Create Custom Content Type with Lookup field using Elements.XML in .Net in SharePoint 2010



Below are the steps to create a custom Content Type using .Net & deploying the package in the SharePoint 2010 Server.
  1. Open Visual Studio & Select the Empty SharePoint Project.


1 2. After giving the Proper Name to the Project, the next screen appears asking to deploy the solution as Sandboxed or Farm level. Select Deploy as Farm Solution

3.    The Default Solution will look as shown below.

   4.  In the given solution we need to add a content type to create a custom content type. Right Click on the Solution to add a new content type as shown below.

    5.  Select Content Type & give it a proper name as shown below.

     6.  The popup to choose the base content type will appear as shown below

7.  Select “Item” as base content type from the drop down or any other content type according to the requirements as shown below.

    8.  Select “Item” as base content type from the drop down or any other content type according to the requirements as shown below.

     9.   Now in the given Elements.XML file we need to add the columns to bind them with our custom content type. Here we can add the default SharePoint columns by using their ID’s. But now in this case we will first create the custom Site Columns & then bind them with the Custom Content Type. As we are creating custom site column with Lookup field, to create the Lookup field we need to give the path of the list & the field from which the values are to be displayed in this lookup field.

     10.  There are two options to create the list. Either create the list using OOTB functionality & provide the given specific name to the list or create the custom list in the given custom solution & use its name & path to define the Lookup Field. If we choose to create the list with OOTB functionality then we need to create the list with the proper name before activating the feature or it will throw error. Hence we will go for option 2 & create the List Instance in the same solution to avoid errors. Also on activating the feature the list will be created automatically & this list will be used as lookup field to the site column.   

In the given solution we need to add a List Instance to create a custom List. Right Click on the Solution to add a new List Instance as shown in step 4.

      11.  Now select the List Instance & give the List Instance a proper name & click add as shown below.

    12.  Now the popup will appear where we need to give the display name to the list, Select the list type from drop down, here we will select Custom List, give the description to the list & finally the relative URL. Now click Finish as shown below.

    13.  Now we can define the site columns in the Elements.XML file of the Custom Content Type after the <ContentType></ContentType> tag but before the closing </Elements> tag as shown below.

To create the Lookup field below is the field tag
<!--Site Column of type "Lookup" which reads values from other list created in the same solution-->
<Field ID="{BF2FCD32-5A68-4E8C-9DB3-C785D8279DD4}"  Type="Lookup" Name="DemoLookupField" DisplayName="DemoLookupField"  Hidden="FALSE" Required="FALSE" Sealed="FALSE" Group="Custom Columns" List="Lists/LookupListInstance" StaticName="DemoLookupField" ShowField="Title" WebId="~sitecollection" Overwrite="TRUE" />

Here in the above Field, type of the field is set to “Lookup”. Now to show the items from other list we need to provide the path of the list & the field name whose values are to be shown in this field. Here we will provide the relative path of the List Instance creates as shown above i.e. List=”Lists.LookupListInstance” & provide the field name as ShowField=”Title”, the id of the current web i.e. WebId=”`sitecollection”.
We will also create some other fields. The code for the same is as follows
<!--Create Custom Site Columns -->
<!--Site Column of type "Text"-->
<Field ID="{2FA325B0-B884-4AAB-8E60-E9AA48AA97F3}" Type="Text" Name="DemoName" DisplayName="DemoName" Required="False" Sealed="TRUE" Group="Custom Columns"/>
<!--Site Column of type "Number"-->
<Field ID="{30C7137C-7320-4603-8F54-B8043C3AAF49}" Type="Number" Name="DemoValue" DisplayName="DemoValue" Required="False" Sealed="TRUE" Group="Custom Columns"/>
<!--Site Column of type "Choice for DropDown"-->
<Field ID="{38A52E05-59EB-4FA8-AEB4-5739AD6562F3}" Type="Choice" Name="DemoType"  DisplayName="DemoChoiceType" Sealed="TRUE" Group="Custom Columns">
<CHOICES>
       <CHOICE>Internal</CHOICE>
       <CHOICE>External</CHOICE>
</CHOICES>
</Field>
<!--Site Column of type "Currency"-->
<Field ID="{34814490-405E-4CC5-A200-1934266F59F8}" Type="Currency" Name="DemoAmount" DisplayName="DemoAmount" Decimals="2"Min="0" Required="FALSE" Group="Custom Columns" />
<!--Site Column of type "Date & Time" & contains today's date as default date-->
<Field ID="{19BFDCF6-2B4C-4666-9D8C-B933C0B1DF5D}" Type="DateTime" Name="DemoDate" DisplayName="DemoDate" Format="DateOnly" Required="FALSE" Group="Custom Columns">
<Default>[today]</Default>
</Field>

While creating the Site Column we have to give every field a unique ID. After copying &changing the ID’s of all the site columns, the solution will look as given below.

    14.  After creating the site columns we have to bind them in our custom content type. To achieve this we have to give the site column’s reference in the <FieldRef></FieldRef> which is present inside <ContentType></ContentType> tag. To add the reference of the fields we have to give the ID of the site column & the Name on the field defined in the Site Columns.
The code for the same is as follows.
<!--Create Field References to the Defined Content Type-->
<FieldRef ID="{2FA325B0-B884-4AAB-8E60-E9AA48AA97F3}" Name="DemoName" DisplayName="DemoName" Required="False"/>
<FieldRef ID="{30C7137C-7320-4603-8F54-B8043C3AAF49}" Name="DemoValue" DisplayName="DemoValue" Required="False"/>
<FieldRef ID="{38A52E05-59EB-4FA8-AEB4-5739AD6562F3}" Name="DemoType"  DisplayName="DemoChoiceType" />
<FieldRef ID="{34814490-405E-4CC5-A200-1934266F59F8}" Name="DemoAmount"  DisplayName="DemoAmount" />
<FieldRef ID="{19BFDCF6-2B4C-4666-9D8C-B933C0B1DF5D}" Name="DemoDate"  DisplayName="DemoDate" />
<FieldRef ID="{BF2FCD32-5A68-4E8C-9DB3-C785D8279DD4}" Name="DemoLookupField"  DisplayName="DemoLookupField" />

The FieldRef ID & the Name should match with the ID & Name of the site column defined below it. After adding the Field References the solution will look as follows.

     15.  Now the next step is to build the solution & deploy it in your SharePoint Web Application. After deploying the solution we can find these columns created in the Custom Columns Group in the Site Columns page as shown below
Also the Content type is created under the Custom Content Types Group as shown below. 

Also the default list will be created for the Lookup field site column.

1 comment: