【问题标题】:Get distinct data of SharePoint list using REST API使用 REST API 获取 SharePoint 列表的不同数据
【发布时间】:2014-11-02 21:11:39
【问题描述】:

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

谢谢!

【问题讨论】:

  • 问之前最好做一些研究,因为这不是一个教程网站

标签: jquery sharepoint sharepoint-online


【解决方案1】:

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

【讨论】:

  • 如果您有超过 100 个结果,这将不起作用
【解决方案2】:

据我了解,您说的是 http:///_vti_bin/Lists.asmx。

嗯,是的,你应该可以通过简单的 SQL 来查询数据——选择 Distinct...

我一直在为用户 Web 服务使用相同的概念。但是,它只是给你一个线索。

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

【讨论】:

    猜你喜欢
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    相关资源
    最近更新 更多