SOLVED

How to share programmatically (C#) Sharepoint Document with AnonymousLink with Block Download?

%3CLINGO-SUB%20id%3D%22lingo-sub-2135882%22%20slang%3D%22en-US%22%3EHow%20to%20share%20programmatically%20(C%23)%20Sharepoint%20Document%20with%20AnonymousLink%20with%20Block%20Download%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2135882%22%20slang%3D%22en-US%22%3E%3CP%3EI%20want%20to%20share%20a%20document%20with%20an%20AnonymousLink%20with%20Block%20Download.%26nbsp%3BGetObjectSharingInformation%20SharingLinks%20shows%20BlocksDownload%20but%20is%20readOnly.%20With%26nbsp%3BMicrosoft.SharePoint.Client.Web.CreateAnonymousLink%20you%20cannot%20set%20BlocksDownload%20or%20RestrictedView.%20RestrictedView%20role%20only%20seems%20to%20work%20with%20user%20and%20not%20anonymously.%26nbsp%3BExternalSharingDocumentOption%20only%20has%20options%20View%20or%20Edit%20(I%20tried%26nbsp%3BCreateAnonymousLinkWithExpirationForDocument).%20Here%20they%20seem%20to%20suggest%20that%20there%20is%20also%20a%20third%20option%20(7%20%3D%20restricted%20view%3F)%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fpowerusers.microsoft.com%2Ft5%2FPower-Automate-Ideas%2FCreate-sharing-link-for-a-file-or-folder-in-SharePoint-for%2Fidi-p%2F194595%2Fpage%2F2%23comments%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fpowerusers.microsoft.com%2Ft5%2FPower-Automate-Ideas%2FCreate-sharing-link-for-a-file-or-folder-in-SharePoint-for%2Fidi-p%2F194595%2Fpage%2F2%23comments%3C%2FA%3E%3C%2FP%3E%3CP%3EIs%20there%20a%20way%20to%20override%26nbsp%3BExternalSharingDocumentOption%20and%20add%20RestrictedView%20%3D%207%20and%20will%20that%20work%3F%20Or%20does%20anyone%20know%20how%20to%20set%20BlocksDownload%20to%20true%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

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...

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?

 

 

1 Reply
best response confirmed by RoderickKranendonk (New Contributor)
Solution

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