0

I have a web page that is open for the entire user base to access. I want to add some web content on this page that will only be displayed if they are part of a Sharepoint Group. Role specific permission seems easy with SPSecurityTrimmedControl however this will not work in our scenario.

As well, it doesn't appear we have a version where Target Audiences are supported.

I've tried googling around, but haven't had much luck finding the answer. Is there some way to amend the SPSecurityTrimmedControl class to check for group privileges rather than targeting an actual role and restrict content like such?

Thank you

0

You have to configure user profile service application to enable audience targeting.

You can use a content editor web part and point it to a text file which holds the content you want to display.

If you have target audience set up, you can set it in the content editor web part.

If you do not have target audience, give unique permissions to the text file so that only the group you wanted has access.

But, that would give a access denied message on the page(it will not redirect them to access denied page) to the other users. Use javascript/jQuery to hide the content editor web part for users without access.

2
  • Let's say configuring user profile service application to enable audience targeting is not an option for me. What are some other options?
    – friedfishy
    Jan 14 '16 at 20:33
  • Read my reply completely for other options. You can still use content editor web part with jQuery Jan 14 '16 at 20:34
0

Since you can't use audiences you can achieve this using JSCOM. Basically what you do is you grab the groups that exist in the site. If the current user is within that group you can then add the content. I am not sure how you are adding the content, but you are going to have to add it within a container that you can access via jquery. I have done this in the past with a CEWP or a list viewer web part. It all depends on what you are trying to surface. Once you add the content to the page, use the browser developer tools to determine the element to access to hide and load. If you set the web part to hidden, it will load hidden, but you can change the attribute to show the content if the user is in the group.

To check if the user is in the SharePoint group add the following code:

function IsUserInSPGroup(spGroupName, OnComplete) {

    //get the context and build the web objects
    var currCtx = new SP.ClientContext.get_current();
    var spWeb = currCtx.get_web();

    //Get the current user object
    var currentUser = currCtx.get_web().get_currentUser();
    currCtx.load(currentUser);

    //Get all the groups within the web
    var spGroupList = spWeb.get_siteGroups();
    currCtx.load(spGroupList);

    //Get the group we need to check against.
    var spGroup = spGroupList.getByName(spGroupName);
    currCtx.load(spGroup);

    //Get the users within the group
    var usersInGroup = spGroup.get_users();
    currCtx.load(usersInGroup);
    currCtx.executeQueryAsync(OnSuccess,OnFailure);

    //loop through each user and determine if current user is part of the list.
    function OnSuccess(sender, args) {
        var isUserInGroup = false;
        var userEnumerator = usersInGroup.getEnumerator();
        while (userEnumerator.moveNext()) {
            var currentGroupUser = userEnumerator.get_current();
            if (currentGroupUser.get_id() == currentUser.get_id()) {
                isUserInGroup = true;
                break;
            }
        }  
        OnComplete(isUserInGroup);
    }

    function OnFailure(sender, args) {
        OnComplete(false);
    }    
}

Then you can call and use the function as follows

IsUserInSPGroup("CustomDataGroup", function (userInGroup) {
  if(userInGroup)
  {
     //code to display your element
  }
});

Hope this helps.

2
  • Thank you - this worked like a charm! Question - how is this on system resources if you have a large user base?
    – friedfishy
    Jan 15 '16 at 16:04
  • Guess it depends. Your user base isn't going to matter unless they are all within the group you are checking. Also, this is completely client side so you won't be taxing the SP system resources. Other than transferring the data to the client, all the work is being done on the client side. If you have a lot of users to loop through the code will take some time, but that is to be expected. Jan 15 '16 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.