3

I am trying to check out a file remotely using the Sharepoint libraries:

using Microsoft.SharePoint.Client;

public void CheckOutFile()
{
    // Set the context (URL) of the site 
    var cc = new ClientContext("http://MainSite/MySite");
    var file = cc.Web.GetFileByServerRelativeUrl("/DocumentLibraryName/Document%20Name.docx");
    file.CheckOut();
    cc.ExecuteQuery();
}

But when my code reaches the ExecuteQuery line, I am seeing this error:

"Value does not fall within the expected range."

I also tried this:

var cc = new ClientContext("http://MainSite/");
var file = cc.Web.GetFileByServerRelativeUrl("/MySite/DocumentLibraryName/Document%20Name.docx");   

But then I got another error:

"The URL MySite/DocumentLibraryName/Document%20Name.docx 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."

Can someone please point me in the right direction with this? Thank you

2 Answers 2

4

I guess you are close to the right point. In your first code-snippet you didn't provide a server-relative url for GetFileByServerRelativeUrl() (the "/MySite" part is missing). In your second snippet you do specifiy the server-relative url, but you're in the context of the RootWeb (this time the "/MySite" part is missing at new ClientContext(). Mixing it all together, following code should work for you:

using Microsoft.SharePoint.Client;

public void CheckOutFile()
{
    // Set the context (URL) of the site 
    var cc = new ClientContext("http://MainSite/MySite");
    var file = cc.Web.GetFileByServerRelativeUrl("/MySite/DocumentLibraryName/Document%20Name.docx");
    file.CheckOut();
    cc.ExecuteQuery();
}
4

If I am not wrong then I guess MySite in your url means /sites/yoursitename/. If its not like this then you must use.

So your line will be

var file = cc.Web.GetFileByServerRelativeUrl("/sites/MySite/DocumentLibraryName/Document%20Name.docx");

Instead of

var file = cc.Web.GetFileByServerRelativeUrl("/MySite/DocumentLibraryName/Document%20Name.docx");

Also your ClientContext Line should be

var cc = new ClientContext("http://MainSite/sites/yoursite");

as you have mentioned in your first code.

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.