Custom List definition and List Form with Content Type Inheritance in SharePoint 2010


Gopal Muthukrishnan
SharePoint Developer
Published On :   21 Dec 2012
Visit Count
Today :  8    Total :   19824
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


In this blog post we can see how to create a custom List definition and also how to create a custom Add and Edit form for a SharePoint List in SharePoint 2010.In this demo application I have created a list with two different content types namely Permanent Employee and Contract Employee. Then created a Custom form for list definition without using List Instance and I have demonstrated how to create our own custom add,edit and cancel using submit form .

Step:1

Open Visual Studio 2010 and create a new EMPTY Share point project located under SharePoint2010 category and name it as ListDefUsingCTypes then click ok to proceed for further step.

sharepoint_newprj

Step 2:

Give the URL of the project that you have created in your SharePoint environment and click validate to ensure that you are able to connect your SharePoint environment. Select Farm solution and click finish

validate

Step:3

Add new item(select List Definiton) in the solution explorer and name as ListDef-TwoCTypes and click Add

List_newpj

STEP 4:

Then select the custom list type of List definition in the dropdown list and uncheck the list instances. Click Finish

List_confgWiqard

Step5:

To modify the list definition Elements.xml file

· In Solution Explorer, under the ListDef-TwoCTypes node, open the Elements.xml file. Replace the contents of the ListDef-TwoCTypes\Elements.xml file with the following markup.

· Add this tag inside DisallowContentTypes="FALSE" in the ListTemplate tag

 <?xml version="1.0" encoding="utf-8"?>
 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <Field ID="{AD2E49DD-0F22-41DF-A6E6-143DA4233A00}" Name="EmpName" DisplayName="EmpName"
         Type="Text" Required="TRUE" Sealed="TRUE"></Field>
   <Field ID="{3836280D-B224-4EB9-B828-825D604F29CE}" Name="Age" DisplayName="Age"
          Type="Number" Required="TRUE" Sealed="TRUE"></Field>
   <Field ID="{13742AE8-8D25-4489-BE21-D096A857005B}" Name="Address" DisplayName="Address"
          Type="Note" Required="TRUE" Sealed="TRUE"></Field>
   <Field ID="{9C058F10-4CA0-42B3-9362-BE7A37BD83BE}" Name="Experience" DisplayName="Experience"
          Type="Number" Required="TRUE" Sealed="TRUE"></Field>
   <Field ID="{0A33F782-B1FF-4F6C-8CF4-A24D8F28B3B8}" Name="VendorCompany" DisplayName="VendorCompany"
          Type="Note" Required="TRUE" Sealed="TRUE"></Field>
   <ContentType ID="0x01009A0146B37A4D43E4B938F14E10DD0872" Name="PermentEmployee" Group="Employee">
     <FieldRefs>
       <FieldRef ID="{AD2E49DD-0F22-41DF-A6E6-143DA4233A00}"/>
       <FieldRef ID="{3836280D-B224-4EB9-B828-825D604F29CE}"/>
       <FieldRef ID="{13742AE8-8D25-4489-BE21-D096A857005B}"/>
       <FieldRef ID="{9C058F10-4CA0-42B3-9362-BE7A37BD83BE}"/>
     </FieldRefs>
   </ContentType>
   <ContentType ID="0x0100CB33B794B1E0466DBE8EA398204F699C" Name="ContractEmployee" Group="Employee">
     <FieldRefs>
       <FieldRef ID="{AD2E49DD-0F22-41DF-A6E6-143DA4233A00}"/>
       <FieldRef ID="{3836280D-B224-4EB9-B828-825D604F29CE}"/>
       <FieldRef ID="{13742AE8-8D25-4489-BE21-D096A857005B}"/>
       <FieldRef ID="{0A33F782-B1FF-4F6C-8CF4-A24D8F28B3B8}"/>
     </FieldRefs>
   </ContentType>

Step: 6

To modify the schema.xml file

· In Solution Explorer, under the ListDefinition1 node, open the Schema.xml file. Replace the content of the ListDefinition1\Schema.xml file with the following markup.

· Then add <FieldRefs>,this will use to show on custom based form

 <FieldRef ID="{54E2464E-94B6-4CC8-964E-C098261D022B}" Name="Title" DisplayName="EmpID" Sealed="TRUE"/>
 <FieldRef ID="{7BE8B8D4-7B6F-4042-B6FA-372578061F2A}" Name="LinkTitle" DisplayName="EmpID" Sealed="TRUE"/>
 <FieldRef ID="{8E69F964-B9A9-41A2-99AC-F25E9B8DBD07}" Name="LinkTitleNoMenu" DisplayName="EmpID" Sealed="TRUE" />

· Then include EnableContentTypes="TRUE" in the list tag

· After that add the following code to customize our windows form based content type that I have used, and in the meantime this is common to both content types

 <XmlDocuments> 
     <XmlDocument NamespaceURI ="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
         <FormUrls xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">                
             <Display>_layouts/ListDefUsingCTypes/FirstLCForm.aspx?mode=display</Display>  
             <Edit>_layouts/ListDefUsingCTypes/FirstLCForm.aspx?mode=edit</Edit>  
             <New>_layouts/ListDefUsingCTypes/FirstLCForm.aspx?mode=new</New>  
         </FormUrls>   
     </XmlDocument>  
 </XmlDocuments>

Add title field in the list template (if not exists)

Then find two fields LinkTitle and LinkTitleNoMenu. Then change their display name to ‘Product Code’ as shown below. These two fields are link to edit menu.

 <Field ID="{7BE8B8D4-7B6F-4042-B6FA-372578061F2A}" Name="LinkTitle" SourceID="http://schemas.microsoft.com/sharepoint/v3"
       StaticName="LinkTitle" Group="$Resources:core,Base_Columns;" ReadOnly="TRUE" Type="Computed"
       DisplayName="EmpID" DisplayNameSrcField="Title" ClassInfo="Menu" AuthoringInfo="$Resources:core,Linked_Item_With_Menu;">
         <FieldRefs>
           <FieldRef ID="{54E2464E-94B6-4CC8-964E-C098261D022B}" Name="Title" />
           <FieldRef ID="{8E69F964-B9A9-41A2-99AC-F25E9B8DBD07}" Name="LinkTitleNoMenu" />
           <FieldRef ID="{1344423c-c7f9-4134-88e4-ad842e2d723c}" Name="_EditMenuTableStart2" />
           <FieldRef ID="{2ea78cef-1bf9-4019-960a-02c41636cb47}" Name="_EditMenuTableEnd" />
         </FieldRefs>
       </Field>
       <Field ID="{8E69F964-B9A9-41A2-99AC-F25E9B8DBD07}" Name="LinkTitleNoMenu" SourceID="http://schemas.microsoft.com/sharepoint/v3"
              StaticName="LinkTitleNoMenu" Group="$Resources:core,Base_Columns;" ReadOnly="TRUE" Type="Computed"
              DisplayName="EmpID" Dir="" DisplayNameSrcField="Title" AuthoringInfo="$Resources:core,Linked_Item;">
         <FieldRefs>
           <FieldRef ID="{54E2464E-94B6-4CC8-964E-C098261D022B}" Name="Title" />
           <FieldRef ID="{9d30f126-ba48-446b-b8f9-83745f322ebe}" Name="LinkFileNameNoMenu" />
         </FieldRefs>
       </Field>

Rename the DisplayName for LinkTitle and LinkTitleNoMenu field

Modify the content in <Views> tag

Open the views tag and add the fields you want to display in default view under <View> with Default value is true as shown below.

 <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>
           <FieldRef Name="EmpName"></FieldRef>
           <FieldRef Name="Age"></FieldRef>
           <FieldRef Name="Address"></FieldRef>
           <FieldRef Name="Experience"></FieldRef>
           <FieldRef Name="VendorCompany"></FieldRef>
         </ViewFields>

Step 7:

Add new item in the Application page from the solution explorer with the name as FirstListForm.aspx

first_form

In the design page of the FirstlistForm.aspx , use table and scripts to design

 <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
 <script type="text/JavaScript">
     function CloseDialog() {
         alert('hi ok');
         SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, 'OK clicked');
         SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.Cancel, 'Cancelled clicked');
     }
     function cancelDialog() {
         SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.Cancel, 'Cancelled clicked');
     }
     </script>    
     <table>
      <tr>
             <td>
                 EmpID
             </td>
             <td>
                 <asp:TextBox ID="txtEmpID" runat="server" MaxLength="255" />
                 <asp:RegularExpressionValidator ID="RegularExpressionValidator3" SetFocusOnError="true" runat="server" ControlToValidate="txtEmpID" ValidationExpression="^[+]?\d+?$" ErrorMessage="Only Numbers"></asp:RegularExpressionValidator>
             </td>
         </tr>
         <tr>
             <td>
                 EmpName
             </td>
             <td>
                 <asp:TextBox ID="txtEmpName" runat="server" MaxLength="255" />
             </td>
         </tr>
         <tr>
             <td>
                 Age
             </td>
             <td>
                 <asp:TextBox ID="txtAge" runat="server" MaxLength="255" />
                 <asp:RegularExpressionValidator ID="RegularExpressionValidator1" SetFocusOnError="true" runat="server" ControlToValidate="txtAge" ValidationExpression="[0-5]?[0-9]?" ErrorMessage="Only Numbers"></asp:RegularExpressionValidator>
             </td>
         </tr>
         <tr>
             <td>
                 Address
             </td>
             <td>
                 <asp:TextBox ID="txtAddress" runat="server" MaxLength="255" />
             </td>
         </tr>
          <tr>
             <td>
                 Experience
             </td>
             <td>
                 <asp:TextBox ID="txtExperience" runat="server" MaxLength="255" />
                 <asp:RegularExpressionValidator ID="RegularExpressionValidator2" SetFocusOnError="true" ControlToValidate="txtExperience" ValidationExpression="^\d{0,2}(\.\d{1,2})?$" runat="server" ErrorMessage="Only Numbers"></asp:RegularExpressionValidator>
             </td>
         </tr>
         <tr>
             <td>
                 <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
             </td>
             <td>
                 <input type="button" value="Cancel" onclick="javascript:cancelDialog()" />
             </td>
         </tr>
     </table>
 
 </asp:Content>

Then open the FirstListForm.CS page and give the following code

 using System;
 using Microsoft.SharePoint;
 using Microsoft.SharePoint.WebControls;
 
 namespace ListDefUsingCTypes.Layouts.ListDefUsingCTypes
 {
     public partial class FirstLCForm : LayoutsPageBase
     {
         protected void Page_Load(object sender, EventArgs e)
         {
             if (!IsPostBack)
                 LoadData();
         }
         private void LoadData()
         {
             var list = SPContext.Current.Web.Lists[new Guid(Request.QueryString["list"])];
             var itemId = 0;
             if (string.IsNullOrEmpty(Request.QueryString["id"]) == false)
             {
                 int.TryParse(Request.QueryString["id"], out itemId);
             }
             if (itemId != 0)
             {
                 var listItem = list.GetItemById(itemId);
                 txtEmpName.Text = (listItem["EmpName"] == null) ? string.Empty : listItem["EmpName"].ToString();
                 txtAge.Text = (listItem["Age"] == null) ? string.Empty : listItem["Age"].ToString();
                 txtAddress.Text = (listItem["Address"] == null) ? string.Empty : listItem["Address"].ToString();
                 txtExperience.Text = (listItem["Experience"] == null) ? string.Empty : listItem["Experience"].ToString();
                 txtEmpID.Text = (listItem["Title"] == null) ? string.Empty : listItem["Title"].ToString();
             }
             if (Request.QueryString["mode"].Equals("display", StringComparison.InvariantCultureIgnoreCase))
             {
                 txtEmpName.Enabled = false;
                 txtAge.Enabled = false;
                 txtAddress.Enabled = false;
                 txtExperience.Enabled = false;
                 txtEmpID.Enabled = false;
                 btnSave.Enabled = false;
             }
         }
 
         protected void btnSave_Click(object sender, EventArgs e)
         {
             var list = SPContext.Current.Web.Lists[new Guid(Request.QueryString["list"])];
             var itemId = 0;
             if (string.IsNullOrEmpty(Request.QueryString["id"]) == false)
             {
                 int.TryParse(Request.QueryString["id"], out itemId);
             }
             SPListItem listItem = null;
             if (itemId == 0)
             {
                 listItem = list.AddItem();
             }
             else
             {
                 listItem = list.GetItemById(itemId);
             }
             listItem["EmpName"] = txtEmpName.Text;
             listItem["Age"] = txtAge.Text;
             listItem["Address"] = txtAddress.Text;
             listItem["Experience"] = txtExperience.Text;
             listItem["Title"] = txtEmpID.Text;
             listItem.Update();
             Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
             Context.Response.Flush();
             Context.Response.End();
         }
     }
 }

Step 8:

Add new item in the Application page from the solution explorer with the name as SecondListForm.aspx

second_form

In the design page of the SecondListForm.aspx, use table and scripts to design

 <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
 <script type="text/javascript">
     function CloseDialog() {
         alert('hi ok');
         SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, 'OK clicked');
         SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.Cancel, 'Cancelled clicked');
     }
     function cancelDialog() {
         SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.Cancel, 'Cancelled clicked');
     }
     </script>
     <table>
      <tr>
             <td>
                 EmpID
             </td>
             <td>
                 <asp:TextBox ID="txtEmpID" runat="server" MaxLength="255" />
             </td>
         </tr>
         <tr>
             <td>
                 EmpName
             </td>
             <td>
                 <asp:TextBox ID="txtEmpName" runat="server" MaxLength="255" />
             </td>
         </tr>
         <tr>
             <td>
                 Age
             </td>
             <td>
                 <asp:TextBox ID="txtAge" runat="server" MaxLength="255" />
             </td>
         </tr>
         <tr>
             <td>
                 Address
             </td>
             <td>
                 <asp:TextBox ID="txtAddress" runat="server" MaxLength="255" />
             </td>
         </tr>
          <tr>
             <td>
                 VendorCompany
             </td>
             <td>
                 <asp:TextBox ID="txtVendorCompany" runat="server" MaxLength="255" />
             </td>
         </tr>
         <tr>
             <td>
                 <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
             </td>
             <td>
                 <input type="button" value="Cancel" onclick="javascript:cancelDialog()" />
             </td>
         </tr>
     </table>
 </asp:Content>

Then open the SecondListForm.aspx.cs page and give the following code.

 using System;
 using Microsoft.SharePoint;
 using Microsoft.SharePoint.WebControls;
 
 namespace ListDefUsingCTypes.Layouts.ListDefUsingCTypes
 {
     public partial class SecondLCForm : LayoutsPageBase
     {
         protected void Page_Load(object sender, EventArgs e)
         {
             if (!IsPostBack)
                 LoadData();
         }
         private void LoadData()
         {
             var list = SPContext.Current.Web.Lists[new Guid(Request.QueryString["list"])];
             var itemId = 0;
             if (string.IsNullOrEmpty(Request.QueryString["id"]) == false)
             {
                 int.TryParse(Request.QueryString["id"], out itemId);
             }
             if (itemId != 0)
             {
                 var listItem = list.GetItemById(itemId);
                 txtEmpName.Text = (listItem["EmpName"] == null) ? string.Empty : listItem["EmpName"].ToString();
                 txtAge.Text = (listItem["Age"] == null) ? string.Empty : listItem["Age"].ToString();
                 txtAddress.Text = (listItem["Address"] == null) ? string.Empty : listItem["Address"].ToString();
                 txtVendorCompany.Text = (listItem["VendorCompany"] == null) ? string.Empty : listItem["VendorCompany"].ToString();
                 txtEmpID.Text = (listItem["Title"] == null) ? string.Empty : listItem["Title"].ToString();
             }
             if (Request.QueryString["mode"].Equals("display", StringComparison.InvariantCultureIgnoreCase))
             {
                 txtEmpName.Enabled = false;
                 txtAge.Enabled = false;
                 txtAddress.Enabled = false;
                 txtVendorCompany.Enabled = false;
                 txtEmpID.Enabled = false;
                 btnSave.Enabled = false;
             }
         }
 
         protected void btnSave_Click(object sender, EventArgs e)
         {
             var list = SPContext.Current.Web.Lists[new Guid(Request.QueryString["list"])];
             var itemId = 0;
             if (string.IsNullOrEmpty(Request.QueryString["id"]) == false)
             {
                 int.TryParse(Request.QueryString["id"], out itemId);
             }
             SPListItem listItem = null;
             if (itemId == 0)
             {
                 listItem = list.AddItem();
             }
             else
             {
                 listItem = list.GetItemById(itemId);
             }
             listItem["EmpName"] = txtEmpName.Text;
             listItem["Age"] = txtAge.Text;
             listItem["Address"] = txtAddress.Text;
             listItem["VendorCompany"] = txtVendorCompany.Text;
             listItem["Title"] = txtEmpID.Text;
             listItem.Update();
             Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
             Context.Response.Flush();
             Context.Response.End();
         }
     }
 }

Step: 9

The solution explorer will look like as below

Solution_Exproler

Step 10: Right click the project in the Solution Explorer and select Rebuild then Deploy the project

Step 11:

1: Enter the URL in the IE and Run SharePoint using username and password

2: Go to Site Action -More Option- create a custom list

3: Run the Custom list

run_newitem

Then click item from new item menu and click the Permanent Employee

Permanent Emp

Then click item from new item menu and click the Contract Employee

contract_page

Step 12:

Final Result Page

Final Result

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Touch