Question

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!

Was it helpful?

Solution

The preferred answer by Sibirman will only return the raw query for the view. User-specified filters are actually appended to the URL (as part of the InplviewHash string) when a user applies a filter action.

e.g. #InplviewHashf16272c0-c177-42d7-9638-35fd75c90348=WebPartID%3D%7BF16272C0--C177--42D7--9638--35FD75C90348%7D-FilterField1%3DProjectRef-FilterValue1%3DProject%25201-FilterField2%3DAddress-FilterValue2%3DPilbara

There are functions within INPLVIEW.js and other SP JavaScript files in /_layouts that include functions for decoding this and re-initialising the view but I wasn't able to decipher it all.

DecodeHashAsQueryString and InitGridFromView are a good place to start.

I wound up writing my own code to check for the hash string and then strip out the key/value pairs.

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",...

And applying them to my own query which doesn't include limits, so all items are returned that meet the filter criteria.

If you want to handle fields other than text opr choice, you'd need to take it a step further and get the field type so you can modify the query’s value type for each field as required.

OTHER TIPS

You can do it by two requests:

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top