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

nin*_*ina 1 c# sharepoint csom

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

但是我们观察到我们收到拒绝访问错误

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

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

很少有文章提到尝试使用 pnp nugget 但找不到相同的代码

Gau*_*eth 6

您可以使用以下示例代码来做到这一点。

请注意,执行此代码需要 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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)