4

I'm writing a powershell script that creates a number of site collections. I want to programmatically set the composed look to "Sea Monster".

Through the web UI I go to Site Settings > "Change the look" and click on "Sea Monster".

Is there a way to do this with powershell? Are these "considered composed" looks or "themes"?

5

In SharePoint 2013, a Composed Look is the combination of following::

  • Master Pages
  • CSS
  • Color palettes
  • Font palettes
  • Background image

enter image description here

Hence can easily be set via PowerShell

$themeName = "Sea Monster"
$SPSite = Get-SPSite "http://sp2013/"    
foreach ($SPWeb in $SPSite.AllWebs) 
{ 
      $SPWeb.allowunsafeupdates = $true
      $fontSchemeUrl = $SPWeb.ServerRelativeUrl + "/_catalogs/theme/15/fontscheme003.spfont"
      $themeurl = $SPWeb.ServerRelativeUrl + "/_catalogs/theme/15/palette005.spcolor"
      $imageUrl = $SPWeb.ServerRelativeUrl + "/_layouts/15/images/image_bg005.jpg"
      $SPWeb.ApplyTheme($themeUrl, $fontSchemeUrl, $imageUrl, $true);
      Write-Host "Set" $themeName "at :" $SPWeb.Title "(" $SPWeb.Url ")" 
      $SPWeb.Update()
      $SPWeb.allowunsafeupdates = $false
      $SPWeb.Dispose()
}  
$SPSite.Dispose()

Happy ShareComposing :)

| improve this answer | |
  • You made a mistake. Try by replace all the $SPSite by $Site ;) – user23668 Feb 13 '14 at 22:01
  • 3
    Doesn't matter it's a variable name ;) – Falak Mahmood Feb 14 '14 at 0:06
  • Hi Falak, Can you tell me how to pass the iamge and font url as null.. ? – variable Mar 19 '14 at 12:01
  • Have you tried passing "" or $nullas a null value? – Falak Mahmood Mar 19 '14 at 15:20
  • FalakMahmood I have tried with both, still getting errors. Thanks for the reply. @variable, did you find a workaround ? – Tito Dec 10 '15 at 10:42
3

Note everyone - SPWeb.ApplyTheme is not obsolete - only the method with the signature ApplyTheme(string) - that was for applying a theme by passing only the theme name. The method SPWeb.ApplyTheme(String, String, String, Boolean) is perfectly fine (see https://msdn.microsoft.com/EN-US/library/jj251358(v=office.15).aspx). That allows you to specify the URLs for the color palette file, the font file, the background image (can be null if no image needed), and a boolean to specify whether (assuming this is a sub-web) the generated theme files should go into the root web.

Hope this helps clean up google results on the subject! - Had me quite worried for a bit.

| improve this answer | |
3

http://msdn.microsoft.com/en-us/library/office/ms431037.aspx

Check the documentation your version of $SPWeb.ApplyTheme no longer works for SharePoint 2013

This is what I've got to work so far:

$web=Get-SPWeb http://intranet.contoso.com
$file=$web.GetFile($web.ServerRelativeUrl + "/_catalogs/theme/15/palette005.spcolor")
$theme=[Microsoft.SharePoint.Utilities.SPTheme]::Open("AnyNameYouWanttoCallIt", $file)
$theme.ApplyTo($web, $false)
$web.Dispose()

If anyone knows how to determine the current theme--I'd like to know?

| improve this answer | |
  • Good answer there. I figured out a way to get the existing Theme. Just do a $web.ThemedCssFolderUrl. That will point you to a Theme folder (Normally /_theme/1) which is the rendered out Theme. I.E, it will have your master page, SPCOLOR etc in it. – Fox Aug 28 '14 at 22:00
0

This technet article has a great script for managing themes across site collections.

This is complete PowerShell snippet which you can use to deploy and apply theme to your environment. I have not tested it for ‘My Sites’ but it should work practically.

Script:

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue;
[xml]$inputfile = Get-Content C:\Data\theme\ThemeScriptInput.xml

$ColorFilePartUrl = "/_catalogs/theme/15/";
$FontFilePartUrl = "/_catalogs/theme/15/";
$MasterPagePartUrl = "/_catalogs/masterpage/";

foreach( $site in $inputfile.SiteCollections.site)
{
    $siteurl = $site.url;
    $themeName = $site.theme;
    $colorfile = $site.colorfile;
    $fontfile = $site.fontfile;
    $masterpage = $site.masterpage;
    $colorshemeFilePath = $site.colorFilePath;
    $colorfontFilePath = $site.fontFilePath;
    $IsfilesToUpload = $site.filesToUpload;

    Write-Host "$siteurl + " :: " + $themeName + " :: " + $colorfile + " :: " + $fontfile + " :: " + $masterpage";

    $site = Get-SPSite $siteurl;
    $rootweb = $site.RootWeb;

    if($IsfilesToUpload -eq "true")
    {
        #region Upload theme files to the root web gallery      

        $colorSchemeBytes = [System.IO.File]::ReadAllBytes($colorshemeFilePath);
        $fontSchemeBytes = [System.IO.File]::ReadAllBytes($colorfontFilePath);

        $rootweb.allowunsafeupdates = $true;
        $themeList = $rootweb.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::ThemeCatalog);
        $folder = $themeList.RootFolder.SubFolders["15"];
        $folder.Files.Add($colorfile,$colorSchemeBytes,$true);
        $folder.Files.Add($fontfile,$fontSchemeBytes, $true);
        $rootweb.allowunsafeupdates = $false;
        #endregion
    }

    $colorfile=$site.AllWebs[0].GetFile("$siteurl$ColorFilePartUrl$colorfile");
    $fontfile=$site.AllWebs[0].GetFile("$siteurl$FontFilePartUrl$fontfile");

    foreach ($Web in $site.AllWebs)
    {
       Write-Host "processing: " $Web.Title;
       $Web.allowunsafeupdates = $true;
       $relativeUrl = $Web.ServerRelativeUrl;
       $spList = $Web.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::DesignCatalog);

       #region Add New theme to the Composed Looks gallery

       $SPQuery1 =  New-Object Microsoft.SharePoint.SPQuery;
       $SPQuery1.Query = "<Where><Eq><FieldRef Name='Name'/><Value Type='Text'>$themeName</Value></Eq></Where>";
       $SPQuery1.RowLimit = 1;
       $SPQuery1.ViewFields = "<FieldRef Name='Name'/>";
       $SPQuery1.ViewFieldsOnly = $true;

       $spListItems1 = $spList.GetItems($SPQuery1);


       if($spListItems1.Count -eq 0)
       {        

       $newThemeItem = $spList.AddItem();

       $newThemeItem["Name"] = $themeName;
       $newThemeItem["Title"] = $themeName;
       $newThemeItem["MasterPageUrl"] =  "$relativeUrl$MasterPagePartUrl$masterpage";#$Web.MasterUrl;
       $newThemeItem["ThemeUrl"] = "$ColorFilePartUrl$colorfile";
       $newThemeItem["FontSchemeUrl"] = "$FontFilePartUrl$fontfile";
       $newThemeItem["DisplayOrder"] = 121;
       $newThemeItem.Update();

       }
       #endregion

       #region Set the theme


        $theme=[Microsoft.SharePoint.Utilities.SPTheme]::Open($themeName, $colorfile);
        Write-Host $theme.Name "to" $Web.Title;
        $theme.ApplyTo($Web, $false);



       #endregion

       #region Set applied theme as current theme             

       $SPQuery =  New-Object Microsoft.SharePoint.SPQuery;
       $SPQuery.Query = "<Where><Eq><FieldRef Name='DisplayOrder'/><Value Type='Number'>0</Value></Eq></Where>";
       $SPQuery.RowLimit = 1;
       $SPQuery.ViewFields = "<FieldRef Name='DisplayOrder'/>";
       $SPQuery.ViewFieldsOnly = $true;

       $spListItems = $spList.GetItems($SPQuery);


       if($spListItems.Count -eq 1)
       {
            $spListItems[0].Delete();
       }

       $currentThemeItem = $spList.AddItem();

       $currentThemeItem["Name"] = [Microsoft.SharePoint.SPResource]::GetString([System.Threading.Thread]::CurrentThread.CurrentUICulture, [Microsoft.SharePoint.Strings]::DesignGalleryCurrentItemName);
       $currentThemeItem["Title"] = [Microsoft.SharePoint.SPResource]::GetString([System.Threading.Thread]::CurrentThread.CurrentUICulture,[Microsoft.SharePoint.Strings]::DesignGalleryCurrentItemName);
       $currentThemeItem["MasterPageUrl"] = "$relativeUrl$MasterPagePartUrl$masterpage";#$Web.MasterUrl;
       $currentThemeItem["ThemeUrl"] = "$ColorFilePartUrl$colorfile";
       $currentThemeItem["FontSchemeUrl"] = "$FontFilePartUrl$fontfile";
       $currentThemeItem["DisplayOrder"] = 0;
       $currentThemeItem.Update();

       #endregion

       $Web.allowunsafeupdates = $false;
       Write-Host "Set" $theme.Name "theme to :" $Web.Title "(" $Web.Url ")" ;
    }

}

Config File

<SiteCollections>
  <site>
    <url>http://local/sites/MyBlog/url>
    <theme>Collaboration</theme>
   <filesToUpload>false</filesToUpload>
    <colorFilePath>C:\theme\collaborationcolor.spcolor</colorFilePath>
    <fontFilePath>C:\theme\collaborationfontscheme.spfont</fontFilePath>
    <colorfile>collaborationcolor.spcolor</colorfile>
   <fontfile>collaborationfontscheme.spfont</fontfile>
    <masterpage>seattle.master</masterpage>
  </site>
  <site>
    <url>http://local</url>
    <theme>Collaboration</theme>
    <filesToUpload>true</filesToUpload>
    <colorFilePath>C:\theme\collaborationcolor.spcolor</colorFilePath>
    <fontFilePath>C:\theme\collaborationfontscheme.spfont</fontFilePath>
    <colorfile>collaborationcolor.spcolor</colorfile>
    <fontfile>collaborationfontscheme.spfont</fontfile>
    <masterpage>seattle.master</masterpage>
  </site>
</SiteCollections>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.