Add property to web with JavaScript in SharePoint 2013 App
Posted onWord count in article: 3.2kReading time ≈5 mins.
Introduction
SharePoint App Model provides a new approach to SharePoint development. And here is the question: where to save app data? There are several ways to save data in an app, you can create a list or connect to a database in Windows Azure or set custom properties in AppManifest.xml. But we can also save data to the property bag of a SharePoint web.
Add property to web
Adding custom property to web is easy. Let’s just see the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var context = SP.ClientContext.get_current(); var web = context.get_web();
// Add a new property var props = web.get_allProperties(); props.set_item("MyProperty", "My property value");
// Apply change to web web.update(); context.load(web);
How do we know “MyProperty” is really added to web? We can check it here: http://your_web/_api/web/AllProperties?$select=MyProperty. The result looks like this: