SharePoint: Run JavaScript based on user rights / permissions


There are all kinds of tricks you can do with JavaScript in SharePoint. Sometimes though you only want the JavaScript to run for selected users.

  • If you want to run the JS for only a single site just check the URL for the page name (document.location.href)
  • If you want to run the JS for only a single person just write some JS to retrieve the user’s name from the Welcome menu at the top of the page
  • If you want to run the JS based on the permissions of the user, then you need to use a SharePoint control called: SPSecurityTrimmedControl

Using SPSecurityTrimmedControl

The SPSecurityTrimmedControl cannot be used inside of Content Editor Web Part as it must be processed on the web servers. So, you must add something to the ASPX page or the master page.


Here’s an example:


<script> var UserHasPermissions=false; </script> <Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ManageWeb"> <script> UserHasPermissions=true; </script> </SharePoint:SPSecurityTrimmedControl>


If you put this block of code somewhere on the master page before where you need to run your code then you can use the value UserHasPermissions in the ASPX file. If you put this in the master page before where web parts are loaded then you can use this in your Content Query Web Part JavaScript tricks!

In your JavaScript you only need to check this value:


<script> // do something based on permissions if ( UserHasPermissions ) { // add your code here alert("ALERT from SecurityTrimmedControl") } </script>


You may want to add several of these tests so you can then have UserHasWebPermissions, UserHasDeletePermissions, UserHasXYZPermissions, etc.


More Info:

For more info on SPSecurityTrimmedControl see:

There is a list of the permissions strings at the bottom of this article: 
These strings are the same permissions found in the SharePoint Permission Levels (Site Actions, Site Settings, Advanced Permissions, Settings, Permission Levels - then add or edit an existing to see the list)


And here’s another example to hide “All People” on the People and Groups pages:

If you don't mind editing the application.master, and there is a permission item that can identify the group of users who should see or not see the option then…

Insert the following just before </BODY> in application.master. Modify PermissionsString="ManageWeb" to one of the choices found here that only your desired users have.

var UserHasPermissions=false;
<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ManageWeb">

if ( !UserHasPermissions )
  var x = document.getElementsByTagName("A")
  for (var i=0;i<x.length;i++)
    if (x(i).innerText=="All People")




No comments:

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.