denyandaddcustomizedpages - 使用 csom 为现代团队网站修改 属性

denyandaddcustomizedpages - modify the property using csom for modern team site

目前我们正在使用新式团队网站并尝试在 SharePoint online 中的新式团队网站上添加对象

但是我们观察到出现访问被拒绝错误

我们尝试通过 powershell 将站点 属性 denyandaddcustomizedpages 设置为 false,它工作正常

但是我们无法获得可以帮助我们使用 csom 客户端对象模型 SharePoint online c# 实现相同目的的代码

很少有文章提到尝试使用 pnp 块,但没有找到相同的代码

您可以使用以下示例代码执行此操作。

请注意,执行此代码需要 SharePoint 管理员权限,请根据您的要求进行必要的修改:

var tenantAdminSiteUrl = "https://tenant-admin.sharepoint.com";
var siteCollectionUrl = "https://tenant.sharepoint.com/sites/Test";

var userName = "admin@tenant.onmicrosoft.com";
var password = "password";

using (ClientContext clientContext = new ClientContext(tenantAdminSiteUrl))
{
    SecureString securePassword = new SecureString();
    foreach (char c in password.ToCharArray())
    {
        securePassword.AppendChar(c);
    }

    clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
    clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);

    var tenant = new Tenant(clientContext);
    var siteProperties = tenant.GetSitePropertiesByUrl(siteCollectionUrl, true);
    tenant.Context.Load(siteProperties);
    tenant.Context.ExecuteQuery();

    siteProperties.DenyAddAndCustomizePages = DenyAddAndCustomizePagesStatus.Disabled;
    var operation = siteProperties.Update();
    tenant.Context.Load(operation, op => op.IsComplete, op => op.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
            }
        }
    }
}