Archive

Archive for December, 2010

Creating Content Types and associating with SharePoint Custom List using CAML

December 30, 2010 2 comments

Lists and libraries can be defined either to use a set of columns or it can be defined to use a content type which was introduced in WSS 3.0.  The content type defines the columns for a list item or row and a list can be set to use one or several different content types [metadata of the content, if you need to have different fields for leave application form and different for appraisal form, and both are associated with the same list , ten content type creating will solve this myth ]. Content Types are defined outside the list and are consumed by list and One Content Type can be used by several lists.

Into this we would learn how to Create Custom Content Type which will inclule custom site colums, which will be wraped into feature using CAML.

Creating Custom Content Type:

Following are steps to create Custom content type in SharePoint.

  • This can be done using Object Model , but I prefer to create it using CAML.
  • Create Feature for Content Type.

Associate the site columns with list, as site columns of the custom content type would be used by the list with which we will bind the custom content type.

For making it much easier , I will use Alpha Content Type as Content Type Name.

Creating Feature for Content Type:
We need to have two xml files.
i. Feature.xml
ii. SiteColumns.xml

Feature.xml includes
<?xml
version=1.0
encoding=utf-8?>
<Feature
Id={2C7E86D0-64F5-4a2a-AA76-39F07EB5D937}
Title=AlphaContentType
Description=Alpha Content Type for demo

Version=12.0.0.0

Hidden=FALSE

Scope=Web
xmlns=http://schemas.microsoft.com/sharepoint/>
<ElementManifests>
<ElementManifest
Location=SiteColumns.xml/>
</ElementManifests>
</Feature>
SiteColumns.xml includes
<?xml
version=1.0
encoding=utf-8?>
<Elements
xmlns=http://schemas.microsoft.com/sharepoint/>
<Field
ID={CA65AE4B-8307-46fc-8705-9121093B8905}
Name=FirstField
DisplayName=FirstField
Type=Text
MaxLength=5
Description=Field for Alpha Content Type
Group=My Content Type/>
<ContentType
ID=0x01000322302384D740d0A691BA10E02FD540
Group=My Content Type
Name=Alpha Content Type>
<FieldRefs>

<FieldRef
ID={CA65AE4B-8307-46fc-8705-9121093B8905}
Name=FirstField/>
</FieldRefs>
</ContentType>
<ContentTypeBinding
ContentTypeId=0x01000322302384D740d0A691BA10E02FD540
ListUrl=Lists\CustomList />
</
Elements>

Into it Field is to be added which has to be site column, Following is description of the tags commonly used into SiteColumns.xml

Field ID=”GUID”
 

Name=Name of Field

DisplayName=Display Name of Field

Type=Type of Field , Lets take as Text

MaxLength=Length of Characters , lets take as 5

Description=Description of field

Group=Group Name under which this site colum will be created />

ContentType ID=Content Type + 00 + GUID
 

Group=Name of Group under which content type is to be created

Name=Name of Content Type>

FieldRef
ID=GUID of the field which is to be associated with the content type
Name=Name of Field/>

Associating Content Type with Custom List using CAML:
Mentioned above are details of creating feature for content types, there are few more steps to go on, Now its time to play with List which is required to be associated with “Alpha Content Type”.
I have created this list using CAML, and into its schema.xml we need to add some tags.

<
List
Title=ILGroups
Description=“”
Direction=0
BaseType=0
Url=Lists/ILGroups
EnableContentTypes=TRUE
DisableAttachments=TRUE
FolderCreation=FALSE
Version=23
Type=100
xmlns=http://schemas.microsoft.com/sharepoint>

EnableContentType We need to add this “EnableContentType” tag to allow management of content types.


After that there comes another part, and that’s linking our custom content type with required list.

Into schema.xml

<
View
DefaultView=TRUE
Type=HTML
DisplayName=All Items
Url=AllItems.aspx
Level=1
BaseViewID=1
ContentTypeID=0x
ImageUrl=/_layouts/images/generic.png
WebPartZoneID=Main>
<ViewFields>
.
.
.
<FieldRef
Name=FirstField/>
.
.
.
</ViewFields>
</View>|
<
Fields>
.
.
.
<Field
ID={CA65AE4B-8307-46fc-8705-9121093B8905}
Name=CT ILGroup
DisplayName=CT ILGroup
Type=Text
MaxLength=5
Description=IntraLinks Content Field For ILGroups
ShowInNewForm=TRUE>
</Field>
.
.
.
</
Fields>
<ContentTypes>
<ContentType
ID=0x01000322302384D740d0A691BA10E02FD540
Name=My Content Type
Group=Alpha Content Type
Description=Custom Content Type
FeatureId={2C7E86D0-64F5-4a2a-AA76-39F07EB5D937}>
<FieldRefs>
<FieldRef
ID={CA65AE4B-8307-46fc-8705-9121093B8905}
Name=FirstField/>
.
.
.
</
FieldRefs>
<XmlDocuments>
<XmlDocument
.
.
.
</XmlDocuments>
</ContentType>

View Add fieldref of field created in sitecolumns.xml to default view of list, so it can be visible in default view of custom list.
Fields <Field
ID=Same ID as in SiteColumns.xml, if GUID is different then it will be treated as saperate field other wise as a field taken from site columns and of custom content type
 


Name=Name of Field


DisplayName=Display Name of field

.

.

</Field>

The best way is to copy all the <Field> and its attributes from SiteColumn.xml

ContentTypes It inclueds content types associated / linked with list. More then one content type can be linked with list.
ContentType
  • ID=”Content Type ID which was mentioned in SiteColumns.xml”
  • Name=”It can be different than the title mentioned in feature.xml of content type, this tag represent the text appearing on clicking “New” from the list bar”
  • Group=”Same Group which is mentioned in SiteColumns.xml”
  • Description=”Description of content type”
  • FeatureId=”Feature ID of Content Type Feature.”

I am using WSP Builder to install the feature, it can also be installed using stsadm commands.
After activating Content Type feature , Content Type with fields would be associated with Custom List.

Advertisements
Categories: SharePoint