1

I am using REST to get item page by specific title,

My question is how to get the specific page in folder from list Pages

Here is my code snippet :

var folder;  //folder
var source;                                         
var page_title = "myPage";
$.ajax({
        url: "https://app.gro.net/sites/ight/fr-fr/_api/web/Lists/getbytitle('Pages')/items?$filter=Title eq '" + page_title +"'",
        type: "GET",
        headers: {
            "ACCEPT": "application/json;odata=verbose"
        },
        success: function (data) {

            if (data.d.results[0]) {
                //var source = $('#content')[0];
                       source = data.d.results[0].Desc;
            }

        },
       error: function(){ //Error }
   }});

Thank you for help

Amine Zel
  • 173
  • 6

2 Answers2

0

Pages is not a list. its library. so you can get & filter file by below api,

 _api/web/GetFolderByServerRelativeUrl('Pages')/Files?$filter=Title eq 'test'

Below is full code

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        getfiles();
    });


    function getfiles() {
        var pageTitle = 'test';
        var webUrl = _spPageContextInfo.webAbsoluteUrl;
        var requestHeaders = { "accept": "application/json;odata=verbose" };
        $.ajax({
            url: webUrl + "/_api/web/GetFolderByServerRelativeUrl('Pages')/Files?$filter=Title eq '"+pageTitle+"'",
            contentType: "application/json;odata=verbose",
            headers: requestHeaders,
            success: onSuccess,
            async: false,
            error: onError
        }); function onSuccess(data, request) {
            alert(data.d.results[0].Name);
        } function onError(error) {
            alert("Error on retrieving page.");
        }
    }
</script>
SP Developer
  • 3,085
  • 1
  • 17
  • 44
  • I have tested with but I still have an empty result, my url is good? : ** URL : "https://ap.gup.honet/sites/sight/fr-fr/_api/web/GetFolderByServerRelativeUrl('Pages')/Files?$filter=Title eq '" + page_title +"'",** – Amine Zel Aug 23 '17 at 14:34
  • I have updated answer with full code. check it. – SP Developer Aug 24 '17 at 05:48
  • thank you for your help, But I still have the empty result [] "data.d.results" , For information the file I am looking for is in a folder X in the library Page Like that : Pages(lib) --> Folder X --> MyPage(file) , You think I should use JSOM like explains this post? (https://info.paitgroup.com/blog/sharepoint-rest-api-and-lists-with-folders) – Amine Zel Aug 24 '17 at 12:36
  • In API url, just change ('Pages') to ('Pages/Folder X') & try. – SP Developer Aug 24 '17 at 12:41
  • Thank you very much Vishal for your help, but I always have the result empty, I will try to do this with csom and after I post the answer, thank you Vishal. – Amine Zel Aug 24 '17 at 14:07
0

Here is the solution to retrieve a page in a specific folder with CAML & REST.

var dm = "PageName";
var queryText =  "" + dm + "";

function getListItems(webUrl,listTitle, queryText,folderUrl) { var viewXml = '' + queryText + ''; var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems"; var queryPayload = {
'query' : { '__metadata': { 'type': 'SP.CamlQuery' }, 'ViewXml' : viewXml, "FolderServerRelativeUrl": folderUrl } };

return $.ajax({
       url: url,
       method: "POST",
       data: JSON.stringify(queryPayload),
       headers: {
          "X-RequestDigest": $("#__REQUESTDIGEST").val(),
          "Accept": "application/json; odata=verbose",
          "content-type": "application/json; odata=verbose"
       }
 });

}

getListItems(_spPageContextInfo.webAbsoluteUrl,'Pages', '', 'Pages/Folder') .then(function(data) {

 var items = data.d.results[0].Title;

    console.log(items);  

}) .fail(function(error){ console.log(JSON.stringify(error)); });

PS: to resolve this issue "Value does not fall within the expected range error in Rest API" please refer :

https://www.enjoysharepoint.com/value-does-not-fall-within-the-expected-range-error-in-rest-api/

Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61
Amine Zel
  • 173
  • 6