0

I use sharepoint online and this is going to be an app

I need to loop through an array to get all items that are selected in a list, now i can only get one item but i need to get more items at once to do that i have .split(',') on this row:

itemId = decodeURIComponent(getQueryStringParameter("SPListItemId")).split(',');

and i need an loop to go through all items that are selected

var i, array;
for (i = 0; i > array.length; i++)
    {


    }

and this is all code:

'use strict';

var clientContext, hostweburl, parentContext, parentWeb, selectedItem, itemId, listId;

clientContext = new SP.ClientContext.get_current();
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
listId = decodeURIComponent(getQueryStringParameter("SPListId"));
itemId = decodeURIComponent(getQueryStringParameter("SPListItemId")).split(',');
parentContext = new SP.AppContextSite(clientContext, hostweburl);
parentWeb = parentContext.get_web();
selectedItem = parentWeb.get_lists().getById(listId).getItemById(itemId);


$(document).ready(function () {
    clientContext.load(selectedItem);
    clientContext.executeQueryAsync(OnSuccess, OnFail);
});



function OnSuccess() {

var i, array;
for (i = 0; i > array.length; i++)
    {


    }




$("#Blue").click(function () {

    selectedItem.set_item('Color', 'Blue');
    selectedItem.update();
    clientContext.load(selectedItem);
    clientContext.executeQueryAsync(function () { window.parent.postMessage('CloseCustomActionDialogRefresh', '*'); }, function (sender, args) { alert('Error:' + args.get_message()); });
});

$("#Green").click(function () {

    selectedItem.set_item('Color', 'Green');
    selectedItem.update();
    clientContext.load(selectedItem);
    clientContext.executeQueryAsync(function () { window.parent.postMessage('CloseCustomActionDialogRefresh', '*'); }, function (sender, args) { alert('Error:' + args.get_message()); });
});

$("#Red").click(function () {

    selectedItem.set_item('Color', 'Red');
    selectedItem.update();
    clientContext.load(selectedItem);
    clientContext.executeQueryAsync(function () { window.parent.postMessage('CloseCustomActionDialogRefresh', '*'); }, function (sender, args) { alert('Error:' + args.get_message()); });
});

$("#Transparent").click(function () {

    selectedItem.set_item('Color', 'No Color');
    selectedItem.update();
    clientContext.load(selectedItem);
    clientContext.executeQueryAsync(function () { window.parent.postMessage('CloseCustomActionDialogRefresh', '*'); }, function (sender, args) { alert('Error:' + args.get_message()); });
});

}


function OnFail() {
alert("Error");
}


function getQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
    var singleParam = params[i].split("=");
    if (singleParam[0] == paramToRetrieve)
        return singleParam[1];
}
}

NOTE i can't use SP.ListOperation.Selection.getSelectedItems(clientContext); because i will not have my code on the host web just on the app web and then this will not work

1

i think what your looking for is:

var selectedItms = SP.ListOperation.Selection.getSelectedItems(clientContext);

Gets the list items being selected.

http://msdn.microsoft.com/en-us/library/office/ff409526(v=office.14).aspx

so it would look like this(change your for code for this one including changeing the selectedItems for this):

  var selectedItms = SP.ListOperation.Selection.getSelectedItems(clientContext);
  var itm;

  //loop though all selected items
  foreach (itm in selectedItms) 
  {
     //print to screen the current selected item
     alert(selectedItms[itm].id);
  }
  • can't use that need to loop trought all items – linkan94 Sep 23 '14 at 13:29
0

Answer:

listId = decodeURIComponent(getQueryStringParameter("SPListId"));
itemIds = decodeURIComponent(getQueryStringParameter("SPListItemId")).split(',');

var i;
for (i = 0; i < itemIds.length; i++)
{
    selectedItems = parentWeb.get_lists().getById(listId).getItemById(itemIds[i]);
    selectedItems.set_item('Color', color);
    selectedItems.update();
    clientContext.load(selectedItems);
}
  clientContext.executeQueryAsync(function () { }, function (sender, args) { alert('Error:' + args.get_message()); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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