spweb class allproperties

The SharePoint SPWeb class contains two different API properties that developers need to be aware of – AllProperties and Properties. This article provides an in-depth understanding of these properties and their usage in your custom SharePoint applications.

AllProperties vs. Properties

AllProperties is a Hashtable, while Properties is a PropertyBag. Although AllProperties was introduced to replace Properties, the latter was retained for backward compatibility.

Key Storage Differences

The unconventional PropertyBag data type in Properties stores its keys in all lowercase, and as a result, it doesn’t support case-sensitive keys. Conversely, the conventional Hashtable in AllProperties does support case-sensitive keys.

Propagation Behavior

Entries added to Properties are propagated to AllProperties with a lowercase key. However, entries added to AllProperties aren’t propagated to Properties.

Best Practices for Managing SharePoint Web Properties

To ensure optimal performance in your custom SharePoint applications, consider the following guidelines:

Use AllProperties for Custom Applications

If you’re working with property entries that only your custom application will read and write, always use AllProperties. This will make managing your data more straightforward.

Add Entries to Both API Properties

When dealing with properties that are read or written by SharePoint internals, add your entries to both API properties. This ensures that the entry is present in both collections and maintains the correct key casing in AllProperties.

Keep in mind the order of update API calls:

  1. Update the SPWeb object.
  2. Update the SPWeb.Properties PropertyBag.

Performing the updates in reverse order results in an entry with a case-sensitive key not being added to AllProperties, exposing only the lowercase-keyed entry.

Code Sample: Adding and Removing Property Entries

Here’s an example of how to add and remove property entries while working with the SPWeb class:

“`csharp
// Add a property entry
web.Properties[key] = value;
web.AllProperties[key] = value;
web.Update();
web.Properties.Update();

// Remove a property entry
web.AllProperties.Remove(key);
web.Properties[key] = null;
web.Update();
web.Properties.Update();
“`

For a more abstract approach to managing Web property interactions, consider utilizing the SharePoint utility class.

By implementing these best practices, you’ll enhance the usability and maintainability of your custom SharePoint applications.