Get elements of the current view / filter in Sharepoint 2010 - using the client object model

How can I get only elements in the current view / filter of the user? Using the SharePoint client object model (for example, Javascript / ECMAScript).

The user can select a view that displays a subset of the items available in the library or list, and then they can apply a filter to one or more columns. I want to get a clean result from all this filtering. I want all elements to be visible to the user, on all pages.

I saw many examples of code that depends on knowing the current view in order to build a query - this does not really help. I already know how to get only selected items, for example.

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

However, this only selects the items on the current page.

Thanks!

+4
source share
2 answers

Sibirman's preferred answer will only return the original request for submission. Custom filters are actually added to the URL (as part of the InplviewHash line) when the user applies the filter action.

eg. # InplviewHashf16272c0-c177-42d7-9638-35fd75c90348 = WebPartID% 3D% 7BF16272C0 - C177--42D7--9638-35FD75C90348% 7D- FilterField1 % 3DProjectRef- FilterValue1 % 3DProject% 25201- Filter Filter % 2 % 3D-Filter

There are functions inside INPLVIEW.js and other SP JavaScript files in / _layouts that include functions to decode this and reinitialize the view, but I have not been able to decrypt it all.

DecodeHashAsQueryString and InitGridFromView are a good place to start.

I ended up writing my own code to check the hash string, and then cross off 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 does not contain restrictions, therefore, all elements that match the filter criteria are returned.

If you want to handle fields other than opr text selection, you need to take one more step and get the field type so that you can change the type of query value for each field as needed.

+2
source

You can do this with two queries:

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

Source: https://habr.com/ru/post/1438309/


All Articles