Friday 17 August 2012

Site Columns & Content Types


You want to Create Two Lists one that feeds off the other, but you're not sure where to start, well you start with site columns & Content types then you move onto List definitions and Instances.

Let’s get started, create a new SharePoint 2010 project and name it Lookup.Schema inside a solution called Lookup.Example, this is where we are going to put the site columns, and the content types for our two lists. Create two folders:

  • Content Types
  • Site Columns

Once that’s complete inside the SiteColumns folder create an Empty Element called SiteColumns and create the following site columns
  •  DepartmentID – Text
  • Department – Text
  • JobPostingName – Text
  • StartDate –DateTime
  • EndDate –DateTime
  • Department – Lookup

The XML for the Site Columns:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field ID ="{B23D01D7-5062-4CBC-8336-2FF4E1731ABE}"
       Name="SPJ_DepartmentName"
       DisplayName ="Department"
       Type="Text"
       Required="TRUE"
       Group="SPJ Columns" />
  <Field ID ="{FE0E46B4-ECC0-4CAB-84AC-7D835B486834}"
       Name="SPJ_DepartmentID"
       DisplayName ="Department ID"
       Type="Text"
       Required="TRUE"
       Group="SPJ Columns" />
  <Field ID ="{07D51A7C-1125-412D-8999-D4A6F8F6F5BA}"
       Name="SPJ_JobPostingName"
       DisplayName ="Job Posting"
       Type="Text"
       Required="TRUE"
       Group="SPJ Columns" />
  <Field ID ="{E7A84BA3-1F9A-43AE-B5A6-07A3F1A726D8}"
       Name="SPJ_StartDate"
       DisplayName ="Start Date"
       Type="DateTime"
       Required="TRUE"
       Format="DateTime"
       Group="SPJ Columns" />
  <Field ID ="{9861E969-637A-49F3-8354-A1D0BB65B14D}"
       Name="SPJ_EndDate"
       DisplayName ="End Date"
       Type="DateTime"
       Required="TRUE"
       Format="DateTime"
       Group="SPJ Columns" />
</Elements>

Now what you might notice is that I’ve listed 6 columns that you need to make but there’s only definitions for five of them. As you will see later on, a lookup site column needs a list instance to feed from, so we’ll set that up once we have our provider list instance ready.

Now that we have our site columns ready to go we need to create Content type for our list definitions. I want you to create to Content types that inherit from Item in the Content Type folder:
  • Department
  • JobPosting
When you see the following screen make sure to select item from the drop down before you click Finish.


With your two content types created your solution explorer should resemble the following:

The Site column xml should be contained within the site columns empty element which is inside the Site Columns folder as for the department and JobPosting content types, they should be inside the Content Types folder. This may seem like overkill for a small demo project like this and it is, but it’s a great habit to get into. SharePoint Projects tend to grow at an exponential rate and due to the brittle nature of this beast it is very much worth wile to be organized from day one, rather than trying to come back later and organize things.

So let’s open up the Department Content type and add the site column detentions we are going to use:
  • Department ID
  • Department Name
The XML is as follows:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Item (0x01) -->
  <ContentType ID="0x010010495f58ea83494d8b3ece02c8ab1cf0"
               Name="Department"
               Group="SPJ Content Types"
               Description="Content type to describe a Department"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID ="{FE0E46B4-ECC0-4CAB-84AC-7D835B486834}" Name="SPJ_DepartmentID"/>
      <FieldRef ID ="{B23D01D7-5062-4CBC-8336-2FF4E1731ABE}" Name="SPJ_DepartmentName"/>
    </FieldRefs>
  </ContentType>
</Elements>

With the Department Content Type complete let's make the Job Posting One:

  • Job Posting Name
  • Start Date
  • End Date

The XML is as Follows

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Item (0x01) -->
  <ContentType ID="0x010068497622be804676bf83a14bbdf3dc67"
               Name="JobPosting"
               Group="SPJ Content Types"
               Description="Content Type to describe a job posting"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID ="{07D51A7C-1125-412D-8999-D4A6F8F6F5BA}" Name="SPJ_JobPostingName" />
      <FieldRef ID ="{E7A84BA3-1F9A-43AE-B5A6-07A3F1A726D8}" Name="SPJ_StartDate" />
      <FieldRef ID ="{9861E969-637A-49F3-8354-A1D0BB65B14D}" Name="SPJ_EndDate" />
    </FieldRefs>
  </ContentType>
</Elements>
With the Content types Complete lets add the finishing touches by expanding the Features folder in the solution explorer, rename Feature1 to Lookup.Schema.
With "Feature 1" renamed to "Lookup.Schema" double click on "Lookup.Schema.feature" to display the actual feature manifest. a feature manifest is a list of things that this feature is going to deploy to the site and activate. With your feature manifest open rename the title to Lookup.Schema WebScope and give it a description, also make sure that is in fact scoped for the web.

With all that complete deploy your solution to add the site columns and content types to your site.