Posted on April 6, 2016|by: Johnny Bouder| 1 Comment

In a previous post, I explained how you could use a new feature in SharePoint 2013 called Client Side Rendering (CSR) to color code a list view. Using that same methodology, I’ll be going over how you can easily hide a form field based on SharePoint permissions…another one of those things that I’m always asked to implement.

For this specific example, I’ll be hiding the “Assigned To” field on the New Form of a standard Tasks list that will only show it if the current user is a member of the Viewers group, or if the user is a site collection admin…I know the example doesn’t make a lot of sense, but I’m trying to keep it simple. :)

To implement this exact solution, follow these steps:

1. Create a standard Tasks list
2. Download the MSS.HidingFormFields.js file
3. Upload the JavaScript file to SharePoint (I like to use the Site Assets library at the root of the site collection)
4. Navigate to the Tasks list
5. Click the “List” tab in the ribbon, click the “Form Web Parts” button in the Customize Form group, and select the “Default New Form” button
6. Edit the Web Part and set the JS Link property under “Miscellaneous” to point to the JavaScript file (Ex. ~sitecollection/SiteAssets/Scripts/MSS.HidingFormFields.js)
7. Save the Web Part

Note: the URL for the JSLink property property must contain an appropriate SharePoint token.

And if all went well, assuming that you’re not in the Visitors group or a Site Collection Admin, the “Assigned To” field should be hidden on the New Form.

A few things to note:

This only applies our custom JavaScript to the New Form. If you want to apply it to the Display and Edit Forms, you’ll also need to set the JS Link property on those. You may also need to either customize my JS file to fit the needs of each form, or create separate files for each form.
I’m using a little jQuery, so to use this solution, you’ll need to make sure that you have jQuery loaded into the page as well. I like to load it onto my Masterpage so I can do jQuery magic everywhere.

If you open the JavaScript file, you can see that we’re just registering an OnPostRender function. This function, when called, initially hides any applicable fields by name, then queries SharePoint using some JSOM to look up the current users permissions. If the user is a member of the group, or a site collection admin, the field can be toggled back to be visible.

And that’s it! Stay tuned for my next post on creating a document rollup using SharePoint 2013 search…which also uses CSR. :)

Enjoy!

Filed in: Collaboration, Sharepoint, Tips & Tricks, Web

One Comment

  1. April 13, 2016 at 10:56 pm

    This looks really useful, thank you for sharing!

Your email is kept private. Required fields are marked *

*
*