0

I have a requirement where I have to change view based on users logged into the portal. If user belongs to guest view then view should be different and if he is logged in as admin a different view should be displayed. I want to do that by changing the default view to the view I have created. How can I do that?

1 Answer 1

0

This code change the view by taking the id of the new view by list. (you can use your own logic to change it)

<script type="text/javascript">
//declare a variable for the exisitng view
var existingview;

function loadViewAndChangeIfExists()
{
  //get the current client context
  clientContext = new SP.ClientContext.get_current();
  //if the client context exists - proceed
  if (clientContext != undefined && clientContext != null) {
    //get the current web
    var web = clientContext.get_web();
    //get all lists
    var listCollection = web.get_lists();
    //get te current list by name - alternative use getById(guid)
    var list = listCollection.getByTitle("COM");
    //get all current views of the list
    var viewcollection = list.get_views();
    //get the view that you will change
    this.existingview = viewcollection.getByTitle("MYVIEW");
    //load your existing view and execute async
    clientContext.load(existingview);
    //the asynchronous execution will try to load the existing view
    //if the view does not exist - it will call onQueryFailed - else it will call changeView
    clientContext.executeQueryAsync(Function.createDelegate(this, this.changeView), Function.createDelegate(this, this.onQueryFailed));
  }
}

function changeView() {
  //the varible existingview is now loaded with your view
  //get the current CAML query
  var existingquery = existingview.get_viewQuery();
  //do somnething with the exisitng query here
  //...
  //reset the query (as sample)
  existingquery = "<Where><In><FieldRef Name="ID" /><Values>";
  //add a CAML filter with the current selected items
  var selectedItems = SP.ListOperation.Selection.getSelectedItems();
  var i;
  for (i in selectedItems) {
    existingquery += "<Value Type="Counter">" + selectedItems[i].id + "</Value>"; //add
  }
  //close the CAML XML
  existingquery += "</Values></In></Where>";
  //assocciate the existing view with the changed query
  existingview.set_viewQuery(existingquery);
  //update the view!!!
  existingview.update();
  //execute the statements async - onQuerySucceeded for success - onQueryFailed for errors
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQueryFailed(sender, args) {
  //show error message ()stacktrace
  alert('Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());
}

function onQuerySucceeded() {
//show successful message
alert('View successfully changed!');
}
</script>

Changing existing list views

2
  • I don't want to redirect the user. I just want to change the view even if it changes for all users. I have separate function to check for the user's group.
    – A Beginner
    Apr 24, 2015 at 9:07
  • updated my answer
    – Nk SP
    Apr 24, 2015 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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