PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

How to bind a CustomContentType to a ListInstance Feature

Posted by PANVEGA on January 19, 2009

Groups of site columns are then organized into Content Types and associated with other lists and functionality throughout SharePoint. Additionally they offer a great deal of flexibility such as the ability to modify content types (add & removing site columns) after creation and attaching additional functionality such as a workflow to.

With all of the advanced functionality they provide in many complex scenarios such as publishing its often overlooked that they can also be used for everyday needs such as defining a simple list with custom fields. The standard declarative approach for this is to create a feature that defines your columns within the schema.xml and a subsequent ListTemplate and/or ListInstance element to register this new list with SharePoint and optionally create an instance of that list. The functionality is very powerful but at the same time can get quite complex as modifying the schema.xml can often be cryptic and error prone.
A simple alternative approach is to define site columns or even reuse out of the box or existing columns, define your content type, and bind that content type to an existing list such as a generic list. Its also possible to take existing content types, such as those use for tasks, and inherit from that content type to add your own site columns and bind that to a defined task list.

In my previous post I already explained how to create a Custom ListInstance within a feature. Read these two posts before you go on.

https://panvega.wordpress.com/2009/01/14/how-to-create-a-custom-content-type-with-a-feature

https://panvega.wordpress.com/2009/01/18/how-to-create-a-new-list-item-instance-with-a-feature

However usually you wanna append your Custom Content Type to your new created List when deploying the feature.

First create a feature.xml, which I expleined in a previous post. In the next stepcreate a ListInstace.xmlxml file for your List feature.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/” Id=”A4E3B5AB-857C-42d1-961D-4B2A6A2D43A7″
Title=”ListInstance”
Description=”The feature create a new Custom List Instance”
Hidden=”FALSE”   Scope=”Web”  Version=”1.0.0.0″>
<ElementManifests>
<ElementManifest Location=”ListInstance.xml” />
</ElementManifests>
</Feature>

Here I created a Custom Content Type called Banner Type with a List Item (0×01):

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<ContentType ID=”0×0100A3B0A8BC17A149e8BEB2EE9E30F993DC”
Name=”Banner Type”
Group=”Banner Types”
Description=”Banner Content Type.”
Version=”1″ >
<FieldRefs>
<FieldRef ID=”{fa564e0f-0c70-4ab9-b863-0177e6ddd247}” Name=”Title” Required=”TRUE”/>
<FieldRef ID=”{CA03227B-77AE-4617-BDFA-FB91299AF318}” Name=”BannerLink” Required=”TRUE” />
<FieldRef ID=”{FCC58C53-9EDF-45c7-97CF-CF336D670EAA}” Name=”BannerNotes” Required=”TRUE” />
</FieldRefs>
</ContentType>
</Elements>

In the next step I created a Custom ListInstance. A list instance is a list created by using a list template. When creating a list this way, you can override some of the list properties set in the template, and you can include data rows that will also be created as list items in the list. In this example we gonna create a Custom List Instance called BannerList.

ListInstance.xml without ContentType binding

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<ListInstance
Id=”2242AE7D-D7B8-4dd5-A03B-F1B3AC8FBEA8″
FeatureId=”00bfea71-de22-43b2-a848-c05709900100
TemplateType=”100″
Title=”BannerList”
Url=”Lists/BannerList”  OnQuickLaunch=”true”
Description=”Contains all Items for the banner function”/>
</Elements>

In order to apped your new Custom Content Type to the List Instance, simple add the following atrribute to your ListInstance.xml file

<ContentTypeBinding ContentTypeId=”0×0100A3B0A8BC17A149e8BEB2EE9E30F993DC” ListUrl=”Lists/BannerList”/>

Note: The ContentTypeId must be the same ID than in your defind ContentType (0×0100A3B0A8BC17A149e8BEB2EE9E30F993DC) and take care for your ListURL.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<ListInstance
Id=”2242AE7D-D7B8-4dd5-A03B-F1B3AC8FBEA8″
FeatureId=”00bfea71-de22-43b2-a848-c05709900100″
TemplateType=”100″
Title=”BannerList”
Url=”Lists/BannerList”
OnQuickLaunch=”True”
Description=”Contains all Items for the banner function”/>
<ContentTypeBinding ContentTypeId=”0x0100A3B0A8BC17A149e8BEB2EE9E30F993DC” ListUrl=”Lists/BannerList”/>
</Elements>

As you can see its pretty straight forward. Unfortunately it doesn’t come without a couple gotchas. First off the default view of your list does not contain those custom columns. When you select the item you will of course see all of your columns but they aren’t listed in the default view. Secondly the default content type for the list is still available to the user. In the case of a generic list the “Item” content type is still present. So when a user enters a new item they are able to select Item or your custom content type.

This may not be desirable. Also if you are using the API to insert a new item into the list it will use the list’s default content type instead of the one you may be expecting. Once again this may not be desirable.

To overcome these restrictions we’re going to create a generic feature receiver that can be reused on any implementation using this technique.

Read more about https://panvega.wordpress.com/2009/01/19/how-to-add-a-spfeaturereceiver-to-a-custom-listinstance

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: