Add and Retrieve property bag by CSOM

This blog will tell you how to add and retrieve web property bag through SharePoint CSOM.

I tried use Add method to insert a new property; however, it does not really add the new property.

Add Property

private int SetProperty(ClientContext clientContext, int flag)
{
  Web web = clientContext.Site.RootWeb;
  /* Add successfully, but not persistantly. Cannot find this new property when retrieve property bag
  clientContext.Load(web, w=>web.AppProperties);
  clientContext.ExecuteQuery();

  if (!web.AllProperties.FieldValues.ContainsKey("Customized"))
  {
     web.AllProperties.FieldValues.Add("Customized", flag);
  }
  else
  {
     web.AllProperties["Customized"] = flag;
  }
  */ 
  
  // Correct Approach
  var allProperties = web.AllProperties;
  allProperties["CIBCCustomized"] = flag;

  web.Update();
  clientContext.ExecuteQuery();

  return flag;
}

Retrieve Property

private int GetFlag(ClientContext clientContext)
{
  Web web = clientContext.Site.RootWeb;
  clientContext.Load(web, w => w.AllProperties);
  clientContext.ExecuteQuery();

  if (!web.AllProperties.FieldValues.ContainsKey("Customized"))
  {
    return 0;
  }
  else
  {
    return (int)web.AllProperties["Customized"];
  }
}
Advertisements

11 thoughts on “Add and Retrieve property bag by CSOM

  1. Hmm it aplearѕ like your website ate my first commеnt (it was extremely long)
    so I guess I’ll just sum it up wat I submitted and say, I’m tɦoroughly enјoying your Ƅlog.
    I tooo am ann asρiring blog wrіter buut I’m still neww to everything.
    Do you have anny pօints for beginner blog writers? I’d Ԁefіnitely appreciate
    it.

  2. Definitely believe that which you said. Your favorite justification appeared to be at the
    net the simplest factor to take into accout of. I say to you, I definitely get irked even as
    folks consider worries that they plainly do not understand about.

    You managed to hit the nail upon the highest and also outlined out the entire thing with no need side-effects , other people could take a signal.
    Will probably be again to get more. Thanks

  3. Hi, I believe your web site could possibly be having web browser compatibility problems.
    When I look at your web site in Safari, it looks fine but
    when opening in IE, it has some overlapping issues.
    I just wanted to give you a quick heads up! Apart from that, excellent site!

  4. I have saved value in list property bag in provider hosted using CSOM code:
    list.Context.Load(list.RootFolder);
    list.Context.ExecuteQuery();
    var properties = list.RootFolder.Properties.FieldValues;
    properties[“Key”] = Value;
    list.RootFolder.Update();
    list.Context.ExecuteQuery();

    The settings are saved successfully but at the time of retrieving the values from list property bag it shows the key not exist.
    Code to get key from property bag:

    list.Context.Load(list.RootFolder);
    list.Context.ExecuteQuery();
    string settings = list.RootFolder.Properties.FieldValues.ContainsKey(“key”)
    ? Convert.ToString(list.RootFolder.Properties[“key”])
    : String.Empty;

    Kindly suggest where am i doing wrong.

    • Just do this:
      var properties = list.RootFolder.Properties;
      properties[“Key”] = “123456”;
      //properties.Add(“Key”, “123456”);
      list.RootFolder.Update();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s