Forum Discussion
How to share programmatically (C#) Sharepoint Document with AnonymousLink with Block Download?
- Feb 15, 2021
I solved it... For those who are interested:
public static string FileSharePointAnonymousLink(string FilePath, bool IsDownloadable)
{ClientContext clientContext = new ClientContext(GetWebAddress(FilePath));
string accessToken = "Bearer " + InitilizerHelpers.accessTokenFactory.sharepointToken.GetAccessToken();
clientContext.ExecutingWebRequest += delegate (object oSender, WebRequestEventArgs webRequestEventArgs)
{
webRequestEventArgs.WebRequestExecutor.WebRequest.Headers.Add("Authorization", accessToken); // accessToken;
};
Web site = clientContext.Web;
var item = site.GetListItem(FilePath);
clientContext.Load(item);
clientContext.Load(item.ParentList);
//clientContext.ExecuteQuery();
clientContext.Load(site);
clientContext.ExecuteQuery();
var digest = clientContext.GetFormDigestDirect();
string szRequestURL = $"{GetWebAddress(FilePath)}/_api/Web/Lists(guid'{item.ParentList.Id}')/GetItemById(@a1)/ShareLink?@a1={item.Id}";string response = "";
using (var client = new WebClient())
{
client.Headers.Add(System.Net.HttpRequestHeader.Authorization, accessToken);
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.DigestValue);var endpointUri = new Uri(szRequestURL);
int role = IsDownloadable ? 1 : 7;
int linkKind = IsDownloadable ? (int)SharingLinkKind.AnonymousView : (int)SharingLinkKind.Flexible;
response = client.UploadString(szRequestURL, "{\"request\":{\"createLink\":true,\"settings\":{\"allowAnonymousAccess\":true,\"linkKind\":" + linkKind + ",\"expiration\":null,\"restrictShareMembership\":false,\"updatePassword\":false,\"password\":\"\", \"description\":\"My description\", \"role\":" + role + ",\"applicationLink\":false,\"limitUseToApplication\":false}}}");
dynamic dynObj = Newtonsoft.Json.JsonConvert.DeserializeObject(response);
if (dynObj == null)
{
return string.Empty;
}
string szURL = dynObj.d.ShareLink.sharingLinkInfo.Url.Value;return szURL;
}
I would need a similar approach for "Specific People" as part of SharePoint online. Can you guide me to the code base to achieve it using CSOM or PnP? Thank you.
sensaba have a look here https://csharp.hotexamples.com/examples/-/ExternalSharingDocumentOption/-/php-externalsharingdocumentoption-class-examples.html
Microsoft.SharePoint.Client.Web.ShareObject