If you think setting Property Bag values programmatically for Modern SharePoint sites using CSOM would be as straight forward as in the Old Classic SharePoint sites, then there is a surprise waiting for you.
As you can see below, in the old C# CSOM this code would set the Property Bag values as follows:

The challenge with the above method is that the PropertyBag values are not persisted after saving. So, if you load the context object again, the values are back to the initial values (i.e. without the “PropertyBagValue1” = “Property”).
The cause of the issue is that Modern Sites have the following setting set; IsNoScript = false, which prevents us from updating the Object model through script. Below is a screenshot of the documentation from MS docs – https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/modern-experience-customizations-customize-sites
ModernTeamSitesPropertyBag_Limitation
Resolution:
Using PowerShell
Resolving this is quite easy using PowerShell, by setting the -DenyAddAndCustomizePages to 0.

Set-SPOsite <SiteURL> -DenyAddAndCustomizePages 0

However, when working with the CSOM model we need to take another approach (see below).
Using .NET CSOM Model
When using the CSOM Model, we must use the SharePoint PnP Online CSOM for this.  Start by downloading it from Nuget or the Package Manager in Visual Studio.  Next, add the code below.

Walking through the code, we are first initializing Tenant Administration and then accessing the Site Collection using the SiteUrl property. Then we use the SetSiteProperties method to set noScriptSite to false.  After that, we can use the final block of code to set the property bag values.
When done, you may want to set the noScriptSite back to true, as there are implications on modern sites as specified in this article here – https://support.office.com/en-us/article/security-considerations-of-allowing-custom-script-b0420ab0-aff2-4bbc-bf5e-03de9719627c
 

Category:
Application Development and Integration, PowerShell, SharePoint
Tags:
, , , ,

Join the conversation! 0 Comments

  1. […] How to set Property Bag values in SharePoint Modern Sites using SharePoint Online .Net CSOM […]

    Reply
  2. […] How to set Property Bag values in SharePoint Modern Sites using SharePoint Online .Net CSOM […]

    Reply
  3. […] DenyAddAndCustomizePages to 1 after you’re done setting your property bag values. Thanks to Asish Padhy for the inspiration to set […]

    Reply

Leave a Reply

Follow ...+

Kloud Blog - Follow

%d bloggers like this: