Forum Discussion

Alex_Wadd's avatar
Alex_Wadd
Copper Contributor
Feb 22, 2021

Caml query not returning fileleafref


I have a CAML query that I am running through the REST api, the simple example below requests 2 fields - FileLeafRef and CustomField. Custom field is simple a text field, nothing special.

CustomField is returned whereas FileLeafRef is not.

If I run the same CAML via CSOM or the old WCF/soap based services then FileLeafRef is returned...

 

<View Scope="RecursiveAll">
  <Query>
    <Where>
      <Eq>
        <FieldRef Name="FileRef" />
        <Value Type="Text">/SomeSite/SomeDocumentLibrary/SomeFile.txt</Value>
      </Eq>
    </Where>
  </Query>
  <RowLimit>1</RowLimit>
  <ViewFields>
    <FieldRef Name="FileLeafRef" />
    <FieldRef Name="CustomField" />
  </ViewFields>
</View>



I know I can use a $select querystring, but this is on an app that the user can specify any number of columns to retrieve and querystrings have a limit.

So is there something I am missing? Or does REST CAML act differently to the other two...

N.b this is against SharePoint Online

 

I should add this is being called via a post to an endpoint like https://tenant.sharepoint.com/SomeSite/_api/web/lists(guid'listGuid')/getitems

1 Reply

  • The column FileRef returns the file URL. In case of FileRef it should be passed to the expands in getItemsByCAMLQuery. Like the code:

    const viewFields = `
    <ViewFields>
    <FieldRef Name='FileRef' />
    <FieldRef Name='ID' />
    <FieldRef Name='Title' />
    </ViewFields>
    `;

    const queryOptions = `<QueryOptions><ViewAttributes Scope='RecursiveAll'/> </QueryOptions>`;
    const camlQuery = '';

    sp.web.lists.getByTitle('Documents')
    .getItemsByCAMLQuery({
    ViewXml: `<View>${viewFields}${camlQuery}${queryOptions}</View>`
    }, 'FileRef')
    .then(console.log)
    .catch(console.log);

    Please note getItemsByCAMLQuery({
    ViewXml: `<View>${viewFields}${camlQuery}${queryOptions}</View>`
    }**, 'FileRef')**

Resources