I am trying to get a file by relative url

Microsoft.SharePoint.Client.File home = web.GetFileByServerRelativeUrl(web.ServerRelativeUrl.EndsWith("/") ? web.ServerRelativeUrl : web.ServerRelativeUrl + "/" + folderName + filename);

It throws the below exception after the last line

Value does not fall within expected range The above code works fine for all site collections except root site collection

The values of the parameters are as below

web.ServerRelativeUrl = "/";
folderName = "_catalogs/masterpage/";
filename = "custom.master";

You had written condition statement combining with add string operation. Your statement missing wrapper and ultimately the conditional operation will return the same result for both i.e. either condition is true or false it will give same result.

Try following script:

Microsoft.SharePoint.Client.File home = web.GetFileByServerRelativeUrl((web.ServerRelativeUrl.EndsWith("/") ? web.ServerRelativeUrl : web.ServerRelativeUrl + "/") + folderName + filename);
  • 1
    it happen many a times while working with conditional statements. I always wrap my conditional statements to get exactly what I want. – ThinkB4Code Jun 1 '17 at 9:48

Try this:

Microsoft.SharePoint.Client.File home = ctx.Web.GetFileByServerRelativeUrl(web.ServerRelativeUrl +"/"+ folderName + "/"+filename);
  • thank you but i guess the / is already there in the parameters – Vignesh Subramanian Jun 1 '17 at 8:06
  • Did you try this? Same error? – harshal gite Jun 1 '17 at 8:07
  • got "File Not Found" error, the master page is there though – Vignesh Subramanian Jun 1 '17 at 8:09
  • try ctx.Web.ServerRelativeUrl instead of web.ServerRelativeUrl – harshal gite Jun 1 '17 at 8:13
  • Web web = ctx.Web, this is how it got declared bro – Vignesh Subramanian Jun 1 '17 at 8:19

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.