Below content will help to guide you on below things:
- How to get the host web url?
- How to check whether the list exists or not?
- How to create a list?
- How to create fields?
- How to add Items into the list?
- How to Read items from the list?
var context; var web; var user; var listname = 'MyList'; var oList; // This code runs when the DOM is ready. It ensures the SharePoint // script file sp.js is loaded and then executes sharePointReady() $(document).ready(function () { SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady); }); // This function creates a context object which is needed to use the SharePoint object model function sharePointReady() { var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl")); context = new SP.ClientContext.get_current(); hostcontext = new SP.AppContextSite(context, hostUrl); web = hostcontext.get_web(); getLists(); } //To get the lists available list in web function getLists() { this.lists = web.get_lists(); context.load(lists); context.executeQueryAsync(Function.createDelegate(this, this.IsListsExists), Function.createDelegate(this, this.onQueryFailed)); } //Below method to check if the lists exists or not? function IsListsExists() { alert('Check Whether the lists exists or not?'); var isListAvail = false; var listEnumerator = lists.getEnumerator(); while (listEnumerator.moveNext()) { list = listEnumerator.get_current(); if (list.get_title() == listname) { isListAvail = true; //Read list items from the lists getItems(); } } if (!isListAvail) { //Create a new list if not exists createList(); addItems(); } } //Create a list function createList() { alert('create a list if not exists'+'-'+ listname); var listCreationInfo = new SP.ListCreationInformation(); listCreationInfo.set_title(listname); listCreationInfo.set_templateType(SP.ListTemplateType.genericList); this.oList = web.get_lists().add(listCreationInfo); context.load(oList); //Add new fields to the list this.newColumn = oList.get_fields().addFieldAsXml("", true, SP.AddFieldOptions.defaultValue); this.newColumn = oList.get_fields().addFieldAsXml(" ", true, SP.AddFieldOptions.defaultValue); context.load(this.newColumn); } function addItems() { alert('Add items from the list'+ '-'+ listname ); var itemCreationInfo = new SP.ListItemCreationInformation(); this.oListItem = oList.addItem(itemCreationInfo); oListItem.set_item('Title','1'); oListItem.set_item('MyName','SasiKumarReddy'); oListItem.set_item('MyBlog', 'http://sharepointquicksolutions.blogspot.in/'); oListItem.update(); context.load(oListItem); context.executeQueryAsync(Function.createDelegate(this, this.getItems), Function.createDelegate(this, this.onGetItemsFail)); } //get Items function getItems() { alert('Read items from the list'+ '-'+ listname ); var selectedListTitle = web.get_lists().getByTitle(listname); var camlQuery = SP.CamlQuery.createAllItemsQuery(); this.listItemCollection = selectedListTitle.getItems(camlQuery); context.load(this.listItemCollection); context.executeQueryAsync(Function.createDelegate(this, this.onGetItemsSuccess), Function.createDelegate(this, this.onGetItemsFail)); } function onGetItemsSuccess() { alert('sucess'); var TextFiled = ""; var ListEnumerator = this.listItemCollection.getEnumerator(); while (ListEnumerator.moveNext()) { var currentItem = ListEnumerator.get_current(); TextFiled += currentItem.get_item('MyName')+ '-' + currentItem.get_item('MyBlog') + '\n'; } alert(TextFiled); } function onQueryFailed(sender, args) { alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); } //To get host web url function getQueryStringParameter(urlParameterKey) { var params = document.URL.split('?')[1].split('&'); var strParams = ''; for (var i = 0; i < params.length; i = i + 1) { var singleParam = params[i].split('='); if (singleParam[0] == urlParameterKey) return decodeURIComponent(singleParam[1]); } }
No comments:
Post a Comment