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>
alert
and do proper development using the F12 console... You are now trying to cycle on the Highway.... you will get killed