Create Custom List View using JSCOM

  • Posted by: Softree Technology

This below-mentioned code block will create the custom list view in a specific list. You can also set your custom view fields while creating the list view.

[code lang=”c”]

var oContext = SP.ClientContext.get_current();
var oWeb = oContext.get_web();
var oList = oWeb.get_lists().getByTitle(“MyList”); //Enter title of your list
hostContext.load(oList);
hostContext.executeQueryAsync(CreateListView(oContext, oList),
function (sender, args) {
//On Error
console.log(args.get_message());
});
function CreateListView(oContext, oList) {

var viewFieldColl = [‘Column1’, ‘Column2’, ‘Column3’]; //Add your list fields title that you want to add in the new view

var listViewColl = oList.get_views();

var myView = new SP.ViewCreationInformation();
myView.set_title(“MyNewView”); //Enter a title for your new view
myView.set_viewFields(viewFieldColl);
var camlQuery = new SP.CamlQuery();
var query = “<View><RowLimit>100</RowLimit></View>”; //You can modify view query as per your requirement
camlQuery.set_viewXml(query);
myView.set_query(camlQuery);
myView.set_viewTypeKind(2048); //View type kind 2048 represents datasheet view

myView.set_setAsDefaultView(true); //If you want to set your new view as default view
listViewColl.add(myView);
appContext.load(listViewColl);
appContext.executeQueryAsync(function () {
console.log(“Succesfully created list view.”);
}, function (sender, args) {
//On Error
console.log(args.get_message());
});
}

[/code]

This solution is brought to you by our SharePoint professionals…

Softree Consulting employs SharePoint consultants, We are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants have the right bent of mind to understand and execute customer requirements. Be it web part development, migrating from SharePoint 2010/2013 to SharePoint 2013/2016, Office 365 or something else in SharePoint, we strive to deliver the best

 

Author: Softree Technology

Leave a Reply