Expand Minimize

SMA290122: Consider to Switch to CSOM for Property bag Manipulation

The PropertyBag is full implemented in CSOM and JSOM, so migration of this component has no blockers.

CheckId SMA290122
TypeName PropertyBagRecomendations
Severity CriticalWarning
Type Assembly

Full Trust Approach App Approach
Managed Code to access cached values against a site

The property bag is attached to each Web Object in the farm (A Web object is the collection of data that describes a Site in a SharePoint Farm). It has default values used by that specific Web, and also the Property Bag is expandable with custom values through code based solutions. The Property Bag is a Hashtable which is a large list of data that can be of any sort, that is referenced by a Key. This works as an index to be able to quickly access data values. The Property Bag can therefore be used to store any data that is valuable to the Site.

Accessing the Property Bag Remotely

The Property Bag is accessed through the {Web object}.AllProperties when being used remotely. Apart from the addition of requesting this property, it works identically to its Full Trust counterpart.

Full Trust Code Sample Client Side Object Model Code Sample
Code Sample
// Allow unsafe updates is required to show that this was intended and not a security breach
web.AllowUnsafeUpdates = true;

// Get Property bag
string spcaf;
if (web.AllProperties.ContainsKey("SPCAF"))
{
  spcaf = web.AllProperties["SPCAF"].ToString();
}
else
{
  spcaf = "SharePoint Code Analysis Framework";

  // Update Property Bag value SPCAF
  web.Properties["SPCAF"] = spcaf;
  web.Properties.Update();
}
// End of the unsafe update
web.AllowUnsafeUpdates = false;
Code Sample
Web web = clientContext.Site.RootWeb;
// Get remote web object with the Property Bag
clientContext.Load(web, w => w.AllProperties);
clientContext.ExecuteQuery();

// Get Property bag
string spcaf;
if (web.AllProperties.FieldValues.ContainsKey("SPCAF"))
{
  spcaf = web.AllProperties["SPCAF"].ToString();
}
else
{
  spcaf = "SharePoint Code Analysis Framework";

  // Update Property Bag value SPCAF
  web.Properties["SPCAF"] = spcaf;

  // In CSOM we update the web object,
  // not the property bag like we did in SSOM
  web.Update();
  clientContext.ExecuteQuery();
}

Efforts and Benefits

Migration Impact Medium
Re-Design Effort Low
Re-Development Effort Low ~ Medium
Long Term Benefit Medium

Effort Drivers

  • Fully migratable to App model

To suppress this violation in managed code add the following attribute to the method which contains the instruction (available since SPCAF version v5.2). Learn more about SuppressMessage here.

// Important: Ensure to have #define CODE_ANALYSIS at the beginning of your .cs file
[SuppressMessage("SPCAF.Rules.MigrationAssessment.AdvancedCustomizationsGroup", "SMA290122:PropertyBagRecomendations", Justification = "Provide reason for suppression here")]
Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of RENCORE AB. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.