1

So I have powershell code to upload a palette file to my themes gallery but I cannot seem to figure out how to create the composed look in the composed look gallery using the link to my uploaded theme in sharepoint 2016/2013 in powershell, any suggestions?

Here is my current code:

If(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)){
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell"
   Add-PSSnapin Microsoft.SharePoint.PowerShell
   }

cls

$web = get-spweb http://test.site.com

<# Create composed look start #>
foreach($site in $web.Site.Allwebs)
{

    <# Upload Palette to Themes Gallery in rootsite Start #>
    try
    {
    $rootWeb = get-spweb $site.Site.RootWeb.Url

    $DocLibName = “_catalogs/theme” #Themes Library Name
    $FilePath = “C:\temp\colorPalette.spcolor” #Local path to palette file

    $List = $rootWeb.GetFolder($DocLibName) 
    $Files = $List.Files

    $FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)

    # Load the file into a variable 
    $File= Get-ChildItem $FilePath

    # Upload it to SharePoint 
    $Files.Add($DocLibName +"/" + $FileName,$File.OpenRead(),$false)
    }
    catch
    {
    #
    }
    <# Upload Palette to Themes Gallery in rootsite End #>

    <# Verify Site not root site Start #>
    if($site.Url -ne $site.Site.RootWeb.Url)
    {
    $paletteUrl = $site.Site.RootWeb.Url + "/_catalogs/theme"
    $paletteUrl

    $compositeLookGallery = $site.GetCatalog(124)
    #$site.Load($compositeLookGallery)
    $spContext = New-Object Microsoft.SharePoint.Client.ClientContext($site.Url);
    }
    <# Verify Site not root site End #>
}
<# Create composed look End #>

1 Answer 1

1

this should help

 #region ######   Upload theme files   ###### 
try{
    $ColorFilePartUrl = "/_catalogs/theme/15/"
    $FontFilePartUrl = "/_catalogs/theme/15/"
    $MasterPagePartUrl = "/_catalogs/masterpage/"

    Write-Host "$SiteUrl + " :: " + $ThemeName + " :: " + $SpColorFileName + " :: " + $SpFontFileName + " :: " + $Masterpage"


    $TargetWeb = Get-SPWeb -Identity $UrlNewSite  
    Write-Host "processing: " $TargetWeb.Title
    $TargetWeb.allowunsafeupdates = $true
    $RelativeUrl = $TargetWeb.ServerRelativeUrl
    $SpList = $TargetWeb.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::DesignCatalog)

    $ThemeList = $TargetWeb.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::ThemeCatalog)
    $Folder = $ThemeList.RootFolder.SubFolders["15"]

    $ColorSchemeBytes = [System.IO.File]::ReadAllBytes($PathToColorSheme)
    $Folder.Files.Add($SpColorFileName,$ColorSchemeBytes,$true)

    try{
        $FontSchemeBytes = [System.IO.File]::ReadAllBytes($PathToColorSheme)
        $Folder.Files.Add($SpFontFileName,$fontSchemeBytes,$true)
    }
    catch{
        Write-Host "No valid spfont file found" -ForegroundColor Yellow
    }
    Write-Host "Upload of theme files...completed" -ForegroundColor Green
}
catch
{
    Write-Host "Caught an exception while uploading theme files:" -ForegroundColor Red
    Write-Host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
    Write-Host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
}$ErrorActionPreference = "Stop"
#endregion

#region ######   Create and apply theme to site   ######
try
{
    $ThemeUrlColor = $UrlNewSite+$ColorFilePartUrl+$SpColorFileName
    $ThemeUrlFont  = $UrlNewSite+$FontFilePartUrl+$SpFontFileName

    $SpColorFile=$TargetWeb.GetFile($ThemeUrlColor)

    try{
        $SpFontFile=$TargetWeb.GetFile($ThemeUrlFont)
    }
    catch{
        Write-Host "No valid spfont file found in $ThemeUrlFont" -ForegroundColor Yellow
    }

    $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"
        $NewThemeItem["ThemeUrl"] = $ThemeUrlColor
        $newThemeItem["FontSchemeUrl"] = $ThemeUrlFont
        $NewThemeItem["DisplayOrder"] = 121
        $NewThemeItem.Update()
    }
    Write-Host "Create theme...completed" -ForegroundColor Green     
    $Theme=[Microsoft.SharePoint.Utilities.SPTheme]::Open($ThemeName, $SpColorFile)
    $Theme.ApplyTo($TargetWeb, $true)
    Write-Host "Apply theme...completed" -ForegroundColor Green 
}
catch
{
    Write-Host "Caught an exception while creating theme:" -ForegroundColor Red
    Write-Host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
    Write-Host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
}     
#endregion
5
  • Sorry, I have a .spcolor file and not a .thmx file so this won't help me :( Commented Oct 9, 2017 at 15:12
  • In the 2. region you are going to create a theme. Having a spcolor file is enough to run the code snippet if($SpListItems1.Count -eq 0) { $NewThemeItem = $SpList.AddItem() $NewThemeItem["Name"] = $ThemeName $NewThemeItem["Title"] = $ThemeName $NewThemeItem["MasterPageUrl"] = "$RelativeUrl$MasterPagePartUrl$Masterpage" $NewThemeItem["ThemeUrl"] = $ThemeUrlColor $newThemeItem["FontSchemeUrl"] = $ThemeUrlFont $NewThemeItem["DisplayOrder"] = 121 $NewThemeItem.Update() }
    – Majo Son
    Commented Oct 9, 2017 at 15:25
  • Thanks! Once I get my dev environment back up I will test and follow up. Commented Oct 9, 2017 at 15:30
  • So I got that to work but if I go to theme and set it back to "Inherit from parent" the site breaks/works but is all jumbled. Thoughts? Commented Oct 11, 2017 at 19:02
  • Disregard previous comment, the issue was on our end. Thanks and I have marked your response as the answer. Commented Oct 11, 2017 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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