Setting property bags using Client Object Model - persistence problem
Hello,


I have a Windows Forms application that adds a new property in the property bag of the site collection, using the Client Object Model. 


When I start the application the property is added, I can get or set its value, everything works fine. 


The problem is that if I close the application and then start it again, the property added is no longer part of the spWeb.AllProperties property bag.


Am I doing something wrong? If so, which is the correct way of adding a new property in the property bag? 


Here is my code:
 
Site spSite = clientContext.Site;
clientContext.Load(spSite);
clientContext.ExecuteQuery();


Web spWeb = spSite.RootWeb;
clientContext.Load(spWeb);
clientContext.ExecuteQuery();


if (spWeb.AllProperties.FieldValues.ContainsKey("MB_Sol")
spWeb.AllProperties.FieldValues["MB_Sol"] = "Licensed";
else
spWeb.AllProperties.FieldValues.Add("MB_Sol", "Licensed");


spWeb.Update();
clientContext.Load(spWeb);
clientContext.Load(spWeb.AllProperties);
clientContext.ExecuteQuery();




I also tried doing it like this:
spWeb.AllProperties["MB_Sol"]="Licensed";
but it's still not persisted.


Normally, in the Server Object Model I used spWeb.SetProperty, but this is not available in the client object model.


Answer needed asap!


Thank you,
Alice

February 22nd, 2012 7:07pm

Hi,

Please refer to the following post: http://e-junkie-chronicles.blogspot.com/2011/10/making-use-of-property-bag-in.html

The set_item method of the Property Bag is used there.

HTH!

Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2012 7:51pm

Thank you for your reply but this is not helpful. I am using the Sharepoint Managed Client Object Model, not the EcmaScript Client Object Model, as you can see in the code. I mentioned this is a Windows Forms Application.

I need a way of solving the persistence issue by using the Sharepoint Managed Client Object Model. 


February 23rd, 2012 8:15am

Hi,

Sorry, I've missed that. I thought you meant the Ecmascript client objectmodel.

Free Windows Admin Tool Kit Click here and download it now
February 24th, 2012 1:23pm

This code works.

 
            var clientContext = new ClientContext(txtSiteURL.Text);
            NetworkCredential credentials = new NetworkCredential("123", "123", "123");
            clientContext.Credentials = credentials;

             Site spSite = clientContext.Site;
            clientContext.Load(spSite);
            Web spWeb = spSite.RootWeb;
            clientContext.Load(spWeb, w => w.AllProperties);
            clientContext.ExecuteQuery();
            var allProperties = spWeb.AllProperties;
            clientContext.Load(allProperties);

            if (!spWeb.AllProperties.FieldValues.ContainsKey("SiteType"))
            {
                spWeb.AllProperties["SiteType"] = "BuildWork";
                spWeb.Update();
            }
            clientContext.Load(spWeb, w => w.AllProperties);
            clientContext.ExecuteQuery();

January 7th, 2013 9:37am

Same logic but created two extension methods with same functionality with less lines of code

        public static string SetWebPropertyValue(this ClientContext clientContext, string property, string value)
        {
            //Get and load site
            Site site = clientContext.Site;
            clientContext.Load(site);

            //Get and load web
            Web web = site.RootWeb;
            clientContext.Load(web, w => w.AllProperties);

            //Execute query
            clientContext.ExecuteQuery();

            //Reading property
            web.AllProperties[property] = value;

            //Update and write back to web
            web.Update();
            clientContext.ExecuteQuery();

            return web.AllProperties[property].ToString();
        }

        public static string GetWebPropertyValue(this ClientContext clientContext, string property)
        {
            //Get and load site
            Site site = clientContext.Site;
            clientContext.Load(site);

            //Get and load web
            Web web = site.RootWeb;
            clientContext.Load(web, w => w.AllProperties);
            
            //Execute query
            clientContext.ExecuteQuery();

            //If property found then return 
            return web.AllProperties.FieldValues.ContainsKey(property) ? web.AllProperties[property].ToString(): string.Empty;
        }
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 11:48am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics