I have a CSOM Script (Below) that I am using to delete a user from a SharePoint Group. But in some of the site the users are added directly to the site.

Can you please let me know, how I can remove a user who is not part of any Group?

I have to run this on each Sub Site in a site collection.

ClientContext clientContext = new ClientContext(siteUrl);
        Web web = clientContext.Web;   

        GroupCreationInformation groupCreationInfo = new GroupCreationInformation();
        groupCreationInfo.Title = "Custom Group";
        groupCreationInfo.Description="Custom group created using Client Object Model";

        User owner = web.EnsureUser(@"domainName\ownerName");
        User member = web.EnsureUser(@"domainName\userName");

        Group group = web.SiteGroups.Add(groupCreationInfo);
        group.Owner = owner;

  • All the users and user groups are maintained on the top level site collection. I think you don't need to run the code on each subsite. Apr 18, 2016 at 11:49
  • You want to remove user or his permissions? Apr 18, 2016 at 13:56
  • I want to remove the user .The scenario is we have broken inheritance at subsite and the site owners started adding users directly to the site instead to the SharePoint Groups at the sub site .So i want to remove the users added directly and add them to the SharePoint Groups
    – Anil
    Apr 18, 2016 at 17:10

1 Answer 1


I had a similar issue where I had to just remove a specific user from the site (Web). Following code did it:

var roleAssignments = clientContext.Web.RoleAssignments;

ctx.Load(roleAssignments, r => r.Include(i => i.Member, i => i.RoleDefinitionBindings));

for(var i = 0; i < roleAssignments.Count; i++)
    var ra = roleAssignments[i];
    if(ra.Member is User && ((User)ra.Member).Title == "USER_TITLE_FOR_MY_SITUATION")


By extending this code, you can remove them from the site and add them to groups. Hope this helps! :)

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.