2

Code work whell before i add to code: items[i].get_item("SITE")

I can take ID from code, but not a column element. What have I do bad. Here is a code:

<input type='button' id='123' value='Pokaż rekordy' onclick="getSelectedItems();"/>
<div>
<p id="p1"></p>
</div>

<script language="javascript" type="text/javascript">
function getSelectedItems()
{
var ctx = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
var myItems = '';
var i;
var siteUrl = '/sites/MCUW-IT/kostka-pilotaze/restauracje-pilotaże/';
document.getElementById("p1").innerHTML="";
for (i in items){
    myItems = items[i].id;

    var targetListItem;
    document.getElementById("p1").innerHTML += items[i].id + " "**+ items[i].get_item("SITE")**;
    window.location.href = window.location.pathname + window.location.search + window.location.hash;

    updateListItem();

function updateListItem() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Testowa Lista');
    this.oListItem = oList.getItemById(myItems);

    oListItem.set_item('Poka_x017c_WWidoku', 'Tak');

    oListItem.update();

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Item updated!');
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}



}
}
</script> 

EDITED

It's don't work. Its don't giving me anything as return. See the code:

<input type='button' id='124' value='Pokaż' onclick="abc();"/>
<div>
<p id="p2"></p>
</div>
<script>
function abc() {

 getSelectedItems().then(function(items){
var i;
for (i in items){
    myItems = items[i].id;

    var targetListItem;
    document.getElementById("p2").innerHTML += items[i].id + " "+ items[i].get_item("SITE");
    window.location.href = window.location.pathname + window.location.search + window.location.hash;

    updateListItem();
}
}
}
</script>
7
  • 1
    var items = SP.ListOperation.Selection.getSelectedItems(ctx); This will return only key i.e. ids of selected item. If you want other calumn data you have to explicitly call using that id. Jul 27, 2018 at 9:06
  • but how. please write sample of code
    – Grzegorz Z
    Jul 27, 2018 at 9:11
  • 1
    This will help : sharepoint.stackexchange.com/questions/92436/… Jul 27, 2018 at 9:13
  • But how connect this with button and selected IDs?
    – Grzegorz Z
    Jul 27, 2018 at 9:17
  • 1
    Your requirement is to update column value to selected items right ? Jul 27, 2018 at 9:19

1 Answer 1

1
var items = SP.ListOperation.Selection.getSelectedItems(ctx)

This will only return key i.e. ID of selected items. To get value of other columns of selected items you have to explicitly query it. You can refer this

Answering your specific query : refer below code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
<script>
var abc = function()
{
getSelectedItems().then(function(items){
var myItems = '';
var i;
document.getElementById("p1").innerHTML="";
for (i in items){
    myItems = items[i].get_id();
    document.getElementById("p1").innerHTML += items[i].get_id() + " **"+ items[i].get_item("SITE")+"**";
    window.location.href = window.location.pathname + window.location.search + window.location.hash;
    var targetListItem;
    updateListItem(myItems);
    }
}
    )
}

function updateListItem(j) {
    var siteUrl = '/sites/MCUW-IT/kostka-pilotaze/restauracje-pilotaże/';
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Testowa Lista');
    this.oListItem = oList.getItemById(j);

    oListItem.set_item('Poka_x017c_WWidoku', 'Tak');

    oListItem.update();

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Item updated!');
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}


function getSelectedItems()
{
    var dfd = $.Deferred(function () {
       var context = SP.ClientContext.get_current();
       var listId = SP.ListOperation.Selection.getSelectedList(); //get selected list Id
       var selectedItemIds = SP.ListOperation.Selection.getSelectedItems(context); //get selected objects
       var list = context.get_web().get_lists().getById(listId);
       var listItems = [];
       for (idx in selectedItemIds)
       {
          var item = list.getItemById(parseInt(selectedItemIds[idx].id));
          listItems.push(item);
          context.load(item);
       }
       context.executeQueryAsync(
         function() {
            dfd.resolve(listItems);
         },
         function (sender, args) {
               dfd.reject(args.get_message());
         }
       ); 
    });
    return dfd.promise();   
}
</script>

<input id="123" onclick="abc();" type="button" value="Pokaż rekordy"/>
<div>
   <p id="p1">​​<br/></p>
</div>

On your button click call this abc function. Hope this helps !!

15
  • 1
    this in addition to code function of getSelectedItems() from shared reference Jul 27, 2018 at 10:22
  • 1
    Please try updated answer. Jul 27, 2018 at 10:36
  • 1
    what is the error that you are getting and where it is failing exactly Jul 27, 2018 at 10:52
  • 1
    You have to give jquery reference in order to use differed. Tip : My solution does work. Code may not work as is as its not tested and tried. Simply manipulated in editor to your reference . You have to debug and grab error and then rectify accordingly. Jul 27, 2018 at 11:22
  • 1
    let me give me exact code. Give me some time Jul 30, 2018 at 8:42

Your Answer

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

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