I am trying to develop My Favourite functionality in Sharepoint using the below approach.

  1. Create My Favourite list
  2. Create a custom script that will read the selection of the list item (any other list in the site)
  3. Based on number of items selected, get the id of each item and save the details to the My Favourite list.

I have done the below code, which works perfectly fine if only one item is selected, but if multiple items are selected then the same item get added again and again.

    var item=[];
var  index=0;
var listItems = []
var context;
function getSelectedItems()
    context = SP.ClientContext.get_current();
    var listId = SP.ListOperation.Selection.getSelectedList(); //selected list Id
    var selectedItemIds = SP.ListOperation.Selection.getSelectedItems(context); //selected Items Ids

    var list = context.get_web().get_lists().getById(listId);

    for (idx in selectedItemIds)
        var itemID=selectedItemIds[idx].id;

    for (index = 0; index < listItems.length; ++index) 

        item = list.getItemById(listItems[index]);

        context.executeQueryAsync                                                                                                                                                                             (Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)


function onQuerySucceeded(sender, args) {            

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

function addToFavList()
    var clientContext = new SP.ClientContext("/sites/somesite");
    var oList = clientContext.get_web().get_lists().getByTitle('My Favourite');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);

    oListItem.set_item('Title', item.get_item("Document_x0020_Title"));

            var Document_Number = new SP.FieldUrlValue();
            oListItem.set_item("Document_x0020_Number", Document_Number);



    Function.createDelegate(this, this.onItemAddedSuccess), 
    Function.createDelegate(this, this.onItemAddedFailed)); 

function onItemAddedSuccess() {

    alert('Item created: ' + oListItem.get_id());

Issue here is that, by the time Item is added the for loop continues and also by the time item details are fetched the item adding continues and hence it ends up in creating as many item as selected by the user but all the item in My Favourite contains same details.

Is there any way i can handle this issue? Adding items or fetching items Synchronously? I really dont know how to proceed. Kindly help.

1 Answer 1


You will need to use jQuery Deferred object with SharePoint Javascript Object Model to resolve this issue. Example:

function successCallback() {

function failCallback() {
    this.d.reject("something bad happened");

function getList(name) {
    var d = $.Deferred();

    var clientContext = SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle(name);

    var o = {d: d, list:list};
    clientContext.executeQueryAsync(Function.createDelegate(o, successCallback), Function.createDelegate(o, failCallback));

    return d.promise();

var p = getList("Documents");
p.done(function(result) {
    // result is an SP.List because that is what we passed to resolve()!
    var list = result;
    // do something with the list
p.fail(function(result) {
    // result is a string because that is what we passed to reject()!
    var error = result;

Source: http://blog.qumsieh.ca/2013/10/31/using-jquery-promises-deferreds-with-sharepoint-2013-jsom/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.