0

I need to check whether Current User is a member of a group [in my case Admin Team] or Item owner ie item is assigned to him as a "Project Owner"[sharepoint field] . If the user doesn't belong to any of these then, popup message should be displayed on page load.

Update: I tried following two approaches , but now I dont know how to combine them and check. Please help! 1. To find if user belongs to a group and perform action accordingly:-

$().SPServices({
        operation:"GetGroupCollectionFromUser",
        userLoginName:$().SPServices.SPGetCurrentUser(),
        async: false,
        completefunc: function(xData, Status){
        var xml = xData.responseText;
        var isUserInAdminTeam = "False";
        if(xml.toLowerCase().indexOf('admin team') != -1)
                    {
                        isUserInAdminTeam = "True";
                    }                                                                   
        if(isUserInAdminTeam = "True")
                    { alert("Message");}
}})

2.To find current user Login name and compare with Project Owner field:

$(document).ready(function() {
    var userdetails=  $().SPServices.SPGetCurrentUser(
    {
        fieldNames:["ID","EMail","UserName","FirstName","LastName","Title"],
        debug:false
    }
);
var ProjectOwner = document.getElementById('ctl00_ctl26_g_bf8acf94_6c2e_4b91_9a48_6cda14aa2dc7_ff391_ctl00_ctl00_UserField_upLevelDiv').textContent;
if ( ProjectOwner != userdetails.Title) { alert("Access denied");})
  • Insufficient information. Please share what approach you are using? CSOM, SSOM,or JSOM,OOTB? – Trimantra Software Solution May 26 '17 at 9:49
  • 1
    Hello, welcome to the community :) .Can you tell us what have you tried till now ? Did you google something related to checking the group membership via JSOM/REST ? – Gautam Sheth May 26 '17 at 9:51
  • @GautamSheth Thankyou :) I have updated the question with approaches I used. Both work fine separately used. Can you help me in combining the two approaches. Please – Sanjana Chhabira May 26 '17 at 11:40
1

I am assuming your end goal is the user in the people field is current user. If this condition is true then you need to check the current user is in the "Admin Team" SharePoint group. If this is what you are trying to achieve you can follow steps below which is working for our solution.

I am using REST API which is our best way of making client side calls.

//function to get user details from people field

function getUserLoginNameFromPeopleField(peoplePickerId){
    var loginName = '';
    var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerId + "_TopSpan"];
    var users = peoplePicker.GetAllUserInfo();
    if(users.length > 0) {
        loginName = users[0].Key;
    }

    return loginName;
}

//load current user and his associated groups

function getCurrentUser(success, fail) {
    var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/CurrentUser?$select=*,Groups/Title&$expand=Groups";
    $.ajax({
        url: url,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            success(data);              
        },
        error: function (data) {
            fail(data);
        }
    });
}

//check your conditions

$(document).ready(function() {
    getCurrentUser(function(data) {
       var loginName = data.d.LoginName;
       var peopleFieldUserName = getUserLoginNameFromPeopleField("UserField"); //this is people field id

       if(loginName == peopleFieldUserName) {
         var userGroups = data.d.Groups;
         //loop through all groups titles and check "Admin Team" group exists
       } else {
        alert("Current user is not a manager");
       }

    }, function(error) {
        alert("Failed to load current user");
    }

});

Let me know if you need additional explanation

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.