0

I'm having trouble retrieving the relative path of a file using CAML:

http://[siteURL]/_api/web/lists/GetByTitle('test')/GetItems(query=@v1)?@v1={"ViewXml":"<View><ViewFields><FieldRef%20Name='FileRef'/></ViewFields><Query></Query></View>"}

It returns following results which don't contain the FileRef field I'm querying for:

<entry m:etag="&quot;2&quot;">
<id>Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)</id>
<category term="SP.Data.Asset_x005f_RightsItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<link rel="edit" href="Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FirstUniqueAncestorSecurableObject" type="application/atom+xml;type=entry" title="FirstUniqueAncestorSecurableObject" href="Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)/FirstUniqueAncestorSecurableObject"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/RoleAssignments" type="application/atom+xml;type=feed" title="RoleAssignments" href="Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)/RoleAssignments"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/AttachmentFiles" type="application/atom+xml;type=feed" title="AttachmentFiles" href="Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)/AttachmentFiles"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContentType" type="application/atom+xml;type=entry" title="ContentType" href="Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)/ContentType"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FieldValuesAsHtml" type="application/atom+xml;type=entry" title="FieldValuesAsHtml" href="Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)/FieldValuesAsHtml"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FieldValuesAsText" type="application/atom+xml;type=entry" title="FieldValuesAsText" href="Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)/FieldValuesAsText"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FieldValuesForEdit" type="application/atom+xml;type=entry" title="FieldValuesForEdit" href="Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)/FieldValuesForEdit"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/File" type="application/atom+xml;type=entry" title="File" href="Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)/File"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Folder" type="application/atom+xml;type=entry" title="Folder" href="Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)/Folder"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ParentList" type="application/atom+xml;type=entry" title="ParentList" href="Web/Lists(guid'18bbc395-68ef-4a43-af94-54dfa73fffce')/Items(2)/ParentList"/>
<title/>
<updated>2015-11-03T14:37:22Z</updated>
<author>
  <name/>
</author>
<content type="application/xml">
  <m:properties>
    <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
    <d:Id m:type="Edm.Int32">2</d:Id>
    <d:ID m:type="Edm.Int32">2</d:ID>
    <d:Modified m:type="Edm.DateTime">2015-10-01T12:40:33</d:Modified>
    <d:CheckoutUserId m:null="true"/>
    <d:EditorId m:type="Edm.Int32">1</d:EditorId>
  </m:properties>
</content>

This is strange because the same query works fine when I test it in CAML Designer 2013. It also works when I query for other fields, for example document title, ID, etc. Any idea what could be the problem?

  • How are you executing this code? using jquery? – Atish Dipongkor - MVP Nov 3 '15 at 15:00
  • I'm testing it in Fiddler. Once I get it working I'll be calling it from an MVC controller. – Dr. Greenthumb Nov 3 '15 at 15:03
  • Can you consider my url? – Atish Dipongkor - MVP Nov 3 '15 at 15:16
1

If your requirement is to get the FileRef of your document, then you can consider following URL.

/_api/web/lists/GetByTitle('test')/Items?$select=FileRef

Just make a GET request to the above URL. If you need more columns, then add them in $select operator separated by coma(,).

Based on the comment: You need the <Where>. You can use $filter instead of <Where>. If you have time, then have a look on following article.

CRUD Operation to List Using SharePoint 2013 Rest API

  • 1
    The problem is that I will be adding a 'where' clause to the query, I don't think the method above has this functionality. – Dr. Greenthumb Nov 3 '15 at 15:25
  • No. It has. You can use $filter instead of where – Atish Dipongkor - MVP Nov 3 '15 at 15:27
  • Is it possible to do a recursive query this way? I need to search sub folders as well. – Dr. Greenthumb Nov 3 '15 at 15:30
  • 3
    I wouldn't give him flack about the requirements. He asked a question about CAML. Instead of answering the question he asked, you gave him a workaround. – wjervis Nov 3 '15 at 15:45
  • 2
    I have combined elements of Atish's URL with my original CAML URL and found that the following URL successfully returns FileRef field: /GetItems(query=@v1)?$select=fileref&@v1={"ViewXml":"<View><Query></Query></View>"} – Dr. Greenthumb Nov 9 '15 at 15:39

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.