5

The functions I need are not complex but I did not succeded to make it work on my sharepoint, hence my posting here.

I have a list of items and a Yes/No column. I want that the user get the possibility to select multiple items and by clicking a button to set the Y/N column to yes for theses selected items.

So far I have this code that manage to get the IDs of the element selected but I dont succeed to update the Y/N field using these IDs.

At the moment, the code is into a Script Editor above my grouped list.

<script type="text/javascript"></script>
<script>
function SetToYes() {
var ctx = SP.ClientContext.get_current();
var selectedItems  = SP.ListOperation.Selection.getSelectedItems(ctx);
var mySeletedItems = '';
var i;

 for (i in selectedItems )
 {
      mySeletedItems += '|' + selectedItems [i].id; // '+= means x += y same as x = x + y
 // I think setting the value of the Y/N field shall happen here using the id of the field selected selectedItems [i].id
 }
alert (mySeletedItems); //just for me to see that the code works properly
}
</script>

<html>
<input id="GetIDselected" type="button" value="Get ID of selected items" onclick="SetToYes();"></input>
</html>

I tried with ctx.currentitem or selecteItems[i].nameOfmyYNfield but no luck so far.

EDIT 1: Ok, new day, new code. I am now using that one to do the update of my Yes/No field but there is an error at the update(). Else I manage to get the item I want (at the moment locked on ID=2 for conveniance) and to read the Yes/No field value.

<script type="text/javascript"></script>
<script>
var itemId = 2;
var targetListItem;

function SetToYes() {
     var clientContext = SP.ClientContext.get_current(); 
     var targetList = clientContext.get_web().get_lists().getByTitle('Reporting Level 1+2');
     targetListItem = targetList.getItemById(itemId);
     clientContext.load(targetListItem, 'Topic_x0020_ECN_x0020_OPS');
     clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
   function onQuerySucceeded() {
       var ListItem;
       alert('Request succeeded. \n\nRetrieved Item is: ' + targetListItem.get_item('Topic_x0020_ECN_x0020_OPS'));
        //targetListItem['Topic_x0020_ECN_x0020_OPS']="1";
       targetListItem.set_item('Topic_x0020_ECN_x0020_OPS',true);
       targetListItem.Update();
   }
   function onQueryFailed(sender, args) {
     alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
   }
</script>

<input id="GetIDselected" type="button" value="Get ID of selected items" onclick="SetToYes();"></input>

The console log tell me the update() method is not allowed on my variable "targetListItem" and I dont get why when I compare to other update scripts found on internet. Any clues?

EDIT2: My final working code, with 2 buttons. 1 to mark as yes, the other as no. It might be usefull to others in the futur.

the last code:

<script type="text/javascript"></script>
<script>
var targetListItem;

function SetToECNOPS(YesOrNo){
    var clientContext = SP.ClientContext.get_current();
    var selectedItems  = SP.ListOperation.Selection.getSelectedItems(clientContext);
    var targetList = clientContext.get_web().get_lists().getByTitle('Reporting Level 1+2');
    var itemArray = [];
    for (var i = 0; i < selectedItems.length; i++) {            

        targetListItem = targetList.getItemById(selectedItems[i].id);
        targetListItem.set_item('Topic_x0020_ECN_x0020_OPS',YesOrNo);
        targetListItem.update();
        itemArray[i] = targetListItem;
        clientContext.load(itemArray[i]);
    }    
    clientContext.executeQueryAsync(function(){
        console.log("success update");
    },function(){
        console.log('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
    });
//refresh page to see the changes
window.parent.location.href = window.location.href;
}
</script>

<input id="SeToYes" type="button" value="Add to ECN OPS" onclick="SetToECNOPS(1);"></input>
<input id="SetToNo" type="button" value="Remove from ECN OPS" onclick="SetToECNOPS(0);"></input>
2
  • 1
    First learn to not use alert and do proper development using the F12 console... You are now trying to cycle on the Highway.... you will get killed Commented Jun 15, 2017 at 19:44
  • I know I schould use console.log or a dialog, but as far as I know, the alert is not hurting the code as I debugg it. Later they will of course be removed.
    – MüdeFuchs
    Commented Jun 16, 2017 at 6:42

1 Answer 1

6

Have modified your code, you need to update it as below.

Couple of things, get rid of alert and function.createdelegate

var itemId = 2;
var targetListItem;

function SetToYes() {
     var clientContext = SP.ClientContext.get_current(); 
     var targetList = clientContext.get_web().get_lists().getByTitle('Reporting Level 1+2');
     targetListItem = targetList.getItemById(itemId);
     targetListItem.set_item('Topic_x0020_ECN_x0020_OPS',1);
     targetListItem.update();     
     clientContext.executeQueryAsync(function(){
            console.log("success update");
     },function(sender, args){
        console.log('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
     });
}

Now, to update selected list items, you can use the below code:

function SetToYes(){
    var clientContext = SP.ClientContext.get_current();
    var selectedItems  = SP.ListOperation.Selection.getSelectedItems(clientContext);
    var targetList = clientContext.get_web().get_lists().getByTitle('Reporting Level 1+2');
    var itemArray = [];
    for (var i = 0; i < selectedItems.length; i++) {            

        targetListItem = targetList.getItemById(selectedItems[i].id);
        targetListItem.set_item('Topic_x0020_ECN_x0020_OPS',1);
        targetListItem.update();
        itemArray[i] = targetListItem;
        clientContext.load(itemArray[i]);
    }    
    clientContext.executeQueryAsync(function(){
        console.log("success update");
    },function(sender, args){
        console.log('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
    });
}
6
  • @Romain - my bad, to set the yes/no field, you need to set the value as 1 or 0. So, to set value, it needs to be targetListItem.set_item('YesNoFieldInternalName', 1); , have updated code accordingly Commented Jun 16, 2017 at 9:06
  • thanks for your answer, I try to implement it now but console says oListItem is not defined (itemArray[i] = oListItem;). it need to be replaced by targetListItem I assume?
    – MüdeFuchs
    Commented Jun 16, 2017 at 9:15
  • Have updated the code, can you check now ? Yes, you are correct, my bad, sorry Commented Jun 16, 2017 at 9:20
  • Yes, it works! Now I just need to refresh my list after the update but I think I will easily find the needed code. Thanks a lot for your support Gautem!! By the way, can you shortly explain what does "itemArray[i]"?
    – MüdeFuchs
    Commented Jun 16, 2017 at 9:25
  • welcome , glad i could help :) Commented Jun 16, 2017 at 9:25

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.