Hi @Pablo Glomby ,
I understand you want to share a file in a document library (szDocumentLibrary), I did following test:
- prepare a test file in a library
- Try below code:
}ClientContext clientContext = new ClientContext(SiteUrl); clientContext.Credentials = new SharePointOnlineCredentials(username, securestring); var web = clientContext.Web; var dig=clientContext.GetFormDigestDirect(); clientContext.Load(web); clientContext.ExecuteQuery(); Console.WriteLine($"{web.Title}--{dig.DigestValue}"); ShareFile(clientContext, clientContext.Credentials as SharePointOnlineCredentials,dig.DigestValue); Console.ReadKey(); } private static void ShareFile(ClientContext context, SharePointOnlineCredentials creds, string digest) { try { string szRequestURL = @"https://abc.sharepoint.com/sites/sbdev/_api/web/GetFileByUrl(@v)/ListItemAllFields/ShareLink?@v='/sites/sbdev/My test doc lib/license.txt'"; string response = ""; using (var client = new WebClient()) { client.Credentials = creds; client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose"); client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose"); client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0"); client.Headers.Add("X-RequestDigest", digest); var endpointUri = new Uri(szRequestURL); response = client.UploadString(szRequestURL, "{\"request\":{\"createLink\":true,\"settings\":{\"allowAnonymousAccess\":true,\"linkKind\":2,\"expiration\":null,\"restrictShareMembership\":false,\"updatePassword\":false,\"password\":\"\", \"description\":\"My description\", \"role\":8,\"applicationLink\":false,\"limitUseToApplication\":false}}}"); dynamic dynObj = JsonConvert.DeserializeObject(response); if (dynObj == null) { return; } string szURL = dynObj.d.ShareLink.sharingLinkInfo.Url.Value; Console.WriteLine("Sharing link: " + szURL); } } catch (Exception e) { Console.WriteLine("Error sharing files: " + e.ToString()); } } }
Result:
Could you please have a try?
Best Regards,
Baker Kong
If the response is helpful, please click "Accept Answer" and upvote it.