5/09/2012

SharePoint: Preventing Content Type Changes

 

There are many good reasons to use Content Types… if you can get people to use them, and then leave them alone.   :-)

When creating a new list item based on a Content Type the user needs to select the Content Type from the New dropdown. Once selected, the Content Type should generally not be changed. Changing it is too easy as the user is offered the choice every time they edit the item.

    image  

Preventing this is pretty easy as all we need to do is hide the first row of the edit form's HTML table. The following JavaScript will do this for us and will work in both SharePoint 2007, SharePoint 2010 and SharePoint Online / Office 365:

<script>
var ttnTables = document.getElementsByTagName("TABLE")
for (var i=0;i<ttnTables.length;i++)
{
  if (ttnTables[i].className=="ms-formtable")
  {
    ttnTables[i].rows[0].style.display="none";
  }
}
</script>

 

Now the Content Type is hidden on the edit form.

    image

But can the site owner change it?

The only problem with this solution is that it also prevents the site owner from changing the Content Types. We need to only run the above code for non site owners. To do this we will use the SPSecurityTrimmedControl and set the PermissionString to a suitable value, such as ManageWeb.

<!-- default value -->
<script type="text/javascript">
  var canChangeContentType = false;
</script>

<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ManageWeb">
  <!--text for users who have the ManageWeb permission -->
  <script type="text/javascript">
    canChangeContentType = true;
  </script>
</SharePoint:SPSecurityTrimmedControl>

<script type="text/javascript">
if (canChangeContentType == false)
{
  var ttnTables = document.getElementsByTagName("TABLE")
  for (var i=0;i<ttnTables.length;i++)
  {
    if (ttnTables[i].className=="ms-formtable")
    {
      ttnTables[i].rows[0].style.display="none";
    }
  }
}
</script>
 

There is a workaround!

A user with permissions to create private views can create a DataSheet view with the Content Type column displayed and then edit the Content Type.

 

Where to add this code

SharePoint 2007

  1. Open SharePoint Designer and open the site with the list that uses Content Types
  2. In the Folder List pane expand Lists and your list
  3. Double-click EditForm.aspx
  4. If the HTML is not displayed, click the Code button at the bottom of the page
  5. Search for "PlaceHolderMain"
  6. Select the entire line, right-click the line and select Find Matching Tag (this should find </asp:Content>)
  7. Add the JavaScript from above just before the </asp:Content> line
  8. Save and test (remember, as a Site Owner you will always be able to see the Content Type)

SharePoint 2010

  1. Open SharePoint Designer and open the site with the list that uses Content Types
  2. In the Navigation Pane click Lists and Libraries
  3. Click the name of the list that uses Content Types
  4. In the Forms area click EditForm.aspx
  5. If the window is split into Design and Code views, click the Split button at the bottom of the window
  6. Click in the area just below the existing web part
    image
  7. You should see something similar to the following in the Code view:
    image
  8. Paste the JavaScript from above between the DIV tags
  9. Save and test (remember, as a Site Owner you will always be able to see the Content Type)

 

.

1 comment:

Katerina said...

Hi!! Very helpful post. It worked!
But I need something more specific. I have created a custom Folder contenttype. So when I open the EditForm of the custom Folder I see the contentTypeChoice but when I open the EditForm for the Document there is no such field (the first field is the name of document). So if I use your code, I can hide the ContentTypeChoice in the first case but also the Name for Document. I need to find an "if" statement for recognising when the first row is to show the ContentType. But I am not very familiar to javascript for achieving this.

I would appreciate your help!!

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.