0

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", "[email protected]", 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)
$ctx.Load($ctx.Web)
$ctx.ExecuteQuery()

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

Moreover I've tried this code:

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

SharingResult result = context.Web.ShareDocument("https://tenantname.sharepoint.com/Documents/sample.docx", "[email protected]", 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 ?

6
  • So, you want the CSOM C# equivalent to the PS script ? Jun 7, 2018 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, 2018 at 16:09
  • sorry, do you want to create and share any anonymous link for a document ? Jun 7, 2018 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, 2018 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 ? Jun 7, 2018 at 16:16

1 Answer 1

1

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);

context.ExecuteQuery();

var editUrl = orgEditLink.Value;

Reference - CreateOrganizationSharingLink

1
  • 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, 2018 at 16:43

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.