Friday, May 30, 2014

Get SPList items (by CAML query) using JavaScript in SharePoint Apps

Introduction

Today I will show how to get SPList Items by filtering using CAML query. Also this post includes how to get the data from a custom field.

Solution Background

I use JQuery.Deferred and Promise in order to achieve the success and failure of the asynchronous call. There are plenty of articles on the internet regarding JQuery.Deferred. Please see the Reference section for more on this.

Solution

  1.   function query() {
  2.  
  3.             var camlQuery =
  4.                 "<Where>" +
  5.                 "<Neq>" +
  6.                 "<FieldRef Name='CustomFieldOne' />" +
  7.                 "<Value Type='Text'>Suhail Jamaldeen</Value>" +
  8.                 "</Neq>" +
  9.                 "</Where>";
  10.  
  11.             //Calling the method getListItemsByCAMLAsync.
  12.             //Here the fail and done methods are from the deffered object which is returning.
  13.             getListItemsByCAMLAsync('NameOfList', camlQuery, 'Include(DisplayName, Id, CustomFieldOne)')
  14.                     .fail(function (e) {
  15.                         alert((handeler.parseSPException(e)));
  16.                     }).done(function (items) {
  17.                         var enu = items.getEnumerator();
  18.                         while (enu.moveNext()) {
  19.                             var cu = enu.get_current();
  20.                             var id = cu.get_id();//Read the Id
  21.                             alert(id);
  22.                             var prope = cu.get_fieldValues();//You can see the fields which you are retreiving
  23.                             var initiator = cu.get_fieldValues().CustomFieldOne;//Readvalue from custom property (Single line)
  24.                             alert(initiator);
  25.                         }
  26.                     });
  27.  
  28.         };
  29.  
  30.         //This returns a deferred object.
  31.         function getListItemsByCAMLAsync(listName, query, fieldsFilter) {
  32.             var def = $.Deferred();
  33.             var clientContext = new SP.ClientContext();
  34.             var oWeb = clientContext.get_web();
  35.             var ol = oWeb.get_lists().getByTitle(listName);
  36.             var qry = new SP.CamlQuery();
  37.             qry.set_viewXml(query);
  38.             var items = ol.getItems(qry);
  39.  
  40.             clientContext.load(items, fieldsFilter);
  41.             clientContext.executeQueryAsync(function () {
  42.                 def.resolve(items);
  43.             },
  44.                 function (a, b) {
  45.                     def.reject(b);
  46.                 });
  47.             return def.promise();
  48.         };

Reference

http://sharepoint.stackexchange.com/questions/69878/how-to-use-client-object-model-javascript-to-get-a-custom-user-profile-propert

http://api.jquery.com/category/deferred-object/

http://www.htmlgoodies.com/beyond/javascript/making-promises-with-jquery-deferred.html

Conclusion

Should say Melick (http://melick-rajee.blogspot.com) for the guide on SharePoint Apps.

3 comments:

  1. hello brother please give email id coz i m new in sharepoint please help me about useing jquery insert update and delete by javascript and jquery, and my email id is subhan29@gmail.com Please help me......

    ReplyDelete
    Replies
    1. You may use and Share the below blog post.

      http://jsuhail.blogspot.com/2014/06/add-update-and-delete-splistitem-in.html

      Delete
  2. Better use my skype instead of email; j.suhail is my skype id; Meanwhile I will write a post with the basics on this.

    ReplyDelete