I want to generate a anonymous view link for 12k files on my SharePoint Online. I use microsoft.sharepoint.online.csom. On an Internet I found a method ctx.Web.CreateAnonymousLinkForDocument but it doesn't appear in my case. Do you have any idea how to do this?


CreateAnonymousLinkForDocument is a PnP Extension method to generate anonymous links.

It wraps the UpdateDocumentSharingInfo method of DocumentSharingManager and generates the Anon links.

You dont need to necessarily use PnP to generate anon links.

You can directly use the OOTB CSOM methods of Microsoft.SharePoint.Client.Sharing.DocumentSharingManager class and generate links as below:

var users = new List<string>() { "user1@domain.com", "user2@domain.com" };

var userRoles = new List<UserRoleAssignment>();
foreach (var user in users)
    UserRoleAssignment role = new UserRoleAssignment();
    role.UserId = user;
    role.Role = Role.View; //Role.Edit

string absoluteFileUrl = "https://tenant.sharepoint.com/sites/test/Documents/Document.docx";
DocumentSharingManager.UpdateDocumentSharingInfo(context, absoluteFileUrl, userRoles, true, true, true, "Your document", true, true);

You just need to add the following using statement:

using Microsoft.SharePoint.Client.Sharing;

Reference - DocumentSharingManager.UpdateDocumentSharingInfo method

  • Thanks! Does exist user like "All" or "Public"? – FromSomewhere Apr 6 '18 at 7:28
  • sorry didn't get you ? can you be more clear ? This will share the links with the users you have mentioned. – Gautam Sheth Apr 6 '18 at 8:01
  • 1
    Think he thinks about just sharing with Anyone, People in "company" etc – Anders Aune Apr 6 '18 at 8:24

That method is part of the PNP client framework.


You will need to reference the OfficeDevPnP.Core.dll to use it. ( https://github.com/SharePoint/PnP )

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.