4

How can I get just the items in the user's current view/filter? Using Sharepoint 2010 Client Object Model (i.e. Javascript/ECMAScript).

A user could select a view that shows a subset of the items available in the library or list, and they could then apply a filter to one or more columns. I want to get the net result of all that filtering. I want all the items visible to the user, in all pages.

I've seen lots of sample code that depends on knowing the current view in order to constuct a query - that doesn't really help in this case. I already know how to get just selected items, e.g.

SP.ListOperation.Selection.getSelectedItems(SP.ClientContext.get_current());

However, that only selects the items in the current page.

Thanks!

4

2 回答 2

2

Sibirman 的首选答案将仅返回视图的原始查询。当用户应用过滤器操作时,用户指定的过滤器实际上附加到 URL(作为 InplviewHash 字符串的一部分)。

例如#InplviewHashf16272c0-c177-42d7-9638-35fd75c90348=WebPartID%3D%7BF16272C0--C177--42D7--9638--35FD75C90348%7D- FilterField1 %3DProjectRef- FilterValue1 %3DProject% 25201- FilterField2 % 3DADdress2

INPLVIEW.js 和 /_layouts 中的其他 SP JavaScript 文件中有一些函数,其中包括用于解码和重新初始化视图的函数,但我无法全部解密。

DecodeHashAsQueryString 和 InitGridFromView 是一个很好的起点。

我最终编写了自己的代码来检查哈希字符串,然后删除键/值对。

var uri = window.location.href;
var hashIndex = uri.search("#");

var filter = false;
if (hashIndex == -1) {
  // Wasn't found
  alert('No filters applied!');
  // ...go with default query.
} else {
  // # found. Get hashstring
  var hashStr = uri.substring(hashIndex);
  newStr = DecodeHashAsQueryString(hashStr);

  var trStr = newStr.substring(newStr.indexOf("FilterField"));
  var retStr = trStr.replace(/%3D|&/g,",").replace(/%2520/g," ");
  retStr = retStr.replace(/FilterField[0-9]+,|FilterValue[0-9]+,/g,"")
  var filtArray = retStr.split(','); // "MyField1","MyValue1",...

并将它们应用于我自己的不包含限制的查询,因此返回所有符合过滤条件的项目。

如果要处理文本 opr 选择以外的字段,则需要更进一步并获取字段类型,以便可以根据需要修改每个字段的查询值类型。

于 2014-08-17T23:27:02.290 回答
1

您可以通过两个请求来做到这一点:

function getItemsFromView(listTitle, viewTitle)
{

    var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle(listTitle);
    var view = list.get_views().getByTitle(viewTitle);
    context.load(view);

    context.executeQueryAsync(
        function(sender, args) {getItemsFromList(listTitle, "<View><Query>" + view.get_viewQuery() + "</Query></View>")},
        function(sender, args) {alert("error: " + args.get_message());}
    );
}

function getItemsFromList(listTitle, queryText) 
{
    var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle(listTitle);

    var query = new SP.CamlQuery();
    query.set_viewXml(queryText);

    var items = list.getItems(query);

    context.load(items);
    context.executeQueryAsync(
        function()
        {
            var listEnumerator = items.getEnumerator();
            var i = 0;
            while (listEnumerator.moveNext())
            {
                i++;
            }
            alert("items retrieved: " + i);
        },
        function(sender, args) {alert("error in inner request: " + args.get_message());}
   );

}
于 2013-01-16T05:09:51.127 回答