Define custom new/edit/display forms for Content Types


Recently, I got a requirement to define custom pages for a content type item. What am I talking about? If that’s the question these are the pages which open when you try to open, edit and create an item from the list.

So the best way to customize these pages is to add these pages entry to the custom content type. You can create your page (Either same for all the three or separate pages) and add them to the layouts folder.

You can then reference these pages in the content type’s element.xml file. So your content type elements.xml file would look like:

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   3:   <ContentType ID="0x010800b1684200801b11ddbcb20050c2490048"
   4:                Name="Publication Approval Content Type5"
   5:                Group="Publication Approval Workflow"
   6:                Description="5 Custom Workflow Task for the Publication Approval Workflow"
   7:                Version="0"
   8:                Hidden="FALSE">
   9:     <FieldRefs>
  10:       <FieldRef ID="{81561b40-7aae-11dd-9b5a-0050c2490048}"
  11:                 Name="_NotifyIR"/>
  12:     </FieldRefs>
  13:     <XmlDocuments>
  14:       <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
  15:         <FormUrls xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
  16:           <Display>_layouts/PublicationApprovalForms/TaskEditForm/TaskEditForm.aspx</Display>
  17:           <Edit>_layouts/PublicationApprovalForms/TaskEditForm/TaskEditForm.aspx</Edit>
  18:           <New>_layouts/PublicationApprovalForms/TaskEditForm/TaskEditForm.aspx</New>
  19:         </FormUrls>
  20:       </XmlDocument>
  21:     </XmlDocuments>
  22:   </ContentType>
  23: </Elements>

Add this code to the elements.xml of the content type and then re-install and activate the content type. And Voila your pages would be customized.

One more thing, if you would like to do the above programmatically then it is easier then the above way. (Of course you would have to create the aspx pages.)

   1: using (SPSite site = new SPSite("http://Site/"))
   2: {
   3:     using (SPWeb web = site.OpenWeb())
   4:     {
   5:         SPList list = web.Lists[documentlibrary];
   6:         SPContentType ct = list.ContentTypes[contentType];
   7:         if (!on)
   8:         {
   9:             ct.EditFormUrl = string.Empty;
  10:             ct.NewFormUrl = string.Empty;
  11:             ct.DisplayFormUrl = string.Empty;
  12:          }
  13:          else
  14:          {
  15:              ct.EditFormUrl = "_layouts/whateverEdit.aspx";
  16:              ct.NewFormUrl = "_layouts/whateverNew.aspx";
  17:              ct.DisplayFormUrl = "_layouts/whateverDisplay.aspx";
  18:          }
  19:          ct.XmlDocuments.Delete("http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url");
  20:          ct.Update();
  21:          list.Update();
  22:      }
  23:  
  24: }


Comments (6)

  1. Anil says:

    hi,

    Where should I write this code to redirect to my custom form?

    Thanks

    Anil

  2. Ashish Kanoongo says:

    Anil

    Create a sample aspx page and use this code and put this page in  layouts folder and run your sharepoint application like http://portal/_layouts/sample.aspx It will update you seleced list/document library.

    Good Luck

    Ashish Kanoongo

  3. Hello,

    I want to use the same page for all 3 urls (view, edit and new). I can therefore set my new form url, edit form url and display form url to the same page (CustomListItemForm.aspx) and I can differentiate edit from new because Sharepoint passes the list item ID in the querystring for edit. Therefore I can use the same form for edit and new. However, how do I differentiate edit from view, when I am using the same form for both? I did not see a querystring that SP provides that would indicate the user is attempting to view the item vs edit it. Also how would I ensure security if the pages are the same page? It seems redundant to have to create an edit page and a view page when they are essentially the same thing, minus whether or not the controls are readonly or not.

  4. MalayVasavada says:

    Hello,

    I am using SharePoint 2010 and working on SharePoint list no on document library.

    I have done exactly same mentioned in above code and code works perfect. I have two content types on list. And I have set respective new,edit and display custom application page for both content types. All works well. However challenge is coming when I click on LinkTitle field value in the allitems.aspx view.

    When I select an item and click on view item from ECB menu or from ribbon, custom display form opens up for that content type which is perfect.

    However when I just click on the hyperlink of LinkTitle, it goes to allitems.aspx.

    Because I am using custom application page, I am using SharePoint:SaveButton to save the list item.

    Please let me know what needs to be done here. Any help or pointer would be highly appreciated.

  5. Asad says:

    If I reorder or hide/show fields in my content type, then is that change going to reflect on the custom pages?