2

I'm new to REST API in SharePoint. I was required to query some items from given URL of Calendar type list. But I know how to query calendar list items (http://server/_api/web/lists/getbytitle('Calendar')/items?$select=Title). But I don't how to do it for given URL (http://server/Lists/Calendar/MyItems.aspx). Please help me how the REST URL should be to query items from http://server/Lists/Calendar/MyItems.aspx.

1 Answer 1

2

The page represents a list view. For example, the URL http://server/Lists/Calendar/MyItems.aspx in your case may represent a view named "My Items". You can go into the view and check what filters are applied to the view. And then based on that you can call the REST api to get the items using $filter operator. Example:

http://server/_api/web/lists/getbytitle('Calendar')/items?$select=Title$filter=COLUMN1 eq 'VALUE'

If there are more than one filters, you can use operators like (and/or).

If the filter is complex, you can create a CAML query of the view and use it to get items in the view.

function getJson(url) 
{
    return $.ajax({       
       url: url,   
       type: "GET",  
       contentType: "application/json;odata=verbose",
       headers: { 
          "Accept": "application/json;odata=verbose"
       }
    });
}


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

    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"
           }
     });
}

Usage

getListItemsForView(_spPageContextInfo.webAbsoluteUrl,'Announcements','Latest News')
.done(function(data)
{
     var items = data.d.results;
     for(var i = 0; i < items.length;i++) {
         console.log(items[i].Title);
     }    
})
.fail(
function(error){
    console.log(JSON.stringify(error));
});

Source: https://stackoverflow.com/questions/26884647/using-rest-to-fetch-sharepoint-view-items

2

Your Answer

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

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