Udostępnij za pośrednictwem


How to hide content type choice control in edit forms

If you want the edit form of a particular list to don’t show some field, you can change the ContentType definition by setting ShowInEditForm=”FALSE” in corresponding FieldRef element.

If you want to do the same with Content Type field, the above hint simply don’t work.

To make it working you have to modify the rendering template of the edit form. To do this you have to create a new rendering template:

  1. copy the standard ListForm template from DefaultTemplates.ascx (ControlTemplate folder)
  2. create a new ascx file in the same folder and paste ListForm template
  3. change the template id to MyCustomForm
  4. remove the Sharepoint ContentTypeChoice control from the custom template

Then you have to associate the edit form to the custom template created above, to do this you can modify the content type definition inserting XmlDocuments tag under ContentType tag like in the following example:

 <XmlDocuments>
  <XmlDocument 
  NamespaceURI="https://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
    <FormTemplates  
    xmlns="https://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
      <Display>ListForm</Display>
      <Edit>MyCustomForm</Edit>
      <New>ListForm</New>
    </FormTemplates>
  </XmlDocument>
</XmlDocuments>

If you don’t have content type you can change the list definition schema by using the following code:

 <Forms>
  <Form Type="DisplayForm" 
        Url="DispForm.aspx" 
        SetupPath="pages\form.aspx" 
        WebPartZoneID="Main" />
  <Form Type="EditForm" 
        Url="EditForm.aspx" 
        SetupPath="pages\form.aspx" 
        Template="MyCustomForm" 
        WebPartZoneID="Main" />
  <Form Type="NewForm" 
        Url="NewForm.aspx" 
        SetupPath="pages\form.aspx" 
        WebPartZoneID="Main" />
</Forms>

HTH
Dario

Technorati Tag: Sharepoint,MOSS

Comments

  • Anonymous
    March 06, 2009
    The comment has been removed

  • Anonymous
    March 07, 2009
    Hi Vadim, probably you only need to delete the reference to the Document CT from your library definition schema to show only one entry in the dropdown list. If you want to hide the dropdown list you can follow the steps in my post. You have to create a new .ascx file (ex. MyCustomForm.ascx) in the 12TEMPLATECONTROLTEMPLATES folder (or subfolder) because is not a best practice to modify sharepoint files and also, most important, it's not supported by Microsoft. Dario

  • Anonymous
    March 09, 2009
    Hello Dario, Here are the steps I've followed:

  1. I've created new MyCustomForm.ascx (by opening the 12TEMPLATECONTROLTEMPLATESDefaultTemplates.ascx file and saving it as MyCustomForm.ascx). So, I have a newly created file as well as the original file.
  2. Inside new MyCustomForm.ascx file I've removed the <td valign="top" class="ms-formbody"><asp:DropDownList ID="ContentTypeChoice" ToolTip="<%$Resources:wss,form_content_type%>" runat="server"/><br> tag
  3. I've modified the content type definition inserting XmlDocuments tag under ContentType tag as  you suggested.   After deployment, activation the feature, going to my doc lib and uploading a file then in the Edit mode I'm getting an empty rendered area. Am I missing something in steps 1-3 above ? Thanks Vadim
  • Anonymous
    March 09, 2009
    Hi Vadim, in your custom .ascx file you need only te ListForm template and not all the templates included in DefaultTemplates.ascx. You have also to change the template id to MyCustomForm (or whatelse you want) and use the same id in XmlDocuments code. The template id is the reference key. HTH Dario

  • Anonymous
    March 10, 2009
    The comment has been removed

  • Anonymous
    March 11, 2009
    Hi Vadim, have you re-created the doclib after deploying new features?

  • Anonymous
    March 12, 2009
    Yes, I did. In my feature: I create my site columns, my content type (inherited from Document), add my site columns to my content type, create my doc lib and bind it to my content type. All are CAML based (no C# code). What I need after the feature will be deployed is while a user loads a document then (1) the choice (drop-down) control should not be visible (rendered) at all and (2) my content type (not Document content type) with the my site columns should be in use (as a default content type). Following the above advise I've created MyCustomForm.ascx from 12TEMPLATECONTROLTEMPLATESMyCustomForm.ascx as follows. Inside MyCustomForm.ascx I removed everything except the lines with "<%@ ... " at the beginning of file and the section with "<SharePoint:RenderingTemplate ID="ListForm" ... </SharePoint:RenderingTemplate> , then I renamed section ID="ListForm" to ID="MyCustomForm". Is this correct ? I need to hide the choice (drop-down) control of content types even the default content type will be the Document from which I inherited my content type. I can try to setup my content type as a default content type via c#.

  • Anonymous
    March 12, 2009
    Your problem is that Document is the default CT of the doclib. In the schema of your list definition you have to specify only your custom content type. If you don't have a custom list definition (and you don't want to create it) you have to write some code to specify the default (and probably only) CT. Dario

  • Anonymous
    March 12, 2009
    The comment has been removed

  • Anonymous
    July 23, 2009
    Great Info Dario. Thanks for the fix.

  • Anonymous
    September 11, 2009
    You don't need to add the entry into the DefaultTemplates.ascx file, you can create a seperate file with your definition inside it, as long as it's in the same folder.  This will keep the original file unmolested and allows for some portability of code to different servers.

  • Anonymous
    October 10, 2009
    You are absolutely right McThrustin! I didn't write about pasting new template to DefaultTemplates.ascx but I agree with you that it can be misunderstood. I will soon update the article.

  • Anonymous
    January 03, 2010
    Any ideas on doing the opposite with custom list forms?  I.e. I need to add the option to change a content type from a DataFormWebPart.  I've tried adding a SharePoint control that points to the ContentType field, but noticed that the onchange event that exists in the ListFormWebPart does not exist for this control.  I tried setting that event via javascript, and although it matched perfectly it still didn't work.  The LFWP handles content type switches by doing a call to STSNavigate and setting the ContentTypeId param in the query string, but the DFWP doesn't know how to handle this param and just errors out. I am currently fumbling through creating a replacement "OK" button, hoping to set the content type of the item based on the value in the Content Type control, but I keep getting a ThreadAbortException which I can't seem to get past.  Any ideas would be greatly appreciated.