Feb 15 2021 02:41 AM
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?
Feb 15 2021 08:31 AM - edited Feb 15 2021 08:43 AM
Feb 15 2021 08:31 AM - edited Feb 15 2021 08:43 AM
SolutionI 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;
}
Aug 03 2021 03:54 AM
Aug 17 2021 03:08 AM
@sensaba have a look here ExternalSharingDocumentOption C# (CSharp) Code Examples - HotExamples
Microsoft.SharePoint.Client.Web.ShareObject