10
 var page = context.Web.GetFileByServerRelativeUrl(FileName);
 context.ExecuteQuery();

When this is executing I am getting an error:

Specified value is not supported for the serverRelativePath parameter

I am giving full file path in this call.

2 Answers 2

9

Use ServerRelativeUrl, eq. /sites/site/libraryname/folder/filename.ext. Function returned File object.

2
  • are you saying that instead of https://<companyName>.sharepoint.com/MyLIb/MyFolder/MyFile.txt we have to mention only path like ('companyName.sharepoint.com/MyLib/MyFolder/MyFile.txt')?
    – TechTurtle
    Commented Feb 12, 2019 at 20:15
  • In your case /MyLib/MyFolder/MyFile.txt will be the ServerRelativeUrl. See my answer below.
    – Pavel Sem
    Commented Apr 17, 2020 at 9:56
7

ServerRelativeUrl is everything after domain name but needs to start with /.

// url -> https://microsoft.sharepoint.com/teams/<sitename>/<libraryname>

var serverRelUrl = context.Web.ServerRelativeUrl;
// returns /teams/<sitename>

So for file it will be in above sample /teams/<sitename>/<libraryname>/folder/subfolder/file.txt

2
  • 1
    A huge piece of this for me was that it needed to start with /.
    – Mike
    Commented Jun 11, 2021 at 10:13
  • me too, a lot head-scratching on that one!
    – James
    Commented Dec 10, 2021 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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