Introduction
I was developing a SharePoint App using SharePoint JavaScript Object Model which also called SharePoint ECMAScript Client Object Model. Today I will show how to retrieve basic properties from SharePoint list.Solution
- function getSPListProperties() {
- //Get the current context. Same as SPContext in server side
- var ctx = SP.ClientContext.get_current();
- //Get the web
- var web = ctx.get_web();
- //Get the list by its title
- var list = web.get_lists().getByTitle('Title of the List Instance');
- //Load the properties which we need
- ctx.load(list, 'Title', 'Id', 'Created', 'ItemCount');
- //Excecute the loded queries asyncronisly
- ctx.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
- //Success method
- function onQuerySucceeded() {
- alert(list.get_title());
- alert(list.get_id());
- alert(list.get_created());
- alert(list.get_itemCount());
- }
- //Falied method
- function onQueryFailed() {
- alert('failed');
- }
- }
Conclusion
Please note that when you get the list through the method web.get_lists().getByTitle('Title of the List Instance'); It doesn’t load all the properties like in server side. We have to explicitly load the properties which we need.When you didn’t load for a property, and try to call the method in order to get the data it will throw an exception.
Reference:
http://msdn.microsoft.com/en-us/library/office/jj245759(v=office.15).aspx#constructors
No comments:
Post a Comment