0

I'm trying to get the value of a field for a list item, using VBA. The REST URL I'm using is:

https://sps.utility.xyz.com/sites/abc123/_api/web/lists/GetByTitle('REST Test List')/items(2)/fields('Title')

However, this returns the following error:

<?xml version="1.0" encoding="utf-8"?>
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <m:code>-1, Microsoft.SharePoint.Client.ResourceNotFoundException</m:code>
    <m:message xml:lang="en-US">Cannot find resource for the request fields.</m:message>
</m:error>

Is there a way to get the field value directly with a REST call, or do I need to call:

https://sps.utility.xyz.com/sites/abc123/_api/web/lists/GetByTitle('REST Test List')/items(2)

and then parse the resulting XML to find the Title node?

Edit:

Using ThinkB4Code's solution, I get:

<?xml version="1.0" encoding="utf-8"?>
<entry xml:base="https://sps.utility.xyz.com/sites/abc123/_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" m:etag="&quot;5&quot;">
<id>Web/Lists(guid'c920cb93-a31a-49a0-a4d6-ac1cb8fb0658')/Items(2)</id>
<category term="SP.Data.REST_x0020_Test_x0020_ListListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" href="Web/Lists(guid'c920cb93-a31a-49a0-a4d6-ac1cb8fb0658')/Items(2)" />
<title />
<updated>2017-09-20T16:01:19Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Title>item 2 has a new value.</d:Title>
</m:properties>
</content>
</entry>

EDIT:

Robin in Or's solution gives:

<?xml version="1.0" encoding="utf-8"?>
<d:Title 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">item 2 has a new value.</d:Title>
3

You can get a single field for a single item with this format:

<YOURSITE>/_api/web/lists/GetByTitle('REST Test List')/items(2)/Title
  • This also returns an XML answer (see edit to original question), which is slimmer than the $select=Title method but still requires XML parsing. Is it possible to just get the string that I'm looking for, or is the XML inevitable per @dylancristy 's comment? – sigil Sep 20 '17 at 16:19
  • 1
    You can get either JSON or Atom depending on your Get Headers, but you're going to get an XML piece regardless. You can store the JSON answer and reference the single field with data.d.Title – Robin in Or Sep 20 '17 at 16:23
2

Instead of doing ‘item(2)/fields(“Title”)’ try doing ‘item(2)?$select=Title’. This will give you the value saved in Title field only. Think of this as a sql query where you write select statement get only required columns. You can include as many required columns in this query using comma.

  • Using the URL https://sps.utility.xyz.com/sites/abc123/_api/web/lists/GetByTitle('REST Test List')/items(2)?$select=Title, I got a complex block of XML in response, as opposed to just the value of Title. See my edit to original question. Did I incorrectly implement your suggestion? – sigil Sep 20 '17 at 16:09
  • 2
    @sigil I don't think you did that incorrectly... as you can see in your response XML, there is only the Title in the properties node, I think if you hadn't done the $select you would see all the columns in the properties. As far as I am aware, there is always going to be a little overhead structure to the response when making REST calls, you are never going to get just the value of a specific field. (I might be wrong though.) ETA - looking at your result for Robin in Or's method, I guess I was wrong... :) – Dylan Cristy Sep 20 '17 at 16:15
  • 1
    @sigil: The implementation was correct and the response you got was also correct. But think of a case where you have 50 columns in a list and you want to display only 5 column data. At that time the the solution posted by @Robin in Or wont able to provide what you require. That's why we have select query string. This $select help us to grab only required columns instead of all the data stored in item. Also the return XML contains only one column data and rest it meta data about your item like the item is coming from REST Test List having xml schema defined in the second line. – ThinkB4Code Sep 21 '17 at 5:36
  • 1
    In case if you want to reduce the the size of returned data, i'll suggest you to go with JSON format. this can be done by appending another query string /item(2)?$format=json. In this query you wont even receive the extra xml payload and the size of return data will be comparatively very less. – ThinkB4Code Sep 21 '17 at 5:39
  • @ThinkB4Code, thanks for the suggestion, I'll definitely use $select when I need multiple columns. As for JSON, I'm using VBA, which has native support for parsing XML but not JSON, so there's a trade-off of easy coding vs. data efficiency. – sigil Sep 21 '17 at 15:58
0

It would be better to use $filter in your REST call for this as it returns a JSON and you can retrieve the data easily.

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.