1

I'm trying to dynamically add filters to an existing view, with an OR between two columns and using CONTAINS constraints, using purely client side javascript

The solutions to me seem to be either:

  1. Create another column that is populated in a presaveaction javascript to concationate the two items together, which can then be filtered using the URL filter

Works, but this is a fall back solution

  1. Imbed the query into the client side JSLINK javascript to dynamically add a filter to an existing view

  2. Use a render to take out rows from the view that do not match the filters.

The only problem is that it will just remove those rows that are within the current page.

  1. Have javascript create a personalised view.

This method uses the following type of code:

(taken from http://blog.techperspect.com/2015/07/create-list-view-using-sharepoint-rest.html)

    function createView(ListName, query ) {
         var bodyContent = "{ '__metadata': { 'type': 'SP.View' }, 'Title': 'View Name', 'PersonalView':   true, 'ViewQuery':'" + query + "'  }";
         var viewUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('"+ListName+"')/views"
         var executor;  executor = new SP.RequestExecutor(_spPageContextInfo.webAbsoluteUrl);  
         executor.executeAsync({ url: viewUrl,
                         method: "POST",
                         body: bodyContent,
                         headers: { 
                               "accept": "application/json;odata=verbose",
                               "content-type": "application/json;odata=verbose"
                                  },
                         success: function (results) { alert("View Created.") },
                         error: errorHandler
                     });

                                   };

       var myquery = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Testvalue</Value></Eq</Where>";

       var scriptbase = _spPageContextInfo.webAbsoluteUrl+ "/_layouts/15/";

    function RunView() {
          createView('mylist',myquery);
    };

    $.getScript(scriptbase + "SP.Runtime.js",
           function () {
               $.getScript(scriptbase + "SP.js",
                   function () { $.getScript(scriptbase + "SP.RequestExecutor.js", Runview) }
                       )
                    }
             ) 
    }

    function errorHandler(data, errorCode, errorMessage) {
          alert(data);
          alert(errorCode);
          alert(errorMessage);
       }  

Questions:

  1. Does anyone have examples for option 2 if that is a possibility? How can this be done?

  2. When I run the code for option 4, the code fails. the message is

"Invalid JSON. A comma character ',' was expected in scope 'Object'. Every two elements in an array and properties of an object must be separated by commas.

What is wrong with my parameters?

The parameter that is passed across is:

bodyContent "{ '__metadata': { 'type': 'SP.View' }, 'Title': 'View Name', 'PersonalView':   true, 'ViewQuery':'<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Testvalue</Value></Eq></Where>'  }"

I assume the commas are missing from the ViewQuery parameter. Any ideas where?

0

on a ListView WebPart you an not do 2. (apply a filter to a View with JavAscript)

Because it is the Server that executes the View and sends the content in the ctx object to the browser.

So you can only 'filter' out what rows you do not want in the CSR OnPreRender function

0

I changed the value of bodyContent to be

var bodyContent = JSON.stringify(
        {
            "__metadata": { "type": 'SP.View' },
            "Title": "MyView",
        "PersonalView":   true,
            "ViewQuery": "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Metest</Value></Eq></Where>" 
        }
    );

And the view creation works.

BUT I'd still like to know whether anyone have examples for option 2 and if it is possible? How can this be done?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.