0
foreach (ListItem item in items)
{
    var sharingInfo = ObjectSharingInformation.GetObjectSharingInformation(cxt, item, true, true, true, true, false, false, false);
    ctx.Load(sharingInfo, si => si.AnonymousViewLink, si => si.AnonymousEditLink);
    ctx.ExecuteQuery();
    if (String.IsNullOrEmpty(sharingInfo.AnonymousEditLink) == false)
    {
        // NOW HOW TO REMOVE IT?
    }
}
1

With a bit more digging, I found a way to remove an anonymous link: Sharepoint Online: How to unshare public links (AnonymousEdit/View links) via REST API using the Web.DeleteAnonymousLinkForObject method.

0

Disabling/removing the link is associated with the RoleAssignments of an item. If we create anonymous links for files, then SharePoint creates an unique permission to that file, so if we want to remove the anonymous links, then we need to remove the unique role assignment of that item. The recommended way to remove all sharing links on a document library is to delete the unique permissions on the document. Doing this not only removes the unique permissions but also deletes all the sharing links on the document. The below snippet can be used to find all documents with unique permissions and reset the permissions, and in turn remove all the sharing links.

ListItemCollection docs = ctx.Web.Lists.GetByTitle(libTitle).GetItems(CamlQuery.CreateAllItemsQuery());  
ctx.Load(docs, i => i.Include(u => u.HasUniqueRoleAssignments));  
ctx.ExecuteQuery();    
foreach (var doc in docs)  
{  
    if (doc.HasUniqueRoleAssignments)  
    {  
        // Reset Unique permissions. This will also delete the sharing links of the document  
        doc.ResetRoleInheritance();  
    }  
}  

Use the below code,

foreach (ListItem item in items)
{
    if (ListItem.HasUniqueRoleAssignments)  
        {  
            // Reset Unique permissions. This will also delete the sharing links of the document  
            ListItem.ResetRoleInheritance();  
        }  
}

For more information, refer the below links,

  1. Generating Sharing Links Report And Removing Sharing Links Using SharePoint Online CSOM

  2. Creating anonymous guest link to images/documents programmatically Sharepoint Online O365

6
  • Thanks for the detailed instructions Karthik. My only concern is that, besides the anonymous links, there may be unique permissions on the files as a result of users sharing the file with specific users. We don't want to lose those permissions. – Brian M Jan 8 '20 at 7:58
  • but that's how Microsoft has designed it. If you find my answer useful, then please upvote and mark it as answer, so that it will be helpful to others too – Karthik Jaganathan Jan 8 '20 at 9:16
  • I understand that removing unique permissions from a document will get rid of all sharing links, but that's not what I want to do. Is there any way to remove anonymous links only? – Brian M Jan 9 '20 at 2:02
  • The question was for csom, that's why provide csom approach. The answer you provide is pointing to jsom. – Karthik Jaganathan Jan 9 '20 at 2:51
  • Hi Karthik. Good point. Thankfully the method is also available with CSOM. – Brian M Jan 9 '20 at 8:59

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.