I need to generate anonymous links to certain files, using C#, in order to give view access to them (or force user to download them once clicked on the link)

I only have access to the file relative path.

My credentials are stored in a config file and are used when calling the function Crud.GetSharepoint()

Here is my code snippet :

// Crud.GetSharepoint returns a ClientContext object
using (var spCtx = Crud.GetSharepoint())
                    foreach (string attachment in attachments)
                        if (spCtx.HasPendingRequest)
                        Microsoft.SharePoint.Client.File file = spCtx.Web.GetFileByServerRelativeUrl(attachment);
                        // Custom made function that someone advised to use because he coded it
                        // Would prefer using Sharepoint functions directly
                        links.Add(Crud.GetAnonymousLink(spCtx, file, 2, true));

This code works fine but I need to make some few improvements related on these points :

  • Url generation works great and my url ends with ?Download=1. Yet since the last update, not all files extensions get downloaded directly (everything that is viewable by your internet browser gets downloaded, otherwise you are redirected to a webpage where you can see the file)
  • When I use the Microsoft.SharePoint.Client.File object, load time get extended as files get bigger (is this normal behavior ?). I only need meta-infomation about the file, not it's content

Any suggestions to improve this ?

  1. The "Web.CreateAnonymousLink" is only available for SharePoint Online.
  2. You can try to fetch only the meta-data of the file, using CAML:
CamlQuery camlQuery = new CamlQuery();

camlQuery.ViewXml = "View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='ServerUrl'></FieldRef><Value Type='Text'>/sites/contoso/Shared Documents/5a8b6e84409589b59936f0fcadf7f99e.jpg</Value></Eq></Where></Query><ViewFields><FieldRef Name='Title' /><FieldRef Name='File_x0020_Size' /><FieldRef Name='File_x0020_Type' /><FieldRef Name='FileDirRef' /><FieldRef Name='FileLeafRef' /><FieldRef Name='FileRef' /><FieldRef Name='ServerUrl' /></ViewFields></View>";

ListItemCollection listCol = docList.GetItems(camlQuery);

Reference: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.web.createanonymouslink.aspx#

| improve this answer | |
  • 1. We are using sharepoint online 2. I will try your CAML query but i was wondering what is docList ? – rak007 May 23 '18 at 14:04
  • docList is List object – Anil Pal May 24 '18 at 5:00
  • @rak007 Great. That means you can use "Web.CreateAnonymousLink". For e.g. Web.CreateAnonymousLink(clientContext, "https://contoso.sharepoint.com/shared%20documents/1.png", false). You can get the base URL with spCtx.Url. You no longer have to access the object of the file, or the properties. – Purvin Desai May 24 '18 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.