I recently implemented a JS/jQuery solution that checks for all of the SP groups to which a user belongs adds the group names classes to the body
tag of the pages I am building. It worked very well until I ran into the problem of users belonging to an Active Directory (AD) group within the SP group. The solution failed to recognize when a user wasn't explicitly added to the SP group but still "belonged" via the AD group.
In prowling around for another solution, I stumbled on this answer to a similar question, in which a workaround was suggested but untested. That workaround would look something like:
- Create a SP List
- Break list inheritance from other SP groups and only allow one group to access it.
- Attempt to access the list using the SP REST API
- On Success, add a class to the body tag
- On Failure, no nothing.
To do that, I am guessing that I would need to do something like the following. (Adapted from this post here.)
ExecuteOrDelayUntilScriptLoaded(CheckList, "sp.js");
function CheckList() {
context = new SP.ClientContext.get_current();
lstStudents = context.get_web().get_lists().getByTitle('MyListNameHere');
web = context.get_web();
currentUser = web.get_currentUser();
var groups = currentUser.get_groups();
context.load(currentUser);
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
Function.createDelegate(this, this.onFailureMethod));
}
function onSuccessMethod(sender, args) {
// Do something here that grabs an item from the list.
// If we get this far, the user has permission to access the list
// Add the group name to the body class.
}
function onFailureMethod()
{
// User does not have access to the SP list
// Therefore don't add the class to the body tag.
}
At this point, I'm kinda tapped out in my SP knowledge. Am hoping that someone can help me fill in the gaps of the function?