Tuesday, December 4, 2012

SharePoint 2010 : How to Create a Custom List Definition and a Instance

I will start by creating an Empty SharePoint project and I am going to deploy it as a Farm solution.
Untitled1
Empty SharePoint Project
Untitled2
Trust Level - Farm Solution
Then I am adding a SharePoint Mapped Folder to Images and I am adding a image to use as Feature Image Url.
Untitled3
SharePoint "Images" Mapped Folder
Untitled4
SharePoint "Images" Mapped Folder created
Then I am right clicking on my Project and add a new item which is a “List Definition”.
image
Add List Definition
Now I am setting the Display name of my list definition and setting up the list type as a “Custom List”.
Untitled5
List Display Name & Type

A Feature will be created in addition to my list definition. I am double clicking on the feature and in the properties I am setting up the Image Url.
Untitled6
Feature Image Url

Now I am modifying the Schema.xml file in the list definition. I am adding fields which should be included in my list.

Schema.xml
<Fields>
        <Field Name="EmpId" DisplayName="Employee Id" Type="Text"></Field>
        <Field Name="EmpName" DisplayName="Employee Name" Type="Text"></Field>
</Fields>


Since I have created an instance of my list, I am going to put some default values. I am modifying the Elements.xml of the instance.

Elements.xml
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListInstance Title="MySharePointProject - MyListInstance"
                OnQuickLaunch="TRUE"
                TemplateType="10000"
                Url="Lists/MySharePointProject-MyListInstance"
                Description="My List Instance">
      <Data>
          <Rows>
              <Row>
                  <Field Name="EmpId">1</Field>
                  <Field Name="EmpName">Jaliya Udagedara</Field>
              </Row>

              <Row>
                  <Field Name="EmpId">2</Field>
                  <Field Name="EmpName">John Doe</Field>
              </Row>

              <Row>
                  <Field Name="EmpId">3</Field>
                  <Field Name="EmpName">Jane Doe</Field>
              </Row>
          </Rows>
      </Data>
  </ListInstance>
</Elements>


Now I am deploying the solution and once it is activated, I can see my List definition is available.
Untitled12
Create New List

And I can see a quick launch item is created.
Untitled7
Created List Instance

I am getting the default view of the list instance. But as you can see the two fields I have added is not displaying here. So I am again modifying the Schema.xml file in the list definition as follows. I am deploying the solution again and now the fields are displaying.

Schema.xml
<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">
    <Toolbar Type="Standard" />
    <XslLink Default="TRUE">main.xsl</XslLink>
    <RowLimit Paged="TRUE">30</RowLimit>
    <ViewFields>
      <FieldRef Name="Attachments"></FieldRef>
      <FieldRef Name="LinkTitle"></FieldRef> <!--if you don’t want to see title, comment this line-->
      <FieldRef Name="EmpId"></FieldRef>
      <FieldRef Name="EmpName"></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>


Untitled9
Created List Instance
Now when I click on Add new item, only Title field is displayed and not my created fields.
Untitled8
Created List Instance
For that, I am commenting the following part in the Schema.xml file in the list definition as follows.

Schema.xml
<!--<ContentTypes>
      <ContentTypeRef ID="0x01">
        <Folder TargetName="Item" />
      </ContentTypeRef>
      <ContentTypeRef ID="0x0120" />
    </ContentTypes>-->


Now I can see the fully completed output.
Untitled11
Created List Instance
Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment