In a page, I need to add a list view web part when the page is loaded.

Is it possible using javascript?

Actual situation is that a list has a column States. It can have multiple values. So in dispform.aspx for each value in this column I need to add saperate list views.

When a new item is added then the user can add any number of state. So if he adds 3 state and when he goes to the dispform.aspx for the same item then he should see 3 list views for each states added.

  • yes it is possible, what have you tried sofar? Commented Jan 27, 2016 at 11:35
  • I have tried searching how to add list view web part in a page. I didnt get anything for doing it using javascript..
    – KumarV
    Commented Jan 27, 2016 at 12:00
  • 1
    What is the real purpose behind it? If you tell us the objective we can better help you
    – Taran Goel
    Commented Jan 27, 2016 at 13:57
  • You can add all the list view web parts corresponding to each state and then display or hide accordingly on document ready
    – Taran Goel
    Commented Jan 27, 2016 at 16:24

1 Answer 1

function CreateView()
    var hostweburl = "INSERT HOST URL HERE";
    var appweburl = "INSERT APP URL HERE";
    //creating the client context
    var clientContext = new SP.ClientContext(appweburl);
    var appContextSite = new SP.AppContextSite(clientContext, hostweburl);
    //get the current web
    var web = appContextSite.get_web();
    //get all lists
    var listCollection = web.get_lists();
    //get my demo list by name "MYLIST" - alternative use getById(guid)
    var list = listCollection.getByTitle('MYLIST');
    //get the view collection to add the view
    var viewcollection = list.get_views();
    //create a viewinfo with the view properties
    var viewInfo = new SP.ViewCreationInformation();
    viewInfo.set_title("MYVIEW"); //set the title
    viewInfo.set_personalView(true); //set the view personal
    //creating the CAML query by unsing "IN"
    var query = "<Where><In><FieldRef Name="ID" /><Values>";
    //get all items selected in the UI
    var selectedItems = SP.ListOperation.Selection.getSelectedItems();
    for (var i in selectedItems) {
      //add the item ID of each selected item
      query += "<Value Type="Counter">" + selectedItems[i].id + "</Value>";

    query += "</Values></In></Where>"; //close CAML XML
    //associate the query to the view
    //add the view to the view collection
    //load the view collection
    //execute the operation async - call onQueryFailed on errors and onQuerySucceeded on success
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));


function onQuerySucceeded() {
  //show successful message
  alert('View successfully created!');

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

i get this code from here, i make changes to use this from webpart

  • this is not what he asked for. You created a view in list, while he wanted to add a listview webpart
    – Taran Goel
    Commented Jan 27, 2016 at 13:57
  • in this case you have to go the aspx file of your webpart and right click in the "body", click on "Insert code fragment", search "SharePoint" and then click on "splistview", you have to change the ListUrl Commented Jan 27, 2016 at 14:25

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.