// we need both calls because in some situations executeOrDelayUntilScriptLoaded doesn't work
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { });
SP.SOD.executeOrDelayUntilScriptLoaded(function () {
// code goes here
}, 'sp.js');
var spContext = SP.ClientContext.get_current(),
spWeb = spContext.get_web();
The first parameter of load function is an object that contains the properties to retrieve from the server. Other parameters are optional and contain names of additional properties you want to retrieve.
In our case you need to get AllProperties property from the server:
spContext.load(spWeb, 'AllProperties');
var allProps = spWeb.get_allProperties(),
// actually allProps.get_fieldValues() contains key-value pairs of the Property Bag
allPropsValues = allProps.get_fieldValues(),
// value of the specific key
value = allPropsValues[key];
allProps.set_item(key, value);
spWeb.update();
spContext.executeQueryAsync(function () {
// some code
}, function () {
//some logging code
});
Full test code looks like that:
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { });
SP.SOD.executeOrDelayUntilScriptLoaded(function () {
var spContext = SP.ClientContext.get_current(),
spWeb = spContext.get_web();
spContext.load(spWeb, 'AllProperties');
spContext.executeQueryAsync(function () {
var allProps = spWeb.get_allProperties(),
key = 'testKey',
// actually allProps.get_fieldValues() contains key-value pairs of the Property Bag
allPropsValues = allProps.get_fieldValues(),
// value of the specific key
value = allPropsValues[key];
allProps.set_item(key, value);
spWeb.update();
spContext.executeQueryAsync(function () {
// some code
}, function () {
//some logging code
});
});
}, 'sp.js');
Have fun!
Comments