Wednesday, 25 April 2012

SharePoint: Create List Template/ List definition - Part 1

List Template blog series:
Part 1 : Create List Templates/ List Definitions – Traditional approach (Currently reading)

Glad to pull this up after so much difficulties...url blocked in office, no internet/development environment at home and work sufficient enough to keep me busy in weekends also.

Coming to the actual topic, List template is definition or schema of the list where fields, content types, views and forms can be specified. It is used to create multiple lists of same type or structure. We will see how easy it is to create the list definitions step by step.

Create Custom list definition (Traditional Approach):
1.       In Visual Studio 2010, open/create the SPCustomDevelopment SharePoint 2010 project.
2.       In Solution Explorer, right-click the Features node and then click Add Feature as shown in Figure 1.

Figure 1: Add Content Type Feature

3.       Name the feature as SPCustomDevelopment – List Templates and give appropriate description as shown in Figure 2.

Figure 2: Name the Feature

4.       Rename the Feature 1 folder to List Templates as shown in Figure 3 for better readability.

Figure 3: Rename the Feature folder

5.       In Solution Explorer, right-click the SPCustomDevelopment project, select Add, and then select New Item as shown in Figure 4. Type the name as Book List Template

Figure 4: Add List Definition project item

6.       In the next window, make the changes as shown in figure 5 and click Finish button.

Figure 5: List Template wizard

This will create two files, elements.xml and schema.xml. In addition VS 2010 will also associate the newly added Book List Template item to List Templates feature as element manifest.

The elements.xml  contains list template defined using ListTemplate tag. The content types, site columns, list forms and list views to be associated with list template is defined in scehma.xml as shown in figure.

Figure 6: List Schema structure

7.       In schema.xml remove the Item and Folder content type references and add Book content type in the ContentTypes tag as shown in Figure 7. Note that multiple content types can be associated with single content type. (copy the content type definition from schema given in step no 9)

Figure 7: Copy content types into the schema

Note that instead of mentioning all the content type definition, only reference to content types also can be mentioned in the schema.xml using ContentTypeRef tag.

8.       Add site columns definitions into the Fields tag as shown in Figure 8.  (copy the field definitions from schema given in step no 9)

Figure 8: Add site columns into the schema

9.       Name the view having BaseViewID equal to 1 as All Books and change its ViewFields as shown in figure 9.

Figure 9: Create a List View

Note that the other view operations like grouping, filtering the items can also be defined in the view definition of schema in Query tag.

Figure 10: View Operations

Now your schema will look like this.

<?xml version="1.0" encoding="utf-8"?>
<List xmlns:ows="Microsoft SharePoint" Title="Book List Template" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/BookListTemplate" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">
  <MetaData>
    <ContentTypes>
      <ContentType ID="0x0100921DB927A11B42709A0C51C4A37EA816"
               Name="Book"
               Group="SharePoint 2010 Custom Development"
               Overwrite="TRUE"
               Version="0"
               >
        <FieldRefs>
          <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" Required="TRUE" DisplayName="Book Name"/>
          <FieldRef ID="{6F87C38C-5218-45D8-905E-877D694ECC31}" Name="Technology"/>
          <FieldRef ID="{52AE411D-32FB-484C-A04F-ED6C7E41A593}" Name="Publication"/>
          <FieldRef ID="{7D8A68DA-3A2D-444D-A721-1E7A6633402B}" Name="PriceOfBook"/>
          <FieldRef ID="{E93762F1-6F7E-4DFC-9161-0B3B35A7BE35}" Name="BookAuthors"/>
        </FieldRefs>
      </ContentType>
    </ContentTypes>
    <Fields>
      <Field ID="{6F87C38C-5218-45D8-905E-877D694ECC31}"
         Type="Choice"
         Name="Technology"
         StaticName="Technology"
         Description="Select the technology on which this book is written."
         DisplayName="Technology"
         Format="Dropdown"
         Group="SharePoint 2010 Custom Development"
         Required="TRUE"
         Overwrite="TRUE"
         >
        <CHOICES>
          <CHOICE>C#</CHOICE>
          <CHOICE>ASP.NET</CHOICE>
          <CHOICE>SharePoint 2010</CHOICE>
          <CHOICE>MOSS 2007 and WSS 3.0</CHOICE>
        </CHOICES>
      </Field>
      <Field ID="{52AE411D-32FB-484C-A04F-ED6C7E41A593}"
             Type="Text"
             Name="Publication"
             StaticName="Publication"
             DisplayName="Publication"
             Description="Type the Publication of this book."
             Group="SharePoint 2010 Custom Development"
             Required="TRUE"
             Overwrite="TRUE"/>
      <Field ID="{7D8A68DA-3A2D-444D-A721-1E7A6633402B}"
             Type="Currency"
             Name="PriceOfBook"
             StaticName="PriceOfBook"
             DisplayName="Price"
             Description="Enter the book cost."
             Group="SharePoint 2010 Custom Development"
             Required="TRUE"
             Overwrite="TRUE"/>
      <Field ID="{E93762F1-6F7E-4DFC-9161-0B3B35A7BE35}"
             Type="UserMulti"
             Name="BookAuthors"
             StaticName="BookAuthors"
             DisplayName="Authors"
             Description="Enter the book authors"
             Group="SharePoint 2010 Custom Development"
             Required="TRUE"
             Mult="TRUE"
             UserSelectionMode="0"
             UserSelectionScope="0"
             Overwrite="TRUE"/>
    </Fields>
    <Views>
      <View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE">
        <Toolbar Type="Standard" />
        <XslLink Default="TRUE">main.xsl</XslLink>
        <RowLimit Paged="TRUE">30</RowLimit>
        <ViewFields>
          <FieldRef Name="LinkTitleNoMenu"></FieldRef>
        </ViewFields>
        <Query>
          <OrderBy>
            <FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
          </OrderBy>
        </Query>
        <ParameterBindings>
          <ParameterBinding Name="AddNewAnnouncement" Location="Resource(wss,addnewitem)" />
          <ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
          <ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_ONET_HOME)" />
        </ParameterBindings>
      </View>
      <View BaseViewID="1" Type="HTML" WebPartZoneID="Main" DisplayName="All Books" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/generic.png" Url="AllItems.aspx">
        <Toolbar Type="Standard" />
        <XslLink Default="TRUE">main.xsl</XslLink>
        <RowLimit Paged="TRUE">30</RowLimit>
        <ViewFields>
          <FieldRef Name="LinkTitle"></FieldRef>
          <FieldRef Name="Technology"></FieldRef>
          <FieldRef Name="Publication"></FieldRef>
          <FieldRef Name="PriceOfBook"></FieldRef>
          <FieldRef Name="BookAuthors"></FieldRef>
        </ViewFields>
        <Query>
          <OrderBy>
            <FieldRef Name="ID"></FieldRef>
          </OrderBy>
        </Query>
        <ParameterBindings>
          <ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
          <ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_DEFAULT)" />
        </ParameterBindings>
      </View>
    </Views>
    <Forms>
      <Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
      <Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
      <Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
    </Forms>
  </MetaData>
</List>



10.   Deploy the project by pressing F5.
11.   Once deployment is successful, Book List Template will be created as shown in figure 11. It can be used to create multiple lists of type Book List Template having same structure.

Figure 11: Newly Added Book List Template


One important thing to remember here is, fields and content types in schema.xml are list fields and list content types.You can create list fields and list content types without creating site fields and site content types.


Custom list forms

There are various ways to modify the list forms. Few are mentioned in my last blog Basicsof Content Types where forms are associated with content types instead of list directly. List schema (schema.xml) too provide a way to associate custom list forms through Forms tag which once associated, are used for all list content types.

I am not going into details how to associate the custom list forms to list using Forms tag. It is best explained by Igno Karstein his two blogs with examples.

Even though I personally like creating list definitions using above traditional approach as I believe it helps me to understand/learn the list definitions, fields and content types all together in better way. There is another approach to create list definition using which you can generate the list definition schema. It does not require knowledge of field, content type schemas. Will cover that in my next blog in this series.

No comments:

Post a Comment