Reputation: 616
I have tried to get all themes of SharePoint online using ClientObjectList themes = tenant.GetAllTenantThemes(); but failed.
I have tried this code but failed to get
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("[email protected]", passWord);
Tenant tenant = new Tenant(ctx);
ClientObjectList<ThemeProperties> themes = tenant.GetAllTenantThemes();
Error is: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
Upvotes: 0
Views: 715
Reputation: 4208
Use the code below to get all themes in tenant. Note: add ctx.load and ctx.ExecuteQuery methods.
string siteUrl = "https://tenant-admin.sharepoint.com";
string userName = "[email protected]";
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);
}
}
We can also use Pnp PowerShell to achieve it.
Set Theme for a site using this.
tenant.SetWebTheme("Custom Black", "http://tenant.sharepoint.com/sites/TestModernTeamSite6");
ctx.ExecuteQueryRetry();
Upvotes: 2