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

  • Share the error message that you are getting.. – Rohit Waghela Aug 23 '17 at 13:03
  • @ Rohit Waghela, There is no error since url is not the right then it ignores the ajax call . thank you for quick response – Faxelf Aug 23 '17 at 13:15
  • I found this publication interesting but I didn't quite understand how to do it : sharepoint.stackexchange.com/questions/194534/… – Faxelf Aug 23 '17 at 13:19
  • I didn't understand what do you mean by publication ?? And is your issue resolved ?? – Rohit Waghela Aug 23 '17 at 13:21
  • @ Rohit Waghela With my script I only get pages that are not in a folder, now my page is in a folder – Faxelf Aug 23 '17 at 13:26
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>
  • I have tested with but I still have an empty result, my url is good? : ** URL : "ap.gup.honet/sites/sight/fr-fr/_api/web/… eq '" + page_title +"'",** – Faxelf Aug 23 '17 at 14:34
  • I have updated answer with full code. check it. – SP Developer Aug 24 '17 at 5: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? (info.paitgroup.com/blog/…) – Faxelf 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. – Faxelf 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 : http://www.enjoysharepoint.com/Articles/Details/value-does-not-fall-within-the-expected-range-error-in-21104.aspx

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.