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
- function query() {
- var camlQuery =
- "<Where>" +
- "<Neq>" +
- "<FieldRef Name='CustomFieldOne' />" +
- "<Value Type='Text'>Suhail Jamaldeen</Value>" +
- "</Neq>" +
- "</Where>";
- //Calling the method getListItemsByCAMLAsync.
- //Here the fail and done methods are from the deffered object which is returning.
- getListItemsByCAMLAsync('NameOfList', camlQuery, 'Include(DisplayName, Id, CustomFieldOne)')
- .fail(function (e) {
- alert((handeler.parseSPException(e)));
- }).done(function (items) {
- var enu = items.getEnumerator();
- while (enu.moveNext()) {
- var cu = enu.get_current();
- var id = cu.get_id();//Read the Id
- alert(id);
- var prope = cu.get_fieldValues();//You can see the fields which you are retreiving
- var initiator = cu.get_fieldValues().CustomFieldOne;//Readvalue from custom property (Single line)
- alert(initiator);
- }
- });
- };
- //This returns a deferred object.
- function getListItemsByCAMLAsync(listName, query, fieldsFilter) {
- var def = $.Deferred();
- var clientContext = new SP.ClientContext();
- var oWeb = clientContext.get_web();
- var ol = oWeb.get_lists().getByTitle(listName);
- var qry = new SP.CamlQuery();
- qry.set_viewXml(query);
- var items = ol.getItems(qry);
- clientContext.load(items, fieldsFilter);
- clientContext.executeQueryAsync(function () {
- def.resolve(items);
- },
- function (a, b) {
- def.reject(b);
- });
- return def.promise();
- };
Reference
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.
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......
ReplyDeleteYou may use and Share the below blog post.
Deletehttp://jsuhail.blogspot.com/2014/06/add-update-and-delete-splistitem-in.html
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