Showing posts with label JSOM. Show all posts
Showing posts with label JSOM. Show all posts

Thursday, December 15, 2016

Promises example in JS and JSOM

 function getListItemByID(listName, itemID) {
return new Promise(function(resolve, reject) {
//debugger;
context = SP.ClientContext.get_current();
        web = context.get_web();
        oList = web.get_lists().getByTitle(listName);

        var currentItem = oList.getItemById(itemID);

        context.load(currentItem);
        context.executeQueryAsync(
            function () {
                resolve(currentItem);
            },
            function (sender, args) {
                reject(args.get_message());
            }
        );
});
};


getListItemByID("Milestones",843)
.then(row=>getListItemByID("Process List",row.get_item("Process_x0020_ID")))
.then(row=>console.log(row.get_item("Process_x0020_Name")))
.catch(error=>console.error(error))



Sunday, October 16, 2016

Thursday, September 15, 2016

simple jsom to get an item

Utils.getListItemByID = function (listName, itemID ){
var dfd =  $.Deferred();
context = SP.ClientContext.get_current();
web = context.get_web();
oList = web.get_lists().getByTitle(listName);

var currentItem = oList.getItemById(itemID);

context.load(currentItem);
context.executeQueryAsync(
function(){
dfd.resolve(currentItem);
},
function(sender,args){
dfd.reject(args.get_message());
}
);
return dfd.promise();
}


Utils.getListItemByID('blah', 36).then(function(item){ console.log(item.get_item('blahName')) },function(sender, args){ console.log(args.get_message())});

Tuesday, September 13, 2016

nice pattern for jsom async calls


pass the call backs as parameters to the function



function getSiteField(fieldName,success,failure)
{
     var ctx = SP.ClientContext.get_current();
     var rootWeb = ctx.get_site().get_rootWeb();
     var field = rootWeb.get_availableFields().getByInternalNameOrTitle(fieldName);
     ctx.load(field);
     ctx.executeQueryAsync(
         function(){
            success(field)
         },
         failure);
}
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {

   //get Title column and print its properties
   getSiteField('Title',
    function(field){
       //print field properties
       console.log(field.get_title());
       console.log(field.get_internalName());
       console.log(field.get_typeAsString());
       console.log(field.get_description());
    },
    function(sender,args){
       console.log(args.get_message());
    });

});

got it here

Thursday, August 25, 2016

taxonomy

i was getting cant do toString on null erros when accessing taxonomy.

I thought the problem was that there wasn't a default taxanomy - but the real reason was I forget to add a reference to taxonomy.js