I am using Microsoft.SharePoint.Client to work with SharePoint 2013 files in asp.net MVC project. I am able to load file then execute the query but after loading, i have to checkout file. on checkout I am getting following error:

The URL is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web.

I created this file via asp.net MVC project only. its fine while creating a file on Sharepoint via code. I also checked out that file from SharePoint directly. its working fine there.

Note:- File which needs to be checkout is under subsite.

any help would be highly appreciable.

following is my sample code:

 //siteUrl = xxx.com
 //subsite = acct
 //relUrl =  /acct/doclib/test.txt
using (ClientContext ctx = getContext(siteUrl))
        var site = ctx.Web;
        var file = site.GetFileByServerRelativeUrl(relUrl);
            if (file.CheckOutType == CheckOutType.None)
                ctx.ExecuteQuery(); // this line is causing error
                return true;

Loading the file works with a non-existing file as well.

Try checking if the file exists with file.Exists before performing any other action.

See: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.file.exists.aspx

| improve this answer | |
  • i am accessing sharepoint online and file is there. i am able to checkout there but not through code. – Ravi Anand Oct 17 '16 at 13:59
  • i used file.Exists method and i am getting true. any other suggestions? – Ravi Anand Oct 17 '16 at 14:11

You need to access the subsite before accessing the file:

using (var ctx = new ClientContext("https://sitecollectionurl.com/acct"))
  var file = ctx.Web.GetFileByServerRelativeUrl("/acct/doclib/test.txt");
| improve this answer | |

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.