3

I am trying to retrieve Images using the REST API from iOS/Android.

The issue I’m running into is when the images are stored in the /sites/siteName directory, the REST call does not work. Let me explain further:

My REST call: //tenant/_api/web/GetFileByServerRelativeUrl('/sites/siteName/PublishingImages/image.jpg')/$value

  • That call does not work. I get a Microsoft.SharePoint.SPException - The file /sites/siteName/PublishingImages/image.jpg does not exist.
  • The file does indeed exist because inside the web browser I can view the image using: //tenant.com/sites/siteName/PublishingImages/image.jpg

The REST call that does work is:

  • //tenant.com/_api/web/GetFileByServerRelativeUrl

    ('/News/PublishingImages/image.jpg')/$value

    • That returns the image as expected.

I’m not a SharePoint developer so my knowledge is very light. But, I’m confused to as why those calls do not work.

Any help is very appreciated.

Thanks

1 Answer 1

0

You can use below mentioned code to get file using REST API call.

url: http://site url/_api/web/lists/getbytitle('<list title>')/items(<item id>)/File/$value
method: GET
headers:
    Authorization: "Bearer " + accessToken

Example

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

var response = client.GetAsync("https://tenant.sharepoint.com/_api/web/lists/getbytitle('Documents')/items(1)/File/$value", HttpCompletionOption.ResponseHeadersRead).Result;
var fileContent = response.Content.ReadAsByteArrayAsync().Result;

GetFolderByServerRelativeUrl Examples

//The root folder of the Shared Documents library
_api/web/GetFolderByServerRelativeUrl('/Shared Documents')

//The file a.txt from the Plans library
_api/web/GetFolderByServerRelativeUrl('/Plans')/Files('a.txt')/$value

Please check here for more information

8
  • Have you got it working?
    – Hardik
    Jun 2, 2016 at 15:21
  • Thanks for the suggestions. The first bit of code unless I'm missing something requires javascript to get the SP.AppContext working. I'm doing this in native iOS and Android. When I use PostMan to test the call as you suggest it states there's no appContext. The other examples do not work as well, still says the files do not exist.
    – Karl Barek
    Jun 2, 2016 at 17:48
  • Check my updated answer... If it will help you
    – Hardik
    Jun 2, 2016 at 17:54
  • If I only know the file name and path, how do I get the item Id of the file?
    – Karl Barek
    Jun 3, 2016 at 12:47
  • Use this _api/web/GetFolderByServerRelativeUrl('/Plans')/Files('a.txt')/$value
    – Hardik
    Jun 3, 2016 at 12:48

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.