kwizcom banner advertisement

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 in master page, as it will repeat on every page. The requirement is targeted on ONLY one page. So, We need role based content inside 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.
Role Based Content in Sharepoint Content Editor Web Part Role Based Content in Sharepoint Content Editor Web Part Reviewed by Salaudeen Rajack on 1:47 PM Rating: 5

2 comments:

  1. 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?

    ReplyDelete
    Replies
    1. That's the reason I'm using a variable "UserHasPermissions" instead of directly using functions! Send me your Master page code & Error!

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.