SOLVED

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

Copper 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?

 

 

3 Replies
best response confirmed by RoderickKranendonk (Copper 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;
}

Hi 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.
1 best response

Accepted Solutions
best response confirmed by RoderickKranendonk (Copper 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;
}

View solution in original post