Add And Retrieve Property Bag Values In SharePoint Using JavaScript Object Model

Property bags in SharePoint act as a key value pair repository, where we can store the metadata values. The property bags more or less serve the same purpose of the app settings section in the Web Config file in ASP.NET, where we can store the configuration values. We can store the property bag values at different scopes in SharePoint like:

  • Farm scope
  • Web Application scope
  • Site collection scope
  • Site scope and
  • List scope

The property bag values can be added using the SharePoint designer as well as programmatically. In this article, we will see how we can update and read the property bag values using the JavaScript object model.

Add Property Bag Value

  • Add reference to jQuery file.
    1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
    2. <script language="javascript" type="text/javascript">  
  • Within the document, call ready function SP.SOD.executeFunc, so as to load the on demand script SP.js . Call the main starting point function, say: ModifyPropertyBag.
    1. SP.SOD.executeFunc('sp.js''SP.ClientContext', ModifyPropertyBag);  
  • Instantiate the client context and get the Web instance. Once the Web object is retrieved, get the property bag collection.
    1. //Get Client Context and Web object.  
    2. clientContext= new SP.ClientContext.get_current();  
    3. varoWeb= clientContext.get_web();  
    4.   
    5. //Get property bag collection and set the new key value pair  
    6. oPropBag= oWeb.get_allProperties();  
    7. oPropBag.set_item("Updated Name""Modified List");  
    8. oWeb.update();  
  • Execute the batch which will send the request to the Server and perform the entire JavaScript object model operation as a batch. Loading of the client context is not necessary to update property bag.
    1. clientContext.executeQueryAsync(Success,Failure);  

Full code

  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js">  
  2. </script>  
  3. <script language="javascript" type="text/javascript">  
  4. $(document)  
  5.     .ready(function()  
  6.     {  
  7.         SP.SOD.executeFunc('sp.js''SP.ClientContext', ModifyPropertyBag);  
  8.     });  
  9. varoList, oPropBag;  
  10.   
  11. function ModifyPropertyBag()  
  12. {  
  13.     //Get Client Context and Web object.  
  14.     clientContext = new SP.ClientContext.get_current();  
  15.     varoWeb = clientContext.get_web();  
  16.     //Get property bag collection and set the new key value pair  
  17.     oPropBag = oWeb.get_allProperties();  
  18.     oPropBag.set_item("Updated Name""Modified List");  
  19.     oWeb.update();  
  20.     //Execute the batch  
  21.     clientContext.executeQueryAsync(Success, Failure);  
  22. }  
  23.   
  24. function Success()  
  25. {  
  26.     console.log('Property bag value created');  
  27. }  
  28.   
  29. function Failure(sender, args)  
  30. {  
  31.     console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  32. }  
  33. </script>  
Read Property Bag Value

The property bags can be read using get_allPropertiesmethod. Once the property bag collection is retrieved, we can get the specific value, using the get_fieldValues method.
  1. oPropBag= oWeb.get_allProperties();  
  2. console.log("Property Bag Value - "+oPropBag.get_fieldValues()["Updated Name"]);  
We can test this in SharePoint by adding it to the Content Editor Web part, as shown below:  
  • Save the code given below to a text file and save it into one of the SharePoint Libraries, say: Site Assets.
    1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
    2. <script language="javascript" type="text/javascript">  
    3.   
    4.    $(document).ready(function() {  
    5.       SP.SOD.executeFunc('sp.js''SP.ClientContext', ReadPropertyBag);  
    6.    });  
    7.   
    8.    varoList, oPropBag;  
    9.   
    10.    function ReadPropertyBag() {  
    11.       //Get Client Context and Web object.  
    12.       clientContext= new SP.ClientContext.get_current();  
    13.       varoWeb= clientContext.get_web();  
    14.   
    15.       //Get property bag collection  
    16.       oPropBag= oWeb.get_allProperties();  
    17.   
    18.       //Load client context and execute the batch  
    19.       clientContext.load(oPropBag);  
    20.       clientContext.executeQueryAsync(Success,Failure);  
    21.    }  
    22.   
    23.    function Success(){  
    24.       console.log("Property Bag Value - "+oPropBag.get_fieldValues()["Updated Name"]);  
    25.    }  
    26.   
    27.    function Failure(sender, args) {  
    28.       console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
    29.    }  
    30. </script>  
  • Go to the edit settings of the SharePoint page and click Web part from the insert tab.



  • Add Content Editor Web part.


  • Click edit Web art from Content Edit Web part. Assign the URL of the script text file and click apply.

Output

Click apply and check the console. It will read the property bag and display the value.



We saved the modified list as the value against the key updated name, which has been retrieved and displayed. In this article, we saw how we can save and retrieve the property bag values, using JavaScript object model.