1

I am currently trying to write an application using C# that accesses a file stored on a SharePoint server programatically using the REST API. For that purpose, I want to check out a file. The file exists at this path:

http://myserver.mydomain.com/sites/MyCompany/012_MyDepartment/Shared%20Documents/rm/MyToolConfig/config.xml

When I paste this line in my browser, I see the file.

Next thing I do is to use REST like this:

http://myserver.mydomain.com/_api/web/GetFileByServerRelativeUrl('/sites/MyCompany/012_MyDepartment/Shared%20Documents/rm/MyToolConfig/config.xml')/CheckOut()

According to the REST-documentation, this should work. However, when I paste this path to my browser, I get the following error:

<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code>-2146232832, Microsoft.SharePoint.SPException</m:code>
<m:message xml:lang="en-US">
The file /sites/MyCompany/012_MyDepartment/Shared%20Documents/rm/MyToolConfig/config.xml does not exist.
</m:message>
</m:error>

So the question is: is it possible that the server maps the physical path of the file internally to another location? How can I figure out what the correct path is that needs to be passed to the function "GetFileByServerRelativeUrl"?

Please note that I do not administrate that SharePoint server, I only have to use it as a "black box" to store and get files from.

EDIT: Here is some sample code I currently use:

const string serverName = @"http://myserver.mydomain.com/sites/MyCompany/_api/web/GetFileByServerRelativeUrl('/sites/MyCompany/012_MyDepartment/Shared%20Documents/rm/MyToolConfig/config.xml')/CheckOut()";            
HttpWebRequest req = WebRequest.Create(serverName) as HttpWebRequest;
req.Method = "POST";
req.Credentials = CredentialCache.DefaultCredentials; 
req.Headers.Add("X-RequestDigest", GetFormDigest());
req.ContentLength = 0;
HttpWebResponse myWebResponse = (HttpWebResponse)req.GetResponse();
1
  • Try doing a REST query on the document library, /_api/web/lists/getbytitle('Shared Documents')/items. Find the list item pertaining to your document, and it should tell you the REST request for it's file.
    – wjervis
    Jun 15, 2015 at 11:36

1 Answer 1

1

You should do it like this:

http://myserver.mydomain.com/sites/MyCompany/_api/web/GetFileByServerRelativeUrl('/sites/MyCompany/012_MyDepartment/Shared%20Documents/rm/MyToolConfig/config.xml')/CheckOut()

And Request should be POST.

Hopefully this helps.

4
  • Thanks for your answer, I tried this, but when I do it this way I get a 403 error (Forbidden), although authentication works basically. Any other idea why this fails? I have edited my start post with my C# code so you see that I use POST instead of GET already.
    – Erik
    Jun 15, 2015 at 12:25
  • First try this URL in your browser. If you're doing it in C# you'll first need to do something about authentication. Oauth. msdn.microsoft.com/en-us/magazine/dn198245.aspx Jun 15, 2015 at 14:03
  • In general I would recommend you to look at the PnP project provided by Microsoft - hosted on github. There are samples that'll be useful to you github.com/OfficeDev/PnP Jun 15, 2015 at 14:09
  • Is it possible that my FormDigestValue is wrong? It looks something like this when I get the String from GetFormDigest(): "0x78156822542D32C05156C84CEB8B03525C74106452223E51FF3B956C80AF1FC0901719279A28C9F4BA6F582F32484AC24472D47929DDDDFB1692D593F6790A71,17 Jun 2015 12:47:47 -0000" Is it correct, especially containing the Date value in the string?
    – Erik
    Jun 17, 2015 at 12:50

Your Answer

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

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