RSS

Programming Lists using CAML in SharePoint 2010. Create List.


Published by Eugene Martynov
Publish date: 11/4/2011 6:29:57 AM

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

Let’s start from simple and create a list:

1.    Create new SharePoint project and add new List Definition (not Instance).

2.    Input the display name: Authors and select the Custom List type. Enable checkbox for Add a list instance.

3.    Open Elements.xml file for the list definition and change the following values:
    -    Type
    -    Description

4.    Open Elements.xml file for the list instance and change the following values:
    -    TemplateType – the same as Type in the definition file
    -    URL
    -    Description
    -    Title

5.    Open Schema.xml and find Fields section. Code can be taken from a server explorer if a SharePoint site already has a prototype of the list. Field ID values should be changed (Generate GUID tool can be used)

6.    Update the default view in Schema.xml. Use ViewFields section.

7.    Delete the ContentTypes section because the scheme is defined in the step 5.

8.    Add default values in Elements.xml file for the list instance.

List Definition file:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">    
    <ListTemplate
        Name="AuthorsListDef"
        Type="10010"
        BaseType="0"
        OnQuickLaunch="TRUE"
        SecurityBits="11"
        Sequence="410"
        DisplayName="AuthorsListDef"
        Description="Authors List Definition"
        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="10010"
                Url="Lists/Authors"
                Description="Authors List">
    <Data>
      <Rows>
        <Row>
          <Field Name="ID">1</Field>
          <Field Name="Title">Record1</Field>
          <Field Name="Employee">True</Field>
          <Field Name="SalaryRate">10</Field>
        </Row>
        <Row>
          ...
        </Row>
        <Row>
          ...
        </Row>
        <Row>
          ...
        </Row>
      </Rows>
    </Data>
  </ListInstance>
</Elements>

Schema.xml file:

<?xml version="1.0" encoding="utf-8"?>
<List xmlns:ows="Microsoft SharePoint" Title="AuthorsListDef" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/SharePointProject1-AuthorsListDef" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">
  <MetaData>
    <Fields>
      <Field DisplayName="Name" Type="Text" Required="TRUE" ID="{C62AC1F4-B1C8-4c6b-B93A-C883613B76DE}" Name="Title"/>
      <Field DisplayName="Name" Type="Boolean" Required="TRUE" ID="{764E754D-574E-4be0-9CFF-00BCA762B837}" Name="LinkTitle"/>
      <Field DisplayName="Employee" Type="Boolean" Required="FALSE" ID="{7B1AC2EF-2E97-403f-B9A2-F36136DB5EE6}" Name="Employee"/>
      <Field DisplayName="Salary/Rate" Type="Currency" Required="FALSE" ID="{11B354B3-9765-4472-A89C-E1C4C066419F}" Name="SalaryRate"/>
      <Field DisplayName="Bio" Type="Note" Required="FALSE" ID="{AA2FC197-14C8-4f6c-9833-2679247F5925}" Name="Bio"/>
    </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>
          <FieldRef Name="SalaryRate"></FieldRef>
        </ViewFields>
        ...
      </View>
    </Views>
    <Forms>
     ...
    </Forms>
  </MetaData>
</List>


Other CAML actions will be described in next articles.

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

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


Tags: sharepoint 2010

Back to Home
blog comments powered by Disqus

Copyright © 2011 Eugene Martynov, CSS Templates by Inf Design