Question

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");})
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top