3

Had a question regarding an AJAX request I'm making via jQuery in my SP site page template.

The code in question:

    //writes CSS classes to body script based on explicitly checked group membership    
function checkGroupMember(groupName) {
    var requestHeaders = { "accept" : "application/json; odata=verbose" };

    $.ajax({
        url : _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getbyname('" + groupName + "')/CanCurrentUserViewMembership",
        method: 'GET',
        headers: { "Accept": "application/json; odata=verbose" },
        success: function(results) {
            if (results.d.CanCurrentUserViewMembership) {
                console.log('The user has access to: ' + groupName);
                $("body").addClass("group-" + groupName); 

            } else {
                console.log('The user DOES NOT have access to: ' + groupName);
            }
        },
        error: function (data) {
            console.log('Failed');
        }
    });
}

// New Function to Check Group Membership via Can Access Member List boolean
checkGroupMember('ric-AETEmployees');
checkGroupMember('ric-AETContractors');
checkGroupMember('ric-SARRestricted');
checkGroupMember('ric-QCUsers');
checkGroupMember('ric-QCRestricted');
checkGroupMember('ric-TIEUsers');
checkGroupMember('ric-TIEGNSUsers');
checkGroupMember('ric-TIEFullAccess');

I'm using the CanCurrentUserViewMembership checkbox to determine if a user belongs to the group, since the groups contain a mix of users directly added to SP and some that are members of a larger Active Directory group. (Yes, I know it's ugly...) All groups mentioned below are set up so that only members can see the membership list, as opposed to everyone.

My question: I've noticed that some users are experiencing a problem where not all eight checks are performed on Document.Ready. Did a spot check of the log results and sometimes there are four or five results instead of eight.

Is there a better way to call that script that will ensure that all eight checks are run each time the page is loaded?

Thanks, -- Steve

5
  • Is your site using the minimal download strategy feature? Jul 20, 2015 at 20:36
  • Good question, Eric. Not sure how I would find out. (SP off-prem, and am definitely not the admin for the farm.) Jul 20, 2015 at 21:36
  • Would encompassing the requests in the following syntax help at all? ExecuteOrDelayUntilScriptLoaded(GetStudentsData, "sp.js"); Jul 20, 2015 at 21:44
  • You'd have a url like teams/eric/_layouts/15/start.aspx#/SitePages/Home.aspx if MDS was being used, and yes wrapping them all in a function like that would help Jul 21, 2015 at 1:19
  • Thanks Eric. MDS is not being used, but wrapping the AJAX calls in a code snippet that delays the execution until sp.js is executed did the trick. Jul 21, 2015 at 17:16

2 Answers 2

1

For clarity, this is how I addressed the problem above. Wrapping the function calls in a function that explicitly waits until the client context is loaded helped to delay the AJAX calls long enough to make sure that they all were executed successfully. No need to use async:false in the AJAX.

//writes CSS classes to body script based on explicitly checked group membership    
function checkGroupMember(groupName) {
    var requestHeaders = { "accept" : "application/json; odata=verbose" };

    $.ajax({
        url : _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getbyname('" + groupName + "')/CanCurrentUserViewMembership",
        method: 'GET',
        headers: { "Accept": "application/json; odata=verbose" },
        success: function(results) {
            if (results.d.CanCurrentUserViewMembership) {
                console.log('The user has access to: ' + groupName);
                $("body").addClass("group-" + groupName); 

            } else {
                console.log('The user DOES NOT have access to: ' + groupName);
            }
        },
        error: function (data) {
            console.log('Failed');
        }
    });
}

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { 
    checkGroupMember('ric-AETEmployees');
    checkGroupMember('ric-AETContractors');
    checkGroupMember('ric-SARRestricted');
    checkGroupMember('ric-QCUsers');
    checkGroupMember('ric-QCRestricted');
    checkGroupMember('ric-TIEUsers');
    checkGroupMember('ric-TIEGNSUsers');
    checkGroupMember('ric-TIEFullAccess');
});


jQuery(document).ready(function($){
 // do some more jQuery stuff here 
});
0

see if you can use 'async : false' in your ajax call and verify the logs to validate that the function is being called for all the 8 users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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