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

Kul*_*ige 3 sharepoint jquery sharepoint-online

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

谢谢!

Vad*_*hev 7

根据SharePoint REST请求中的使用OData查询操作,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);
Run Code Online (Sandbox Code Playgroud)

的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);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

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

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));
    }
);
Run Code Online (Sandbox Code Playgroud)