HOW TO: Add properties to SPWeb property bag using JSOM in SharePoint 2013
This post is a contribution from Jaishree Thiyagarajan, an engineer with the SharePoint Developer Support team.
Here’s the sample.
Create a simple HTML page and add the below scripts.
<html>
<head>
<title></title>
<script
src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"
type="text/javascript">
</script>
<script
type="text/javascript"
src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js">
</script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function () {
var hostweburl = "https://siteURL/";
var scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", execOperation);
}
);
});
function execOperation() {
AddWebSiteProperties("https://siteURL");
}
function AddWebSiteProperties(siteUrl) {
var clientContext = new SP.ClientContext(siteUrl);
oWebsite = clientContext.get_web();
alert(oWebsite);
clientContext.load(oWebsite);
alert("Load");
var props = oWebsite.get_allProperties();
props.set_item("Myproperty", "PropertyValue");
oWebsite.update();
clientContext.load(oWebsite);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded2),
Function.createDelegate(this, this.onQueryFailed2)
);
}
function onQuerySucceeded2(sender, args) {
alert("Success"); }
function onQueryFailed2(sender, args) {
alert("Failed");
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
</script>
Hi there
</body>
</html>
You can check whether the property value is added or not by various methods. One of the way is to directly browse to the site like: https://intranet.contoso.com/\_api/web/AllProperties?$select=Myproperty.
Hope this was helpful!
Comments
Anonymous
January 01, 2003
ThanksAnonymous
May 28, 2014
Can we achieve same through .NET Managed Client Objcet model..? I have a requirement like that.Can u please help..Anonymous
June 01, 2017
Is it possible to hold the long string value(something like paragraph) in this property?