如何获取所有主题并在线设置SharePoint的特定主题

时间:2019-09-03 06:54:29

标签: sharepoint-online csom

我尝试使用以下方法在线获取SharePoint的所有主题 ClientObjectList主题= tenant.GetAllTenantThemes();但失败了。

我尝试过此代码,但未能获得

using System.Security;
using Microsoft.SharePoint.Client;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.Online.SharePoint.TenantManagement;

...

ClientContext ctx = new ClientContext("https://mysite-admin.sharepoint.com/");
var pwd = "mypassword";
var passWord = new SecureString();
foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);
ctx.Credentials = new SharePointOnlineCredentials("admin@mydomain.com", passWord);
Tenant tenant = new Tenant(ctx);
ClientObjectList<ThemeProperties> themes = tenant.GetAllTenantThemes();

错误是: 集合尚未初始化。尚未请求或尚未执行请求。可能需要明确要求。

1 个答案:

答案 0 :(得分:1)

使用下面的代码获取租户中的所有主题。注意:添加ctx.load和ctx.ExecuteQuery方法。

string siteUrl = "https://tenant-admin.sharepoint.com";
string userName = "lz@tenant.onmicrosoft.com";
string password = "xxx";
var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
using (ClientContext ctx = new ClientContext(siteUrl))
{
    ctx.Credentials = new SharePointOnlineCredentials(userName, securePassword);               
    Tenant tenant = new Tenant(ctx);
    ClientObjectList<ThemeProperties> themes = tenant.GetAllTenantThemes();
    ctx.Load(themes);
    ctx.ExecuteQuery();
    foreach(ThemeProperties pres in themes)
    {
        Console.WriteLine(pres.Name);
    }
}

我们还可以使用Pnp PowerShell来实现它。

Get-PnPTenantTheme

Add-PnPTenantTheme

为此设置网站主题。

tenant.SetWebTheme("Custom Black", "http://tenant.sharepoint.com/sites/TestModernTeamSite6");
ctx.ExecuteQueryRetry();