Showing posts with label jsom. Show all posts
Showing posts with label jsom. Show all posts

Sunday, 26 March 2017

Run executeQueryAsync() synchronously SharePoint 2013

In JSOM, to fetch the data from the server we make a call through executeQueryAsync(). This works asynchronously by default.We need to make executeQueryAsync() behave synchronously.

That means we have to make this function wait till the operation gets completed and should return some values , then proceed to the next function.

This can be done by using JavaScript callbacks and deferred/Promises.
The promises pattern significantly simplifies JavaScript code when you make an app, which has multiple, nested asynchronous calls and it makes it very powerful.



<script type="text/javascript">
    var camlValues;
    $(document).ready(function () {
             
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getItems);
    });
 
    function getItems() {
        getListItem('Test list').done(function()
{
        console.log(“Second Console Pass”);  
}).fail(function()
{
 console.log(“Second Console Fail”);  
});            
    }
 
    function getListItem(listTitle) {

      var deferred=$.Deferred();
        var clientContext = new SP.ClientContext.get_current();
        var olist = clientContext.get_web().get_lists().getByTitle(listTitle);
        var camlQuery = new SP.CamlQuery();
        ocamlItems = olist.getItems(camlQuery);
        clientContext.load(camlValues);
        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.success),
            Function.createDelegate(this, this.failure)
        );

return deferred.promise();
    };
 
    function success(sender, args) {
        var listItemEnumerator = ocamlItems.getEnumerator();
        while (listItemEnumerator.moveNext()) {
            var olistItem = listItemEnumerator.get_current();
console.log(‘First Console Pass ‘);
             
deferred.resolve(olistItem );
        }
    }
 
    function onQueryFailed(sender, args) {
        console.log('An error occured while retrieving list items:' + args.get_message());

deferred.reject(olistItem );
    }
</script>





For the code given above, we will get the output, as shown below.

First Console Pass
Second Console Pass


Thanks.. Happy Coding..

Sunday, 30 August 2015

How to programmatically get the Titles and Guids of all the lists in a Sharepoint site.

Hi all, Below code is used to get the details of all lists in the Site using CSOM.
I have logged the details in console. You can manipulate the data as per the requirement.

$(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext');
    retrieveAllListProperties();
});


function retrieveAllListProperties() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    this.collList = oWebsite.get_lists();
    clientContext.load(collList);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),       Function.createDelegate(this, this.onQueryFailed));

}

function onQuerySucceeded() {

    var listInfo = '';
    var listEnumerator = collList.getEnumerator();
    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        listInfo += 'Title: ' + oList.get_title() + ' Guid: ' + oList.get_id().toString() + '\n';
    }
    console.log(listInfo);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}


Happy Coding :)

Sunday, 26 July 2015

Get current user using javascript - SharePoint client object model

Hi All,

  In this post we will see about, getting the loggedin Username using Javascript only.
In Client object model , to get the value of the loggedin User, mostly everyone will go for $().SPServices.SPGetCurrentUser. SP Services ibrary has tons of uses and is very easier.
But we can do this without referencing a third party library, if your requirement is like so.

Below given is the code for it.

function getCurrentUser()
{
var context = new SP.ClientContext.get_current();
this.website = context.get_web();
this.currentUser = website.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}



function onQuerySucceeded(sender, args)
 {
 alert(currentUser.get_loginName());
 }



function onQueryFailed(sender, args)
{
alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
}

Hope this helps somewhere. Happy coding..