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 |
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: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:
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>
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: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.
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" />
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.
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.
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>
_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?
- Download the code from here.
- Deploy the solution to a SharePoint site.
- Create a new list with “ProductListDefinition” template. This template will be installed in the site as you deploy the SharePoint solution.
- 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..
Could you write code on the custom edit/display/add forms that you describe in the article?
ReplyDeleteYes 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.
ReplyDeleteThanks a lot for this post
ReplyDeleteCan 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.
You rock!! This is just exactly what I was looking for.
ReplyDeleteSohel, very nice!! really good article. I have a question: How can I add this in a content editor web part? Is it possible? Thanks!
ReplyDeleteI don't think you can add list definition in CEW.
ReplyDeleteGreat post! Thanks. One question Sohel. Do you have ever tried to create a custom display form (DispForm.aspx)for a picture library? Regards, Thomas
ReplyDeleteThomas, no I had not tried custom display form for picture library but I think the same process described here can be used.
ReplyDeleteSohel, 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
ReplyDeleteThomas, 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.
ReplyDeleteHow can I update custom form edit without deploy all the proyect?
ReplyDeleteThe problem is that I deploy the proyect all elements of the list are deleted and I don't want this!!
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.
ReplyDeleteCan 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.
ReplyDeleteI achived almost same thing is WSS 3.0 by following these steps
ReplyDelete1) 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!
Interesting post! I enjoyed reading it!
ReplyDeleteThanks for sharing this useful info.keep updating same way.
Cheers,
Ramesh Roy
Sharepoint Custom Development
Thanks for sharing. Very helpful for me.
ReplyDeleteI 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.
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
ReplyDeletethanks for sharing. Very helpful for me.
ReplyDeleteHow to deploy the solution to sharepoint? Please need help.
ReplyDelete