3

I'm on SharePoint Online and I need to use the Rest API to query the Pages library.

Normally, I'd use GET, but since I'm filtering on a Managed Metadata field, I have to use POST instead. For some reason reason, however, I'm not able to get some fields even though I have it on my ViewFields (I'm specifically interested in EncodedAbsUrl or FileRef).

Here's my query:

var query = '<Query><Where><Eq><FieldRef Name="RelatedTag"/><Value Type="TaxonomyFieldType">SomeValue</Value></Eq></Where></Query>';
var viewfields = '<ViewFields><FieldRef name="EncodedAbsUrl" /><FieldRef Name="Title" /></ViewFields>';

$.post({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Pages')/GetItems(query=@v1)?@v1={'ViewXml':'<View>"+query+viewfields+"</View>'}",
    headers: {
        "Accept": "application/json;odata=verbose",
        "Content-Type" : "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    }
});

The response I get back is this: enter image description here

As you can see, Title is there, but EncodedAbsUrl isn't. This also does not work if I put in FileRef, FileLeafRef, etc. RelatedTag and all my other custom fields work though.

2 Answers 2

2

So I've thought about using a combination of POST and GET requests to the Rest API but found it way too chatty. Instead I looked into JSOM as an alternative and it works:

var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Pages');

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name="RelatedTag"/>' +
    '<Value Type="Text">SomeValue</Value></Eq></Where></Query></View>');
this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);

clientContext.executeQueryAsync(function (sender, args) {
    var listItemEnumerator = collListItem.getEnumerator();
    console.log(listItemEnumerator);
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        console.log('Title: ' + oListItem.get_item('Title') +
            '\nFileRef: ' + oListItem.get_item('FileRef'));
    }
}, null);
1

Are you trying to get the url of a File? Your query is getting the item, not the file, so you have to $expand=File and then you will have all the Files properties, including FileRef, FileLeafRef and EncodedAbsUrl

1
  • I just want to get the URL of the page. How would you 'expand' in a POST request? I know for GET you can.
    – kei
    Commented Mar 31, 2017 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.