0

How can I get groups and users that has been assigned to Sharepoint List using Client Side Script.

Please help me to on this

2

The assigned users and groups to a list can be retrieved using RoleAssignments. Here is a sample code to list them.

CSOM C# sample

ClientContext clientContext = new ClientContext(siteUrl);
Web web = clientContext.Web;
clientContext.Load(web);
List list = web.Lists.GetByTitle(listNameTestCL);
var assignments = list.RoleAssignments;
clientContext.Load(assignments, ac => ac.Include(a => a.Member));
clientContext.ExecuteQuery();
var assignmentList = assignments.ToList();
foreach(var assignment in assignmentList)
{
    var strRoleDefinition = "";
    forreach(var roleDefinition in assignment.RoleDefinitionBindings)
    {
        strRoleDefinition += roleDefinition.Name + ", ";    
    }
    strRoleDefinition = strRoleDefinition.Trim().TrimEnd(',');
    Response.Write(assignment.Member.Title.ToString() + " -- " + assignment.Member.PrincipalType.ToString() + " -- " + strRoleDefinition);
}

JSOM sample

function listAllRoleAssignments(){
    var clientContext = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
    var webLists = clientContext.get_web().get_lists();
    var list = webLists.getByTitle("TestCL");
    clientContext.load(list);
    var assignments = list.get_roleAssignments();
    clientContext.load(assignments, 'Include(Member,RoleDefinitionBindings)');
    clientContext.executeQueryAsync(onSuccess, onFail);
    function onSuccess(){
        var assignmentsEnumerator =  assignments.getEnumerator();
        while (assignmentsEnumerator.moveNext()) {
            var assignment = assignmentsEnumerator.get_current();
            var roleDefinitionBindings = assignment.get_roleDefinitionBindings();

            var roleDefinition = getRoleDefinition(roleDefinitionBindings);
            console.log(assignment.get_member().get_title() +  " -- " + getPrincipalTypeName(assignment.get_member().get_principalType()) + " -- " + roleDefinition);
        }
    }
    function onFail(o, args){
        console.log("error: " + args.get_message());
    }

}
function getPrincipalTypeName(principalType){
    switch(principalType){
        case 0:
            return "None";
        case 1:
            return "User";
        case 2:
            return "Distribution List"; 
        case 4:
            return "Security Group";    
        case 8:
            return "SharePoint Group";                  
        default:
            return "All"
    }
}
function getRoleDefinition(roleDefinitionBindings){
    var strRoleDefinition = "";
    for(i=0; i<roleDefinitionBindings.get_count(); i++){
        strRoleDefinition += roleDefinitionBindings.getItemAtIndex(i).get_name() + ", ";
    }
    return strRoleDefinition.substring(0, strRoleDefinition.length - 2);
}
ExecuteOrDelayUntilScriptLoaded(listAllRoleAssignments, "SP.js");
  • Dear Boss can i have this one code for ecma script this one through the error – sanka jayaram Sep 14 '17 at 4:15
  • Boss can i have ECMA script above code... – sanka jayaram Sep 14 '17 at 4:20
  • @sankajayaram I have updated the JSOM sample in the answer. Hope that helps. – Ahmad Zia Sep 14 '17 at 5:31
  • You are the great boss .now it is working ..Thank you very much..If any thing i will msg you.. – sanka jayaram Sep 14 '17 at 6:20
  • Boss how can i get Role Defination for the group..any properties – sanka jayaram Sep 14 '17 at 7:12

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.