SharePoint: Accessing List Items using ECMAScript
Here is a simple, slim down with no frills attached code to access list item using ECMAscript in SharePoint 2010/13 . You can simply use this code in Content Editor Webpart and trigger this function using a HTML button or something and display the results in a HTML.
Here I am trying to access all items under all the fields of the list. In the onSuccess function which executed in case the list items are accessed successfully, I further call another method that will make use of the
function GetListItems() { try { setNewEventLinkURL(); objContext = new SP.ClientContext.get_current(); objWeb = objContext.get_web(); objList = objWeb.get_lists().getByTitle("My List"); objContext.load(objList); objContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFail)); } catch (e) { } } function onSuccess(sender, args) { var TextFiled = ""; //var ListEnumerator = objAllItems.getEnumerator(); alert('Success! Item Count: ' + objList.get_itemCount()); //_getDays(objList); } //Incase the list data could not be accessed function onFail(sender, args) { alert('Some error has occured. ' + args.get_message()); }A word of advice: I deployed this code from a visual Studio SharePoint Project. I added a module to this Project and wrote my ecmaScript inside. For that reason, ExecuteOrDelayUntilScriptLoaded(GetListItems, "sp.js") to call this function. That means first sp.js will load and them GetListItems(). Hope this helps Cheers
Comments
Post a Comment