Pages

Tuesday, June 14, 2011

SharePoint: Custom add/edit/display form for list

Sometimes we don’t want to use SharePoint’s custom add/edit/display form. We want our own custom form when user will try to view, edit or add an item. I’ll show you today how easily we can do so.
For this post I’ll consider I’ll have a list with three fields: Product Code, ProductName and ProductDescription. I’ll show how we can create a list with these two fields with custom add/edit/display form. The list’s fields are described in the table below:
Field name Field Type Comments
Product Code Text Title field will be used instead of creating a new one
Produce Name Text
Product Description Text
Table 1: Custom list template/Content Type’s fields

The first step of this approach is to create a content type with required fields associated with the content type. The noticeable point here is that In the content type declaration, we can define custom forms for add/edit/display.

Step 1: Create a content type for your list

To create content type right click on your project and click ‘add new item’ and then select content type as shown below:
image
Figure 1: Add content type

Next you will be prompted for the base content type as shown below: If you want to create a custom list, you can select Item as shown below:
image
Figure 2: ‘Item’ is the base content type for custom/generic list

Then you will have provided the content xml file. You need to modify the content type xml file as shown below. Please modify the Inherits=”False” from the content types.
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!--Defined fields-->
  <Field ID="{30C3D21A-A7C9-410E-A896-82875475F697}" Name="ProductName"
         DisplayName="Product Name" Type="Text" Required="FALSE" >
  </Field>
  <Field ID="{9621763e-3494-4a86-a3eb-fd2593f1a1f1}" Name="ProductDescription"
         DisplayName="Product Description" Type="Text" >
  </Field>
  <!-- Parent ContentType: Item (0x01) -->
  <ContentType ID="0x0100c5e54b7f62ad451a92f9235d43ec9082"
               Name="ProductContentType"
               Group="Custom Content Types"
               Description="My Content Type"
               Inherits="false"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" 
                DisplayName="Product Code" Sealed="TRUE"/>
      <FieldRef ID="{82642ec8-ef9b-478f-acf9-31f7d45fbc31}" Name="LinkTitle" 
                DisplayName="Product Code" Sealed="TRUE"/>
      <FieldRef ID="{BC91A437-52E7-49E1-8C4E-4698904B2B6D}" Name="LinkTitleNoMenu" 
                DisplayName="Product Code" Sealed="TRUE" />
      <FieldRef ID="{30C3D21A-A7C9-410E-A896-82875475F697}" Name="ProductName" 
                DisplayName="Product Name" Required="False" />
      <FieldRef ID="{9621763e-3494-4a86-a3eb-fd2593f1a1f1}" Name="ProductDescription" 
                DisplayName="Product Description" Required="False" />
    </FieldRefs>
    <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/blogtest/Product.aspx?mode=display</Display>
          <Edit>_layouts/blogtest/Product.aspx?mode=edit</Edit>
          <New >_layouts/blogtest/Product.aspx?mode=new</New>
        </FormUrls>
      </XmlDocument>
    </XmlDocuments>
  </ContentType>
</Elements>
Figure 3: Content Type xml file with fields and add/edit/display form declared

Now let’s explain what’s in the xml shown in figure 3.
  • Firstly I’ve modified Inherits to false in ConentType tag.
  • I’ve defined two fields inside the <Elements> tag that I’ve used later in content types
  • Then used those fields in <FieldRefs> of <ContentType> tags. These fields will be available in Content type. I’ve also used three existing fields (for Title) from base Content Type (Item).
  • Finally I’ve defined New, Edit and Display form for these content types in <XmlDocuments> section.


Step 2: Create a list Template based on Content type

Now you have defined content types with three fields. Next step is to define a list template based on the content type. To do so click add new item from visual studio context menu and select “List Definition From Content Type” as shown below:
image
Figure 4: Create list definition from content type in ‘Create new Item’ dialog.

Next you will be prompted for available  content types in the project as shown below. Remember to uncheck the button ‘Add a list instance for this list definition’ for this demo now.
image
Figure 5: Create list definition from Content Type

Now you will find two files Elements.xml and Schema.xml files are added. Our full focus will be now on Schema.xml.

Modify the content in <Fields> tag:
Ensure Title fields with display name ‘product code’ exists as shown below:
<Field ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" DisplayName="Product Code" Sealed="TRUE" Type="Text" />

image
Figure 6: 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.
image
Figure 7: 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.
image
Figure 8: Define the fields to be shown in default view

 

 

Step 3: Create Custom add/edit/display form

Next step is to develop a custom application page to use for add/edit/display. As sown in figure 3, you can three different pages for add, edit and view. However for brevity I want to use a single page for all these three operations. You need to create an application page in appropriate location (in my case this is _layouts/blogtest folder). Rather than using three different files for add/edit/display, you can use a single page for all these three tasks as shown below:
<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/blogtest/Product.aspx?mode=display</Display>
      <Edit>_layouts/blogtest/Product.aspx?mode=edit</Edit>
      <New >_layouts/blogtest/Product.aspx?mode=new</New>
    </FormUrls>
  </XmlDocument>
</XmlDocuments>

By passing different parameter to a single page we can identity the page’s mode (add, edit or view). Also SharePoint by default add the list and item id at the end of the page. So your page’s url will look like for new item:
_layouts/blogtest/Product.aspx?mode=new&List=LISTGUID&ID=ITEMID
So from the page (Product.aspx) you can identity the list id and item id from querystring.
I’m not showing details of the product.aspx page here.
You can download the full source code from this skydrive link.

How to use the project attached with this post?

  1. Download the code from here.
  2. Deploy the solution to a SharePoint site.
  3. Create a new list with “ProductListDefinition” template. This template will be installed in the site as you deploy the SharePoint solution.
  4. Now try to add/edit/view items in the list. You will find the custom form product.aspx is used for add/edit/view.

Hope some persons might find the post useful..

19 comments:

  1. Could you write code on the custom edit/display/add forms that you describe in the article?

    ReplyDelete
  2. Yes you can write code in the custom add/edit/display form. The page I've used in this post is Product.aspx which is an application page developed by me. So the full control of the add/edit/display form is urs.

    ReplyDelete
  3. Thanks a lot for this post

    Can I utilize visual studio 2010 project templates/ development technique above for WSS 3.0 also? If yes can you guide me with the tweaks that I have to do.

    ReplyDelete
  4. You rock!! This is just exactly what I was looking for.

    ReplyDelete
  5. Sohel, very nice!! really good article. I have a question: How can I add this in a content editor web part? Is it possible? Thanks!

    ReplyDelete
  6. I don't think you can add list definition in CEW.

    ReplyDelete
  7. Great post! Thanks. One question Sohel. Do you have ever tried to create a custom display form (DispForm.aspx)for a picture library? Regards, Thomas

    ReplyDelete
  8. Thomas, no I had not tried custom display form for picture library but I think the same process described here can be used.

    ReplyDelete
  9. Sohel, I think there is a difference because we have here libraries and not lists and therefore another form templates. My goal is to have some code-behind in the dispform.aspy to manipulate the images at runtime. But I cannot find a solution to cover this need... Regards, Thomas

    ReplyDelete
  10. Thomas, whether you use list or libraries the main thing is content type. Both list and libraries have content type derived from. So if you define a library definition or content type then you can apply custom form for that libraries also.

    ReplyDelete
  11. How can I update custom form edit without deploy all the proyect?
    The problem is that I deploy the proyect all elements of the list are deleted and I don't want this!!

    ReplyDelete
  12. All elements of the list deleted because the list is recreated. Please don't install the list instance feature automatically. Rather enable/disable it programmatically when needed.

    ReplyDelete
  13. Can I utilize visual studio 2010 project templates/ development technique above for WSS 3.0 also? If yes can you guide me with the tweaks that I have to do.

    ReplyDelete
  14. I achived almost same thing is WSS 3.0 by following these steps
    1) create the required list
    2) save the list as template
    3) download the list template to local machine
    4) edit the template file and change the save edit form xml as mentioned above.
    5) reload the template file to list template gallery
    6) create a list using this template
    Here you go you have your list poiting to your custom pages.

    only one problem that I am facing that I would like you to conform if you or anyone else has came across same issue while editing an item
    Case 1) from context menu of item I select edit item. Everything works fine and URL is like this http://server/site/subsite/_layouts/PageName.aspx?mode=edit&List={list ID}&ID={Item ID}&Source={Orignating URL}

    Case 2)I click on title which takes me to display form which is in my case the orignal display form and not the custom.
    Over here I select Edit item option from top menu part of the page. In this case page opens up fine but URL is little messed up like this
    http://server/site/subsite/_layouts/PageName.aspx?mode=edit?ID={Item ID}&List={List ID}&Source={Orignating URL}
    Due to this in code behind I am not getting Mode and ID in Request.querystring collection.
    Due to this my code breaks and only solution I left with is to create two seperate forms for new and edit which I really don't want to do.

    Let me know even if you have some pointers or if you feel that there could be a better way of doing it.

    Thanks in advance!

    ReplyDelete
  15. Interesting post! I enjoyed reading it!
    Thanks for sharing this useful info.keep updating same way.

    Cheers,
    Ramesh Roy
    Sharepoint Custom Development

    ReplyDelete
  16. Thanks for sharing. Very helpful for me.
    I am ok for new form and edit form using your code but when i click title to see display form, only defult display is showed.

    ReplyDelete
  17. Hi, this is good way if you need to deploy your completely custom forms with wsp package. But there are few other ways to customize SharePoint forms easier, if you need to transfer form design to production farm there are also possibilities. Look at this post http://plumsail.blogspot.ru/2012/11/customization-sharepoint-forms-with.html

    ReplyDelete
  18. thanks for sharing. Very helpful for me.

    ReplyDelete
  19. How to deploy the solution to sharepoint? Please need help.

    ReplyDelete

Note: Only a member of this blog may post a comment.