Role-Based Content in SharePoint Content Editor Web Part

The requirement here is: My client wants to show different contents in the content editor web part to Anonymous users and authorized users. To handle these kind of security trimming, we usually use “Security Trimming” in the master page.

But here, the end-user doesn’t want to place the content on the master page, as it will repeat on every page. The requirement is targeted on ONLY one page. So, We need role-based content inside the SharePoint content editor web part.

Alright, Here is the solution for the issue:

Insert the below code to Master page: (You can insert just below <form runat=”server” onsubmit=”return _spFormOnSubmitWrapper();”> )

<script>
  var UserHasPermissions=false;
</script>

<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ManageWeb">

  <script>
    UserHasPermissions=true;
  </script>

</SharePoint:SPSecurityTrimmedControl>

Save & Publish the master page.

Now, In the content editor webpart on the target page,  place the below logic code.

<script>
if (UserHasPermissions)
{
  // Place the content for Logged-in user 
}
else
{
 //Place the content for anonymous user 
}
</script>

That’s it, we are done.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

2 thoughts on “Role-Based Content in SharePoint Content Editor Web Part

  • I get an error when the user is not authenticated. It does not understand what this “if (UserHasPermissions)” is because the user is not authenticated. Any ideas around this?

    Reply
    • That’s the reason I’m using a variable “UserHasPermissions” instead of directly using functions! Send me your Master page code & Error!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *