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 - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world 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 *