1

I'm trying to get user names from List and for those user roles i need to Retrieve from Site using CSOM. Please help me if any one have idea i have written the below code

RoleAssignmentCollection roleAssignment = currentweb.RoleAssignments; 

not initializing..

using (ClientContext context = new ClientContext(URL))
            {
                currentweb= context.Web; 
                try
                {

                    List list = context.Web.Lists.GetByTitle("Team & Contacts");
                    CamlQuery query = new CamlQuery();
                    query.ViewXml = "<View>"
                         + "<Query>"
                         + "<Where><Eq><FieldRef Name='Team' /><Value Type='Choice'>Core Team</Value></Eq></Where>"
                         + "</Query>"
                         + "</View>";

                    ListItemCollection itemsContacts = list.GetItems(query);

                    context.Load(list);
                    context.Load(itemsContacts);
                    context.ExecuteQuery();

                    DataTable dtContacts = new DataTable();
                    DataTable dtdistinct = new DataTable();
                    dtContacts.Columns.Add("Employee Name");
                    dtContacts.Columns.Add("Login Name");
                    dtContacts.Columns.Add("Team");
foreach (ListItem item in itemsContacts)
                    {
                        context.Load(item);

                        FieldUserValue singleValue = (FieldUserValue)item["Employee_x0020_Name"];
                        User user = context.Web.EnsureUser(singleValue.LookupValue);
                        context.Load(user);
                        context.ExecuteQuery();

                        string userName = singleValue.LookupValue.ToString();

                        userName = user.LoginName;

RoleAssignmentCollection roleAssignment = currentweb.RoleAssignments; 
                        for (int i = roleAssignment.Count - 1; i >= 0; i--)
                        {
                            RoleAssignment roleAssignmentSingle = roleAssignment[i];
                            System.Type t = roleAssignmentSingle.Member.GetType();
                            //Principal signleUser = (Principal)userName;
                            if (t.Name == userName)
                            {

                            }

                        }
 dtContacts.Rows.Add(userName, item["Role"], item["Team"]);

                    }  
                    dtgvContacts.DataSource = dtContacts;
                } 

                catch (Exception ex)
                {

                }

1 Answer 1

1

This happens because web has not been loaded. Load the web i.e. context.Load(currentweb);. Finally Load the RoleAssignmentCollection and then execute query before looping through them. Example:

RoleAssignmentCollection roleAssignment = currentweb.RoleAssignments; 
context.Load(roleAssignment);
context.ExecuteQuery();
for (int i = roleAssignment.Count - 1; i >= 0; i--)
1
  • Hi Role assignment collection loading I need to get Roles for specific user . please help me how can I get RoRoles for specific User Feb 21, 2015 at 2:42

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.