0
public interface ISiteMetadataField
{
    string Name { get; set; }

    string Value { get; set; }               
}

public class SiteMetadataField : ISiteMetadataField
{
    private string _name;
    private string _Value;


    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    public string Value
    {
        get { return _Value; }
        set { _Value = value; }
    }

}

private Web UpdateTextField(Web web, ISiteMetadataField siteMetaDataItem)
{
 web.SetPropertyBagValue(siteMetaDataItem.Name, 
 Convert.ToString(siteMetaDataItem.Value)); 
 return web;
}

I am trying to update the property bag for the classic and modern site as per the article below:- https://asishpadhy.com/2018/04/23/how-to-set-property-bag-values-in-sharepoint-modern-sites-using-sharepoint-online-net-csom/

I am getting the below error when i am trying to update:

Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: 'Access denied. You do not have permission to perform this action or access this resource.'

I have already checked that I have the required permissions as I am able to update the values Chrome SP Dev Tools extension. But when I am doing the same via code, I am getting the error. Please guide as I am new to SharePoint.

4
  • did you run the powershell script first before executing the code ? Jul 19, 2018 at 6:16
  • No, which powershell script do we need to run? Jul 19, 2018 at 7:01
  • Its mentioned in the blog Set-SPOsite -Url "https://sitecollectionurl" -DenyAddAndCustomizePages 0 Jul 19, 2018 at 7:07
  • Actually I need to do it using the C# code without using powershell. I was referring to the resolution in the blog :) Jul 19, 2018 at 8:39

1 Answer 1

0

You need to reset the IsNoScript setting by setting the DenyAddAndCustomizePages to disabled first.

So, to do that using CSOM, you can use the below method:

var tenant = new Tenant(clientContext);
var siteProperties = tenant.GetSitePropertiesByUrl("https://<your-sitecollection-url>", true);
tenant.Context.Load(siteProperties);
tenant.Context.ExecuteQuery();

siteProperties.DenyAddAndCustomizePages = DenyAddAndCustomizePagesStatus.Disabled;
var operation = siteProperties.Update();
tenant.Context.Load(operation, i => i.IsComplete, i => i.PollingInterval);
tenant.Context.ExecuteQuery();

// this is necessary, because the setting is not immediately reflected after ExecuteQuery
while (!operation.IsComplete)
{
    Thread.Sleep(operation.PollingInterval);
    operation.RefreshLoad();
    if (!operation.IsComplete)
    {
        try
        {
            tenant.Context.ExecuteQuery();
        }
        catch (WebException webEx)
        {
            // catch the error, something went wrong
        }
    }
}

Once the above code is executed for your site collection, you should run your UpdateTextField method and it will update property bag.

Do note: - this code requires SharePoint tenant admin rights, it will not work with Site collection admin rights.

1
  • Hello, thank you for your answer! One question: how did you grant the SharePoint tenant admin rights? I am using an Azure app principal, and I granted the SharePoint Administrator right to it to no avail. Mar 20 at 5:43

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.