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:
- 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
Imbed the query into the client side JSLINK javascript to dynamically add a filter to an existing view
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.
- 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:
Does anyone have examples for option 2 if that is a possibility? How can this be done?
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?