Skip to content

Instantly share code, notes, and snippets.

@koohq
Created November 12, 2017 10:15
Show Gist options
  • Save koohq/17fd06d5d6ae56b9e8d0807320da3de6 to your computer and use it in GitHub Desktop.
Save koohq/17fd06d5d6ae56b9e8d0807320da3de6 to your computer and use it in GitHub Desktop.
Get/Update for SPListItem at DispForm
/**
* sp-contextitemhelper.js
* Provide functions that manupilates the context list item (displayed now).
* Assumed for showing/updating hidden fields at the browser.
*
* (c) 2017 koohq. Licensed under CC0.
* https://creativecommons.org/publicdomain/zero/1.0/legalcode
*/
var SPContextItemHelper = (function() {
var clientCtx = SP.ClientContext.get_current();
var list = clientCtx.get_web().get_lists().getById(new SP.Guid(_spPageContextInfo.pageListId));
var item = list.getItemById(parseInt(GetUrlKeyValue('ID'), 10));
function select() {
clientCtx.load(item);
clientCtx.executeQueryAsync(function() {
console.log(item.get_fieldValues());
}, function(sender, args) {
console.error('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
});
}
function update(obj) {
Object.keys(obj).forEach(function(key) { item.set_item(key, obj[key]); });
item.update();
clientCtx.executeQueryAsync(function() {
console.log('Item updated.');
}, function(sender, args) {
console.error('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
});
}
return {
select: select,
update: update
};
})();
/**
* sp-contextitemhelper.usage.js
*
* (c) 2017 koohq. Licensed under CC0.
* https://creativecommons.org/publicdomain/zero/1.0/legalcode
*/
// Show the field values on console (contains hidden fields)
ContextItemHelper.select();
// Update the fields values on console (contains hidden fields)
ContextItemHelper.update({ VisibleField: 'xxx', HiddenField: 'yyy' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment