0

I need to pass from one site to another number of site and shiow this record on the View. If I passing record by ID it's all work great, but when I pass by caml Query nothing happening. I tried many times and use u2u to, but when I use camlQuery nothing showing, here is my 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();
    site = items[i].get_item("SITE");
    document.getElementById("p1").innerHTML += items[i].get_id() +" "+ site +" "+ items[i].get_item("Title")+" "+ items[i].get_item("kierownik")+" "+ items[i].get_item("konsultant")+"<br />";
   // window.location.href = window.location.pathname + window.location.search + window.location.hash;
    var targetListItem;
    updateListItem(myItems,site);
    }
}
    )
}

function updateListItem(j,site) {
    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');
    // var collListItem = oList.getItemById(j);

    var queryString ="<View><Query><Where><Contains><FieldRef Name="Connect" /><Value Type="LookupMulti">5</Value></Contains></Where></Query></View>";
        var query = new SP.CamlQuery();
        query.set_viewXml(queryString);
        collListItem = oList.getItems(query);


    collListItem.set_item('Poka_x017c_WWidoku', 'Tak');
    collListItem.update();
    clientContext.load(collListItem);

    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>

Please help me. I must pass number of site, not id number because I need to find if column connect has got a column with has got mamy number of sites.

EDITED

I tried this bottom code with enumerator, but button still don't fire:

  <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();
    site = items[i].get_item("SITE");
    document.getElementById("p1").innerHTML += items[i].get_id() +" "+ site +" "+ items[i].get_item("Title")+" "+ items[i].get_item("kierownik")+" "+ items[i].get_item("konsultant")+"<br />";
   // window.location.href = window.location.pathname + window.location.search + window.location.hash;
    var targetListItem;
    updateListItem(myItems,site);
    }
}
    )
}

function updateListItem(j,site) {
    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');
    // var collListItem = oList.getItemById(j);

    var queryString ="<View><Query><Where><Contains><FieldRef Name="Connect" /><Value Type="LookupMulti">5</Value></Contains></Where></Query></View>";
        var query = new SP.CamlQuery();
        query.set_viewXml(queryString);
        collListItem = oList.getItems(query);

    clientContext.load(collListItem, 'Include(Connect)');
    collListItem.set_item('Poka_x017c_WWidoku', 'Tak');
    collListItem.update();


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

function onQuerySucceeded() {

 var listEnumerator = listItems.getEnumerator();
            while (listEnumerator.moveNext()) {

                // get the current list item.
                var listItem = listEnumerator.get_current();

                // get the field value.
                var fieldValue = listItem.get_item('Connect');


                alert('Item updated!'+fieldValue);

    }
}

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>
0
oList.getItems(query); 

will return item collection. You need to explicitly load and execute this collection and then iterate through all item one by one to achieve.

Below code for your reference:

<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) {
    getItemstoUpdate().then(function(itemsToUpdate,ctx,list){
var tempi = 0;      
         var itemArray = [];
        var listItemEnumerator = itemsToUpdate.getEnumerator();
         while (listItemEnumerator.moveNext()) {
            var oItem = listItemEnumerator.get_current();
            var oListItem = list.getItemById(oItem.get_id());
            oListItem.set_item('Poka_x017c_WWidoku', 'Tak');  
            oListItem.update();
            itemArray[tempi]=oListItem;
            ctx.load( itemArray[tempi]);
             tempi++;
    }
          ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    })
}

function onQuerySucceeded(sender, args) {
   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();   
}


function getItemstoUpdate()
{
    var dfd = $.Deferred(function () {
        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');
        var query = new SP.CamlQuery();
               var listItems = [];
        query.set_viewXml('<View><Query><Where><Contains><FieldRef Name="Connect" /><Value Type="LookupMulti">5</Value></Contains></Where></Query></View>');
    var collListItem = oList.getItems(query);

    clientContext.load(collListItem,'Include(Id, DisplayName)');
       clientContext.executeQueryAsync(
         function() {
            dfd.resolve(collListItem,clientContext,oList);
         },
         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>

If two items selected then item will be updated twice. Please check modify accordingly.

  • But in Succes i haven't seen this: collListItem.set_item('Poka_x017c_WWidoku', 'Tak'); collListItem.update(); How BhaskarDhone I can use this two liness when success – Grzegorz Z Jul 31 '18 at 13:59
  • Please Bhaskar Dhone look on my Edited code. I add a Iterator, but the button don't firred. Somewere there is a javascript mistake. – Grzegorz Z Jul 31 '18 at 14:24
  • 1
    let me give you exact code to try – Bhaskar Dhone Jul 31 '18 at 14:36
  • Can You Bhaskar Dhone look for code? Please – Grzegorz Z Aug 1 '18 at 7:05
  • Look my edited Code – Grzegorz Z Aug 1 '18 at 7:17

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.