Monday, May 23, 2016

JSOM sharepoint 2013 List and listitem selection by Javascript object model


1.  Fetching data from same farm but from any site application. And showing it.
 ------------------------------------
var allItems = null;
function retrieveListItemsInclude() {
        SP.SOD.executeOrDelayUntilScriptLoaded( retrieveListItemsInclude, "/_layouts/SP.js");       
        var ctx = new SP.ClientContext('http://xxxxx/sites/pms2015');
        var list = ctx.get_web().get_lists().getByTitle('Active Appraisals');
        var query = new SP.CamlQuery();       
        query.set_viewXml('<View><Query><Where><And>'+
                '<Eq><FieldRef Name="StaffID" /><Value Type="Text">92407</Value></Eq>'+
                  '<Eq><FieldRef Name="AppraisalYear" /><Value Type="Text">2015</Value></Eq>'+
                '</And></Where></Query>'+
                '<RowLimit>10</RowLimit></View>');
        allItems = list.getItems(query);
        ctx.load(allItems);
        ctx.executeQueryAsync(retrieveListItemsSuccess, retrieveListItemsFail);
}


 function retrieveListItemsSuccess(sender, args) {
         var enumerator = allItems.getEnumerator();
         var markup = 'Items in the list: <br><br>';
         //Loop through all the items
         while (enumerator.moveNext()) {
         var listItem = enumerator.get_current();
         markup += '<a href="' + listItem.get_item('FormLink').get_url() + '" target="_blank">' + listItem.get_item('FormLink').get_description() + '</a>'; 
         markup += 'Item ID: ' + listItem.get_id() + '<br><br>'; 
      } 
      displayDiv.innerHTML = markup; 
   }

  
   function retrieveListItemsFail(sender, args) { 
      var markup = '<p>The request failed: <br>'; 
      markup += 'Message: ' + args.get_message() + '<br>'; 
      displayDiv.innerHTML = markup;
   }

  

No comments:

Post a Comment