1

I have shared a folder/document to an external user (outside the organization) in SharePoint online doc library. An email to the new user is shared with One time passcode to access the file. Now, the requirement is to Un-Share the document only for that user programatically. PLease suggest me the right API and sample code piece to help me in this regard. Any help as CSOM, REST API is appreciated TIA!

PS: I am using .NET CSOM apis and I am not sharing anonymous links to external user

1 Answer 1

0

here is the script. Haven't tested. You can add sub-sites too in another foreach.

string WebUrl = "https://company.sharepoint.com/sites/web";
string UserLoginName = " i:0#.f|membership|name.surname_domain.com#EXT#@microsoft.com";

public static void RemoveUserPermissions(string SiteUrl, string UserLoginName)
{
    using (var ctx = new ClientContext(SiteUrl))
    {
        var Web = ctx.Web;
        var Lists = Web.Lists;
        ctx.Load(ctx.Web);
        ctx.Load(Lists);
        ctx.ExecuteQuery();

        Console.WriteLine(string.Format("Removing user {0} from Site: {1}", UserLoginName, SiteUrl));
        var User = Web.SiteUsers.GetByLoginName(UserLoginName);
        Web.RoleAssignments.GetByPrincipal(User).DeleteObject();
        ctx.ExecuteQuery();

        foreach (var List in Lists)
        {
            var UniqueListPerm = List.HasUniqueRoleAssignments;
            ctx.Load(UniqueListPerm);
            ctx.ExecuteQuery();
            if(UniqueListPerm)
            {
                Console.WriteLine(string.Format("Removing user from List or Library: {0}", UserLoginName, List.Title));
                List.RoleAssignments.GetByPrincipal(User).DeleteObject();
                ctx.ExecuteQuery();
            }

            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View><Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>";

            var Items = list.GetItems(camlQuery);
            ctx.Load(Items);
            ctx.ExecuteQuery();
            foreach (var Item in Items)
            {
                var UniqueItemPerm = Item.HasUniqueRoleAssignments;
                ctx.Load(UniqueItemPerm);
                ctx.ExecuteQuery();

                if (UniqueItemPerm)
                {
                    Console.WriteLine(string.Format("Removing user from Library {0} and Item: {1}", List.Title, Item.Id));
                    Item.RoleAssignments.GetByPrincipal(User).DeleteObject();
                    ctx.ExecuteQuery();
                }
            }
        }
    }
}
10
  • Are you sure it will work for external user viz. "[email protected]"? Also, why we have to remove user from Site Level (external user to be removed only from a document)? sorry..... it's just a thought
    – DvG
    Jan 21, 2019 at 6:43
  • Also, what you suggest to put at 2nd line - string UserLoginName = " i:0#.f|membership|name.surname_domain.com#EXT#@microsoft.com"; If the user is [email protected]
    – DvG
    Jan 21, 2019 at 6:49
  • 1
    for this kind of user will be created a ext account in your tenant like : i:0#.f|membership|john_gmail.com#EXT#@yourdomain.onmicrosoft.com do you understand? Jan 21, 2019 at 7:36
  • @zdenek this didnt worked for me... Throwing exception at List.RoleAssignments.GetByPrincipal(User).DeleteObject(); as User does not exist
    – DvG
    Jan 23, 2019 at 6:49
  • so get var User = Web.SiteUsers and find like john maybe its in different format. Try find john_gmail.com#EXT#@yourdomain.onmicrosoft.com dont forget change "yourdomain.onmicrosoft.com" to your default O365 domain. Jan 23, 2019 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.