0

I am looking for some mechanism to remove a specific user from a ShareLink in SharePoint Online via Rest API, CSOM or PNP, whilst leaving the ShareLink active and available for other users.

I want to replicate the functionality thats available via the UI below.

enter image description here

1 Answer 1

0

you can try this using CSOM:

using (ClientContext ctx = new ClientContext(url))
                {
                    ctx.Credentials = new SharePointOnlineCredentials(Admin, password);
                    //get all list items
                    List list = ctx.Web.Lists.GetByTitle(listName);
                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"></View>";
                    ListItemCollection listItems = list.GetItems(camlQuery);
                    ctx.Load(listItems);
                    ctx.ExecuteQuery();

                    //get the user
                    User RemoveUser = ctx.Web.EnsureUser(username);

                    //get the list item
                    ListItem listItem = listItems.FirstOrDefault(t => t.DisplayName == FileName);
                    ctx.Load(listItem, lt => lt.HasUniqueRoleAssignments, lt => lt.RoleAssignments.Include(p => p.Member, p => p.RoleDefinitionBindings.Include(r => r.Name)));
                    ctx.ExecuteQuery();

                    //remove user
                    if(listItem.HasUniqueRoleAssignments)
                    {
                        foreach(RoleAssignment ra in listItem.RoleAssignments)
                        {
                            foreach(RoleDefinition rd in ra.RoleDefinitionBindings)
                            {
                                string[] member = ra.Member.LoginName.Split('\\');
                                if (member.Length > 1)
                                {
                                    string m = member[1];

                                    if (username == m)
                                    {
                                        ra.RoleDefinitionBindings.Remove(rd);
                                        ra.Update();                                        
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

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.