Welcome to MSDN Blogs Sign in | Join | Help
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="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms"> <FormTemplates
xmlns="http://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: ,
Posted: Monday, January 26, 2009 6:10 PM by dario.martirani
Filed under:

Comments

Vadim Dzyuban said:

Hi Dario,

It is very interesting topic but I'm not so clear with everything. Does your solution require to modify/change

the \12\TEMPLATE\CONTROLTEMPLATES\DefaultTemplates.ascx file or create a new one (with new name) based of this file (steps 1-3 in your post) ?

I've created a feature with site columns, content type (MyContentType inherited from Document content type) and document library (MyDocLib) and deployed it. All are CAML based. Then while uploading a file into doc lib, in edit and view mode the Content Type choice/drop-down box appears with two choices - Document and MyContentType. I need to Hide this choice/drop-down box and MyContentType must be used by default while loading documents into MyDocLib library.

Could you please explain your solution in more detail. I'm new to SharePoint.

Thanks

Vadim Dzyuban

# March 6, 2009 11:54 AM

dario.martirani said:

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 \12\TEMPLATE\CONTROLTEMPLATES folder (or subfolder) because is not a best practice to modify sharepoint files and also, most important, it's not supported by Microsoft.

Dario

# March 7, 2009 11:34 AM

Vadim Dzyuban said:

Hello Dario,

Here are the steps I've followed:

1. I've created new MyCustomForm.ascx (by opening the \12\TEMPLATE\CONTROLTEMPLATES\DefaultTemplates.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

# March 9, 2009 4:06 PM

dario.martirani said:

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

# March 9, 2009 5:53 PM

Vadim Dzyuban said:

Hi Dario,

1. I've created new MyCustomForm.ascx in the \12\TEMPLATE\CONTROLTEMPLATES folder from DefaultTemplates.ascx. 2. Inside this new file I left just one rendering template with ID="ListForm" and then renamed it to ID="MyCustomForm" 3. I used the same name "MyCustomForm" in XmlDocuments code. 4. I deployed and activated my feature, went to my doc lib, uploaded a file.

Unfortunately the drop-down content type choice box still has been rendered and visible (not hidden) in Edit form/mode as well.

Any idea ? Did anyone did this implementation ?

Thanks

Vadim

# March 10, 2009 10:08 AM

dario.martirani said:

Hi Vadim,

have you re-created the doclib after deploying new features?

# March 12, 2009 6:49 AM

Vadim Dzyuban said:

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 \12\TEMPLATE\CONTROLTEMPLATES\MyCustomForm.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#.

# March 12, 2009 10:04 AM

dario.martirani said:

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

# March 12, 2009 10:58 AM

Vadim Dzyuban said:

You're right Dario, and this can be done as explained in the Ton Stegeman's post via C# at http://www.tonstegeman.com/Blog/Lists/Posts/Post.aspx?List=70640fe5-28d9-464f-b1c9-91e07c8f7e47&ID=27. Following his advise and implementing code below then the choice (drop-down) box of content types is not showing up at all (it is like hidden)

SPContentType listCt1 = list.ContentTypes["MyContentType"];

       SPContentType listCt2 = list.ContentTypes["Document"];

SPFolder folder = list.RootFolder;

       SPContentType[] orderedContentTypes = new SPContentType[1];

       orderedContentTypes[0] = listCt1;

       folder.UniqueContentTypeOrder = orderedContentTypes;

       folder.Update();

Thank you anyway for your feedback, patience and help. I'm still with my question, am I correctly created MyCustomForm.ascx file (see above)... something missing in that process of hiding choice control via xml/CAML ?

Thanks

Vadim

# March 12, 2009 4:33 PM

RG said:

Great Info Dario. Thanks for the fix.

# July 23, 2009 1:27 PM

McThrustin said:

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.

# September 11, 2009 2:53 PM

dario.martirani said:

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.

# October 11, 2009 2:40 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker