Get And Set Property Bag Data Using CSOM SharePoint 2013

Introduction

In this article we will get to know how to get and set values from the property bag in SharePoint 2013.

What is property bag

The Property Bag is a kind of hash table that can store any metadata as Key-Value pairs at site level. Examples of values that can be stored are connection strings, server names, file paths, and other settings needed by the SharePoint application. Most of the time we will store the above settings in configuration file, which is common to the entire web application. If there is any setting specific each and individual sites in the web application, then we can use the SharePoint Property Bag.

How to get the property bag

The property bag can be found by following the below steps:

  1. Open SharePoint Designer 2013 – open the particular site.

  2. In the ribbon you will see the “Site Options” clicking on which you can add new/modify key-value pairs

    Site Options

    We will now see how to set and get new values into the Property Bag. Follow the below steps for the same.

Steps

  1. Open Visual Studio & create a new SharePoint App Project.

    App in SharePoint

  2. Select the Host for the app as “SharePoint-Hosted”.

  3. Once the project is created, you will find the following structure.

    TestApp

  4. Add a save button to the default.aspx page as below:
    1. <asp:Buttonrunat="server"ID="btnSave"OnClientClick="btnSaveClick()"Text="Save"/>  
  5. Add the following code to App.js file,
    1. 'use strict';  
    2. var appWebContext;  
    3. var listResult;  
    4. var hostweburl;  
    5. var webProperties;  
    6. function btnSaveClick()  
    7. {  
    8.     appWebContext = newSP.ClientContext.get_current();  
    9.     hostweburl = decodeURIComponent($.getUrlVar("SPHostUrl"));  
    10.     varhostwebContext = newSP.AppContextSite(appWebContext, hostweburl);  
    11.     var web = hostwebContext.get_web();  
    12.     appWebContext.load(web);  
    13.     webProperties = web.get_allProperties();  
    14.     appWebContext.load(webProperties);  
    15.     webProperties.set_item("StringConnection"true);  
    16.     web.update();  
    17.     appWebContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);  
    18. }  
    19. functionon QuerySucceeded()  
    20. {  
    21.     varpropertyValue = webProperties.get_fieldValues().StringConnection;  
    22.     alert(propertyValue);  
    23. }  
    24. functionon QueryFailed(sender, args)  
    25.     {  
    26.         alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
    27.     }  
    28.     // jQuery plugin for fetching querystring parameters  
    29. jQuery.extend(  
    30. {  
    31.     getUrlVars: function ()  
    32.     {  
    33.         varvars = [], hash;  
    34.         var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');  
    35.         for (vari = 0; i < hashes.length; i++)  
    36.         {  
    37.             hash = hashes[i].split('=');  
    38.             vars.push(hash[0]);  
    39.             vars[hash[0]] = hash[1];  
    40.         }  
    41.         returnvars;  
    42.     },  
    43.     getUrlVar: function (name)  
    44.     {  
    45.         returnjQuery.getUrlVars()[name];  
    46.     }  
    47. });  
  6. Go to AppManifest.xml a Permissions tab. We need to give permission to the app to access the host web. Select “SiteCollection” Permission Level ”Manage”.

  7. Deploy the solution, Trust the app.

  8. Once the page opens, click on the Save Button, an Alert will be coming up with the value as “TRUE”.

  9. Check in the SharePoint designer if a key called “String Connection” has been set as “TRUE”.

    String Connection