3

I have a requirement where in I need to develop a web page using SharePoint Addin in Visual Studio such that user enters certain configuration data . This data that has to be stored on the SharePoint Online Web Property Bag i.e AllProperties.FieldValues. The code block below shows what I have implemented on a button click event of the page.

 var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
var clientContext = spContext.CreateUserClientContextForSPHost();
var ConfigDataString=TextBox1.Text;
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();                
web.AllProperties.FieldValues.Add("Config", ConfigDataString);                
web.Update();
clientContext.ExecuteQuery();

But when I tried to fetch the same key value again by implementing the following code on another button click, I could not find the key on the property field values.

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
var clientContext = spContext.CreateUserClientContextForSPHost();
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();                
var configData = web.AllProperties.FieldValues["Config"].ToString();//Exception here

Then, I tried to update other properties of the Web such as Web Title or Description and I could successfully post the changes to the SharePoint Online.I have same code block for SharePoint On premise server and I could successfully store data on the RootWeb or Site Properties. Did I miss anything when it comes to SharePoint Online? I have a speculation that the problem is with the client context. Please let me know where I can see the properties of the Web or SiteCollection in the SharePoint Online interface.

2 Answers 2

1

To add values to property bag, try the below code:

Web web = context.Site.RootWeb; //change as per your web object

context.Load(web, w=>web.AllProperties);
context.ExecuteQuery();  

var allProperties = web.AllProperties;
allProperties["Config"] = "some value";

web.Update();
context.ExecuteQuery();

To retrieve the value, you need to use below code:

Web web = context.Site.RootWeb; //change as per your web object
context.Load(web, w => w.AllProperties);
context.ExecuteQuery();

var propValue = web.AllProperties["Config"];
2

Try adding properties by doing the following:

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
var clientContext = spContext.CreateUserClientContextForSPHost();
var ConfigDataString=TextBox1.Text;
var web = clientContext.Web;
web.AllProperties["Config"] = ConfigDataString;
web.Update();
clientContext.ExecuteQuery();

This was how I was setting properties in one of my apps. Not sure why it works but not .Add()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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