Attaching Custom Content Types to a list with site definition or Web Template

7. February 2012 00:08 by Mahmoud Farhat in   //  Tags:   //   Comments (0)

Created site definition by using SharePoint 2010 and visual Studio which correctly creates all lists and includes pages instances in Pages Document Library, these pages use the correct custom page layouts which using the custom content types and therefore the page doesn’t work as expected.

Then I opened the Library Settings from Pages Document Library and I didn’t find my custom content Type attached to the Pages Document Library.

 

 

 

So how we can attach Custom Content Types to lists like Pages document library?

We can attach our custom content types by the following steps:

1- Create new Empty Element Item and then write the below  code with your content type ID in the Element.xml file 

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">   
<ContentTypeBinding ContentTypeId="0x010100899F73B9F6814EA8AED9876985F28B46" ListUrl="Pages" /> 
</Elements>

2- Add new feature to the Site Definition project, this feature just contains the new element that is already created with the above code.

3- Add a feature dependency in the onet.xml file

<!-- Binds custom content types to Pages library.-->
<Feature ID="260a85fd-e9db-4e1a-aece-7d1c7caa3196">
</Feature>

    4- Add each page in a module with the content type

<File Url="test.aspx" Type="GhostableInLibrary">
         <Property Name="Title" Value="Test" />
         <Property Name="ContentType" Value="Custom_ContentTypes_Publishing - CustomFive" />
         <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/Custo Page.aspx, $Resources:cmscore,PageLayout_WelcomeLinks_Title;" />
</File>

Finally you can deploy your package to the SharePoint and you will find your custom content types attached to the Pages Document Library.

Good luck and I hope this information is helpful to you and Please feel free to write your comment.