1

I want to save some custom properties to web and access these properties for later use. Is it possible using JavaScript object model? Please help.

6

You can read/write to web property bag using JavaScript object model. Example of reading the custom property:

//wait until client object model dependencies are loaded before executing our code
ExecuteOrDelayUntilScriptLoaded(getWebProperties, "sp.js");

var webProperties;

function getWebProperties() {
    var clientContext = new SP.ClientContext.get_current();
    webProperties = clientContext.get_web().get_allProperties();
    clientContext.load(webProperties);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.getWebPropertiesSucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function getWebPropertiesSucceeded() {

          //debugger; //use this to force a break here
    //returns an object with all properties.  
          //Use the quick watch to expand this out to see all of them.
    var allProps = webProperties.get_fieldValues();

    var customProp = "";

         //make sure the property is there before using it.
    if(webProperties.get_fieldValues().CustomSite_Version != undefined)
    {
        var customProp = webProperties.get_fieldValues().CustomSite_Version;
    }
    alert(customProp);
}

function onQueryFailed(args, sender)
{
     //handle errors here
}

Source: Accessing the web property bag with JavaScript

Example of setting custom property:

<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>

Source: Add properties to SPWeb property bag using JSOM in SharePoint 2013

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.