It's pretty easy to share document:

string link = context.Web.CreateAnonymousLinkForDocument("https://tenantname.sharepoint.com/Documents/sample.docx",ExternalSharingDocumentOption.View);

SharingResult result = context.Web.ShareDocument("https://tenantname.sharepoint.com/Documents/sample.docx", "someone@example.com", ExternalSharingDocumentOption.View,true, "Doc shared programmatically");

But unfortunately it's not enough for me. I need to specify shared link type. But I have no idea hot i supposed to do this in code behind. However it's pretty easy to achieve by UI UI or by ps(see for details):

$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)

$spoTenant= New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($Ctx) 
$spoTenant.DefaultSharingLinkType= [Microsoft.Online.SharePoint.TenantManagement.SharingLinkType]::$SharingLinkType

Moreover I've tried this code:

Office365Tenant tenant = new Office365Tenant(context.Web.Context);
tenant.DefaultSharingLinkType = SharingLinkType.Internal;

SharingResult result = context.Web.ShareDocument("https://tenantname.sharepoint.com/Documents/sample.docx", "someone@example.com", ExternalSharingDocumentOption.View,true, "Doc shared programmatically");

But no success.

UPDATE. Another one way which I've found recently is using microsoft graph Just one thing you have to do is specify scope for new link:

POST /me/drive/items/{item-id}/createLink
Content-type: application/json

  "type": "view",
  "scope": "anonymous"

Is there any alternatives in csom ?

  • So, you want the CSOM C# equivalent to the PS script ? – Gautam Sheth Jun 7 '18 at 16:07
  • now I would like to emphasize that I can't specify scope for shared link in csom. However it's possible by microsoft graph. – isxaker Jun 7 '18 at 16:09
  • sorry, do you want to create and share any anonymous link for a document ? – Gautam Sheth Jun 7 '18 at 16:10
  • No I need to create a link to all people on organization. I mean scope of this link must be - organization. But using pnp core it's only possible to create an anonymous link or call ShareDocument which takes only one email. – isxaker Jun 7 '18 at 16:14
  • sorry i didnt understand it much :( , how would you go about doing that via the UI ? Which section of the link you posted should i check ? – Gautam Sheth Jun 7 '18 at 16:16

You can use the native CSOM's CreateOrganizationSharingLink method to get the URL of the document.

You need to specify the full URL of the document in the parameter.

If you want the edit link, keep the 3rd parameter value set to true, else change it to false if you want the view link.

The sample code is as below:

var orgEditLink = Microsoft.SharePoint.Client.Web.CreateOrganizationSharingLink(context, 
"https://tenant-name.sharepoint.com/sites/Test/Documents/abcd.docx", true);


var editUrl = orgEditLink.Value;

Reference - CreateOrganizationSharingLink

  • btw, do you know what exactly method ShareDocument() does ? I mean CreateOrganizationSharingLink()/CreateAnonymousLinkForDocument() it's quite clear, because it returns created link's url. But ShareDocument() doesn't return any shared link's url. Can you clarify difference between ShareDocument() and CreateOrganizationSharingLink/CreateAnonymousLinkForDocument() please. – isxaker Jun 8 '18 at 16:43

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.