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

时间:2018-09-04 17:24:22

标签: c# sharepoint csom

当前,我们正在使用现代团队网站,并尝试在SharePoint Online中的现代团队网站上添加添加对象

但是,我们发现收到访问拒绝错误

我们通过在Powershell中将站点属性denyandaddcustomizedpages设置为false进行了尝试,效果很好

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

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

1 个答案:

答案 0 :(得分:2)

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

请注意,执行此代码需要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
            }
        }
    }
}