HOW TO: Add properties to SPWeb property bag using JSOM in SharePoint 2013
Published May 01 2019 03:44 PM 370 Views
Microsoft

First published on TECHNET on May 06, 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="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"


type="text/javascript">


</script>


<script


type="text/javascript"


src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js">


</script>


</head>


<body>


<script type="text/javascript">





$(document).ready(function () {








var hostweburl = "http://siteURL/";








var scriptbase = hostweburl + "/_layouts/15/";








$.getScript(scriptbase + "SP.Runtime.js",


function () {


$.getScript(scriptbase + "SP.js", execOperation);


}


);


});








function execOperation() {








AddWebSiteProperties("http://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: http://intranet.contoso.com/_api/web/AllProperties?$select=Myproperty.



Hope this was helpful!

Version history
Last update:
‎Sep 01 2020 03:17 PM
Updated by: