使用REST API获取SharePoint列表的不同数据

时间:2014-09-09 12:33:14

标签: jquery sharepoint sharepoint-online

如何使用REST API获取SharePoint列表的不同列数据? 有没有办法实现它而不循环?

谢谢!

2 个答案:

答案 0 :(得分:5)

根据Use OData query operations in SharePoint REST requests,不支持grouping等操作。

解决方案是在之后应用分组从SharePoint REST服务返回JSON结果。

如何使用jQuery

从数组中获取不同的值
function groupBy(items,propertyName)
{
    var result = [];
    $.each(items, function(index, item) {
       if ($.inArray(item[propertyName], result)==-1) {
          result.push(item[propertyName]);
       }
    });
    return result;
}


var catalog = { products: [
   { category: "Food & Dining"},
   { category: "Techonology"},
   { category: "Retail & Apparel"},
   { category: "Retail & Apparel"}
]};

var categoryNames = groupBy(catalog.products, 'category'); //get distinct categories
console.log(categoryNames);

JSFiddle

实施例

假设以下函数用于通过SharePoint REST API获取列表项:

function getListItems(url, listname, query, complete, failure) {
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items" + query,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            complete(data.d); 
        },
        error: function (data) {
            failure(data);
        }
    });
}

然后,以下示例演示了如何打印不同的任务名称:

getListItems('https://tenant.sharepoint.com/project','Tasks','?select=Title',
    function(items){    
       var taskNames = groupBy(items,'Title');
       console.log(taskNames);
    },
    function(error){
       console.log(JSON.stringify(error));
    }
);

答案 1 :(得分:0)

据我所知,你在谈论http:///_vti_bin/Lists.asmx。

是的,您应该能够通过简单的SQL查询数据 - 选择Distinct ...

我一直在使用用户网络服务的相同概念。但是,这只是你的主角。

参考:http://msdn.microsoft.com/en-us/library/office/ms429658(v=office.14).aspx