0

The goal I'm trying to achieve is retrieving the URL from a document in a document library on sharepoint online. When I use the REST API to show me a list of all the documents in this library I come up with the next XML:

<?xml version="1.0" encoding="utf-8" ?> 
- <feed xml:base="https://thomasmorestudent17.sharepoint.com/sites/devtest/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
  <id>5b88dd38-c2f0-4d00-8b73-80e0ca4b0eb6</id> 
  <title  /> 
  <updated>2014-03-20T14:47:21Z</updated> 
- <entry>
  <id>https://thomasmorestudent17.sharepoint.com/sites/devtest/_api/Web/Lists(guid'ab8811c5-0d39-457c-8fd1-c15a45c78f89')/files('Aanleiding en achtergrond van het project.docx')</id> 
  <category  term="MS.FileServices.File" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
  <link  rel="edit" href="Web/Lists(guid'ab8811c5-0d39-457c-8fd1-c15a45c78f89')/files('Aanleiding%20en%20achtergrond%20van%20het%20project.docx')" /> 
  <title  /> 
  <updated>2014-03-20T14:47:21Z</updated> 
+ <author>
- <content type="application/xml">
  - <m:properties>
    + <d:CreatedBy m:type="MS.FileServices.UserInformation">
      <d:ETag>"{ECAEE072-FEDD-4FF6-8A27-1EFF131B0064},1"</d:ETag> 
      <d:Id>Aanleiding en achtergrond van het project.docx</d:Id> 
    + <d:LastModifiedBy m:type="MS.FileServices.UserInformation">
      <d:Name>Aanleiding en achtergrond van het project.docx</d:Name> 
      <d:Size m:type="Edm.Int32">21616</d:Size> 
      <d:TimeCreated m:type="Edm.DateTime">2014-03-14T17:24:25Z</d:TimeCreated> 
      <d:TimeLastModified m:type="Edm.DateTime">2014-03-14T17:24:25Z</d:TimeLastModified> 
      <d:Url>/sites/devtest/Shared Documents/Aanleiding en achtergrond van het project.docx</d:Url> 
    </m:properties>
 </content>
</entry>
</feed>

This is the code I'm using which worked before when I wanted the title and id of the list:

HttpWebRequest itemRequest =
                (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/Web/lists(guid'" + listId + "')/Files");
            itemRequest.Method = "GET";
            itemRequest.Accept = "json;odata=verbose";
            itemRequest.Headers.Add("Authorization", "Bearer " + accessToken);
            using (HttpWebResponse itemResponse = (HttpWebResponse)itemRequest.GetResponse())
            {
                using (StreamReader itemReader = new StreamReader(itemResponse.GetResponseStream()))
                {
                    XDocument doc = XDocument.Parse(itemReader.ReadToEnd());

                    var id = doc
                        .Descendants(atom + "entry").Descendants(atom + "content").Descendants(m + "properties").Elements(d + "Url");

                    TableRow tableRow = new TableRow();
                    TableCell tableCell1 = new TableCell();
                    TableCell tableCell2 = new TableCell();
                    tableCell2.Text = id.ToString();
                    tableRow.Cells.Add(tableCell2);
                }
            }

The error I get basicly says there is no element to refer to. Looks like my streamreader is not reading the XML correctly? Any help would be appreciated.

1 Answers1

0

You are not receiving xml, but json, as requested in the Accept header. itemRequest.Accept = "json;odata=verbose";

You should either parse the json or change the header. I would personally suggest the first option, using the JSON.Net library

Radu Simionescu
  • 4,518
  • 1
  • 35
  • 34