Forum Discussion
RoderickKranendonk
Feb 15, 2021Copper Contributor
How to share programmatically (C#) Sharepoint Document with AnonymousLink with Block Download?
I want to share a document with an AnonymousLink with Block Download. GetObjectSharingInformation SharingLinks shows BlocksDownload but is readOnly. With Microsoft.SharePoint.Client.Web.CreateAnonymousLink you cannot set BlocksDownload or RestrictedView. RestrictedView role only seems to work with user and not anonymously. ExternalSharingDocumentOption only has options View or Edit (I tried CreateAnonymousLinkWithExpirationForDocument). Here they seem to suggest that there is also a third option (7 = restricted view?): https://powerusers.microsoft.com/t5/Power-Automate-Ideas/Create-sharing-link-for-a-file-or-folder-in-SharePoint-for/idi-p/194595/page/2#comments
Is there a way to override ExternalSharingDocumentOption and add RestrictedView = 7 and will that work? Or does anyone know how to set BlocksDownload to true?
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;
}
3 Replies
Sort By
- RoderickKranendonkCopper Contributor
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;
}- sensabaCopper ContributorHi RoderickKranendonk,
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.- RoderickKranendonkCopper Contributor
sensaba have a look here ExternalSharingDocumentOption C# (CSharp) Code Examples - HotExamples
Microsoft.SharePoint.Client.Web.ShareObject