SharePoint List Creation JSOM example
//var context = null;// Function which Initiate list creation processfunction initiateListCreation() {var listName = document.getElementById("txtLibraryName").value;if(listName != null) {createList(listName);}}// Create a list with given namefunction createList(listName) {var context = getSPContext(); ;var oWebsite = context.get_web();var listCreationInfo = new SP.ListCreationInformation();listCreationInfo.set_title(listName);listCreationInfo.set_templateType(SP.ListTemplateType.genericList);var listCollection = oWebsite.get_lists()listCollection.add(listCreationInfo);context.load(listCollection, 'Include(Title)');context.executeQueryAsync(Function.createDelegate(this, function () { enableListContentType(listCollection,listName); }),Function.createDelegate(this, this.errorHandler));}// Enable list content type setting in the listfunction enableListContentType(listCollection,listName) {// Enable Allow management of content typesvar context = getSPContext();var listEnumerator = listCollection.getEnumerator();var createdList = null;while (listEnumerator.moveNext()) {if (listEnumerator.get_current().get_title() == listName) {createdList = listEnumerator.get_current();break;}}if (createdList != null) {createdList.set_contentTypesEnabled(true);createdList.update(); //update operation is required to apply list changescontext.load(createdList, 'Title');}context.executeQueryAsync(Function.createDelegate(this, function () { loadListContentTypeCollection(createdList); }),Function.createDelegate(this, this.errorHandler));}// Loading List Content type collectionfunction loadListContentTypeCollection(createdList){var context = getSPContext();var listName = createdList.get_title();var list = context.get_web().get_lists().getByTitle(listName);var listContentTypeCollection = list.get_contentTypes();context.load(listContentTypeCollection, 'Include(Name)');context.executeQueryAsync(Function.createDelegate(this, function () { removeDefaultContentType(createdList,listContentTypeCollection); }),Function.createDelegate(this, this.errorHandler));}// Removing default content type from the listfunction removeDefaultContentType(createdList,contentTypeCollection){var contentTypeEnumerator = contentTypeCollection.getEnumerator();var context = getSPContext();while (contentTypeEnumerator.moveNext()) {if (contentTypeEnumerator.get_current().get_name() == 'Item') {itemContentType = contentTypeEnumerator.get_current();break;}}itemContentType.deleteObject();context.executeQueryAsync(Function.createDelegate(this, function () { loadContentTypeCollection(createdList); }),Function.createDelegate(this, this.errorHandler));}// Loading content type colltionfunction loadContentTypeCollection(createdList){var web = getSPContext().get_web();var contentTypeCollection = web.get_contentTypes();var context = getSPContext();context.load(contentTypeCollection, 'Include(Name)');context.executeQueryAsync(Function.createDelegate(this, function () { addContentTypeToList(createdList); }),Function.createDelegate(this, this.errorHandler));}// Adding selected content type to the listfunction addContentTypeToList(createdList) {var selectedContentType = null;var selectedContentTypeValue = document.getElementById('contentType').value;var web = getSPContext().get_web();var contentTypeCollection = web.get_contentTypes();var contentTypeEnumerator = contentTypeCollection.getEnumerator();var context = getSPContext();while (contentTypeEnumerator.moveNext()) {if (contentTypeEnumerator.get_current().get_name() == selectedContentTypeValue) {selectedContentType = contentTypeEnumerator.get_current();break;}}if (selectedContentType != null) {createdList.get_contentTypes().addExistingContentType(selectedContentType);context.load(createdList, 'Title');}context.executeQueryAsync(Function.createDelegate(this, listCreationCompleted),Function.createDelegate(this, this.errorHandler));}// Success method after createionfunction listCreationCompleted() {alert('List creation successfully completed');document.getElementById('txtLibraryName').value = '';}// Error handing methodfunction errorHandler(sender, args) {alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());}// Get current sharepoint contextfunction getSPContext(){if (context == null) {context = SP.ClientContext.get_current();}return context;}//
Comments
Post a Comment
Your comments...