RSS

Programming Lists using CAML in SharePoint 2010. Create List based on a Content Type.


Published by Eugene Martynov
Publish date: 11/4/2011 7:25:54 AM

This article describes how to programming lists using CAML in SharePoint 2010. Examples were also taken from PluralSight site.

Creating List based on a Content Type using CAML:

1.    Use the SharePoint project from previous article and add new List Definition from Content Type.

2.    Choose the display name and select the Author content type (from previous article).

3.    Open the Elements.xml from the List Definition and change the following fields:
    -    Type
    -    Add the DisallowContentTypes="FALSE" item to allow users manage it
    -    Description

4.    Open the Elements.xml from the List Instance and change the following fields:
    -    Title
    -    TemplateType should the same as Type in the List Definition file
    -    URL
    -    Description

5.    Open the Schema.xml file and delete the FieldRefs section.

6.    Change ContentType element to  ContentTypeRef.

7.    Delete all unnecessary attributes from Fields section (see the code below). Use IDs from the Content type fields. Title fields are taken IDs from base Content type

8.    Change the default view. Use ViewFields section.

9.    In the top of the Schema.xml file inside List element add EnableContentTypes = “TRUE” attribute.

List Definition file:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">    
    <ListTemplate
        Name="AuthorsCTListDef"
        Type="10020"
        BaseType="0"
        DisallowContentTypes="FALSE"
        OnQuickLaunch="TRUE"
        SecurityBits="11"
        Sequence="410"
        DisplayName="Authors"
        Description="Authors List Template"
        Image="/_layouts/images/itgen.png"/>
</Elements>

List Instance file:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListInstance Title="Authors"
                OnQuickLaunch="TRUE"
                TemplateType="10020"
                Url="Lists/Authors"
                Description="Authors List Instance">
  </ListInstance>
</Elements>

Schema.xml file:

<?xml version="1.0" encoding="utf-8"?>
<List xmlns:ows="Microsoft SharePoint" EnableContentTypes="TRUE" Title="Authors" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/SharePointProject1-AuthorsCTListDef" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">
  <MetaData>
    <ContentTypes>
      <ContentTypeRef ID="0x0100FA10CAABDE214D22AC081096EA118DDA">        
      </ContentTypeRef>
    </ContentTypes>
    <Fields>
      <Field ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" DisplayName="Name" Name="Title" Type="Text"/>
      <Field ID="{82642ec8-ef9b-478f-acf9-31f7d45fbc31}" DisplayName="Name" Name="LinkTitle" Type="Computed"/>
      <Field ID="{4bf9541b-d24b-40e8-beda-45f747649c22}" DisplayName="Bio" Name="Bio" Type="Note"/>
      <Field ID="{c90086ca-5382-444c-9269-434e48fc132d}" DisplayName="Employee" Name="Employee" Type="Boolean"/>
      <Field ID="{2cef6fde-b198-4761-9d0b-2880e928fb96}" DisplayName="Salary/Rate" Name="SalaryRate" Type="Currency"/>
    </Fields>
    <Views>
      <View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE">
        ...
      </View>
      <View BaseViewID="1" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/generic.png" Url="AllItems.aspx">
        ...
        <ViewFields>
          <FieldRef Name="Attachments">
          </FieldRef>
          <FieldRef Name="LinkTitle">
          </FieldRef>
          <FieldRef Name="Employee"/>
          <FieldRef Name="SalaryRate"/>
        </ViewFields>
        ...
      </View>
    </Views>
    <Forms>
      ...
    </Forms>
  </MetaData>
</List>


This is the last article from “Programming Lists using CAML” series.

Programming Lists using CAML in SharePoint 2010. Create List.

Programming Lists using CAML in SharePoint 2010. Create Content Type.


Tags: sharepoint 2010

Back to Home
blog comments powered by Disqus

Copyright © 2011 Eugene Martynov, CSS Templates by Inf Design