Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to add a new item to the Web property bag using the SharePoint managed client object model, but it fails to persist. I'm using the following code:

 using (var context = new ClientContext("http://projectdev:82/PWA"))
 {
     var web = context.Web;
     context.Load(web);

     var allProperties = context.Web.AllProperties;
     context.Load(allProperties);

     context.ExecuteQuery();

     if (!allProperties.FieldValues.ContainsKey(CONNECTION_STRING_KEY))
     {
         allProperties.FieldValues.Add(CONNECTION_STRING_KEY, "Test");
     }

     context.Web.Update();
     context.ExecuteQuery();
 }
share|improve this question

migrated from stackoverflow.com Oct 17 '12 at 19:22

This question came from our site for professional and enthusiast programmers.

    
I have the equivalent working code in JavaScript, I use get_fieldValues()[someProp] to read properties, but to add/set properties I must call set_item(someProp, someValue). Although I can not see set_item in the C# version –  eirikb Oct 18 '12 at 5:56
add comment

2 Answers 2

up vote 8 down vote accepted

Here you go:

using (var context = new ClientContext("http://localhost"))
{
  var allProperties = context.Web.AllProperties;
  allProperties["testing"] = "Hello there";
  context.Web.Update();
  context.ExecuteQuery();
}
share|improve this answer
add comment
SPSecurity.RunWithElevatedPrivileges(delegate()
{
 using (SPSite currentSiteCollection = new SPSite("http://myserver/mysite"))
 {
    using (SPWeb currentWeb = currentSiteCollection.OpenWeb())
    {
        // unsafe updates are required to be able to write to the property bag
         currentWeb.AllowUnsafeUpdates = true;
         if (!currentWeb.AllProperties.ContainsKey(key))
            currentWeb.Properties.Add(key, value.Value.ToString());
        else
            currentWeb.AllProperties[key] = value;

        // update the properties
        currentWeb.Properties.Update();
        currentWeb.AllowUnsafeUpdates = false;
    }
  }
});

you can get more information from this blog: http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/05/12/how-to-write-a-value-into-the-property-bag.aspx

hope this helps!

share|improve this answer
1  
I think iulianchira is asking specifically for Client Object Model. And the statement that "AllowUnsafeUpdates are required" is plainly wrong. And I don't think one needs to call ...Properties.Update(). This worked fine for me: web.AllProperties.Add("testing", "Hello");web.Update(); –  eirikb Oct 18 '12 at 5:38
    
This is the classic API, the server model, since my code runs from outside the SharePoint farm, I cannot use it. –  iulianchira Oct 22 '12 at 16:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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