使用 REST 获取 SharePoint 视图项 [英] Using REST to fetch SharePoint View Items

查看:26
本文介绍了使用 REST 获取 SharePoint 视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建正确的 URL 以使用 REST api 返回 SharePoint 视图中的项目.

I am trying to construct the correct URL to return the items in a SharePoint View using the REST api.

使用我的浏览器和以下 URL,我可以返回列表中的项目.

Using my browser and the following URL I can return the items in the list.

https://mysharepoint.sharepoint.com/sites/MySite/_api/web/lists/getbytitle('Announcements')/Items

我可以使用以下 URL 获取视图定义.

And I can get the view definition using the following URL.

https://mysharepoint.sharepoint.com/sites/MySite/_api/web/lists/getbytitle('Announcements')/Views/getbytitle('Latest News')/

但我无法弄清楚需要在该 URL 的末尾放置什么才能实际获取 View 返回的项目.

But I cannot figure out what I need to put at the end of that URL to actually get the items that are returned by the the View.

推荐答案

SP.View 对象包含任何操作列表项的方法.但是 SP.View 对象包含SP.View.viewQuery 属性指定列表视图使用的查询.这意味着可以使用以下方法来检索列表项以供查看:

SP.View object does not contain any methods for manipulating list items. But SP.View object contains SP.View.viewQuery property that specifies the query that is used by the list view. That means the following approach could be used for retrieving list items for 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"
           }
     });
}


function getListItemsForView(webUrl,listTitle,viewTitle)
{
     var viewQueryUrl = webUrl + "/_api/web/lists/getByTitle('" + listTitle + "')/Views/getbytitle('" + viewTitle + "')/ViewQuery";
     return getJson(viewQueryUrl).then(
         function(data){         
             var viewQuery = data.d.ViewQuery;
             return getListItems(webUrl,listTitle,viewQuery); 
         });
}

使用

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

这篇关于使用 REST 获取 SharePoint 视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆