テーマ可能なCSSが進化するときに複合的な外観を再生成する方法?
-
29-09-2020 - |
質問
企業公開サイトでは、Composite Managerがブランドの配色を選択できるようにするコンポジットを使用しています。
基本的には、2つのCSSファイルを使用してポータルをブランド化しました。ほとんどのルールを含むmain.css
ファイル、およびStyle library
にあります。テーマに依存しているすべての規則を含む別のtheme.css
。
だからユーザーはサイト設定の「外観の変更」メニューを使って自分の配色を選択できます。
これは非常によく機能しています(システムマスターページに関連するいくつかのGotchasですが、それは範囲外です)。
しかし、theme.css
ファイルの実装にはいくつかの変更があります。コンポジットルックエンジンのために、このファイルの変更はテーマが再度適用されるまで反映されません。
ポータルには多くの面積があるため、すべてのサブサイトでテーマを手動で再生成するのに質問がありません。
テーマの再生を強制する方法はありますか?
必要に応じてPowerShellスクリプトを使用できます。私は試してみました:
function ReapplyTheme{
param(
[Microsoft.SharePoint.SPWeb]$Web
)
$file=$web.GetFile($web.ServerRelativeUrl.TrimEnd('/') + "/_catalogs/theme/15/Palette_rouge.spcolor")
$theme=[Microsoft.SharePoint.Utilities.SPTheme]::Open("Name of my theme", $file)
$theme.ApplyTo($web, $false)
}
.
しかし、このスクリプトはサブサイトを破る。さらに、カラーパレットとテーマの名前はハードコードされています...多分値の後どこかに見える?
解決
PowerShellスクリプトを使用して問題を解決し、テーマが適用されているかどうかを確認することができます。
あなたの情報のために、Spthemeクラスではいくつかの静的メソッドを見つけることができます:
- 現在のテーマを取得するには、静的メソッドを使用できます.plicalpliedtheme
- テーマのスタイルを再生成するために静的メソッドを使用することができますenforcetheMedstylesForWeb
- またはApplytoメソッド を使用して現在のテーマを簡単に再適用することもできます。
public static void ReapplyCurrentComposedLook(SPWeb web)
{
// try to get the currently applied theme
SPTheme theme = SPTheme.OpenAppliedTheme(web);
if (theme == null)
{
// return if no theme is applied
return;
}
// reinforce the styles for the web..
SPTheme.EnforceThemedStylesForWeb(web);
// .. or simply reapply the whole theme.
theme.ApplyTo(web, true);
}
. 他のヒント
ついに私のスクリプトが進化しました。
基本的に、各サブウェブでは、現在のテーマである順序 "0"で構成された外観の後に見てください。また、Webオブジェクトのプロパティを確認して、Webがその親を継承するかどうかを確認します。
発見された構成された外観からのデータを使用すると、それらを再生することができます。
は非常にうまく機能しているようです。
これはスクリプトです:
#requires -PSSnapin Microsoft.SharePoint.PowerShell
param(
[Parameter(Mandatory = $true, Position=0, ParameterSetName='SPSite')]
[Microsoft.SharePoint.PowerShell.SPSitePipeBind]$Site,
[Parameter(Mandatory = $true, Position=0, ParameterSetName='SPWeb')]
[Microsoft.SharePoint.PowerShell.SPWebPipeBind]$Web,
[Parameter(Position=1)]
[Switch]$Recurse=$true
#TODO: add argument to allow specify a specific theme, instead of regenerating it
)
$actualWeb = if(-Not $Web) {
$site.Read().RootWeb
} else{
$Web.Read()
}
function Get-ThemeData{
param(
[Parameter(Mandatory=$true)]
[Microsoft.SharePoint.SPWeb]$Web
)
$designList = $Web.GetList($web.ServerRelativeUrl.TrimEnd('/') + "/_catalogs/design")
$query = New-Object Microsoft.SharePoint.SPQuery
$query.ViewFieldsOnly = $true
$query.ViewFields = "<FieldRef Name='ThemeUrl' /><FieldRef Name='ImageUrl' /><FieldRef Name='FontSchemeUrl' />"
$query.Query = "<Where><Eq><FieldRef Name='DisplayOrder' /><Value Type='Number'>0</Value></Eq></Where>";
$designItem = $designList.GetItems($query)[0]
$resultProperties = @{
"Web" = $Web.ServerRelativeUrl
"FontSchemeUrl" = $designItem["FontSchemeUrl"]
"ThemeUrl" = $designItem["ThemeUrl"]
"ImageUrl" = $designItem["ImageUrl"]
"InheritsThemedCssFolderUrl" = [bool]::Parse($web.AllProperties["__InheritsThemedCssFolderUrl"])
"InheritsCustomMasterUrl" = [bool]::Parse($web.AllProperties["__InheritsCustomMasterUrl"])
"InheritsMasterUrl" = [bool]::Parse($web.AllProperties["__InheritsMasterUrl"])
MasterUrl=$web.MasterUrl
CustomMasterUrl=$web.CustomMasterUrl
}
New-Object PSObject -Property $resultProperties
}
function Process-SPWeb{
param(
[Parameter(Mandatory=$true)]
[Microsoft.SharePoint.SPWeb]$Web
)
Write-Host "Traitement de $($web.ServerRelativeUrl)"
$themeData = Get-ThemeData $Web
if(-not $themeData.InheritsThemedCssFolderUrl){
$color = if($themeData.ThemeUrl) { $web.GetServerRelativeUrlFromUrl((New-Object Microsoft.SharePoint.SPFieldUrlValue($themeData.ThemeUrl)).Url) }
$font = if($themeData.FontSchemeUrl) { $web.GetServerRelativeUrlFromUrl((New-Object Microsoft.SharePoint.SPFieldUrlValue($themeData.FontSchemeUrl)).Url) }
$image = if($themeData.ImageUrl) { $web.GetServerRelativeUrlFromUrl((New-Object Microsoft.SharePoint.SPFieldUrlValue($themeData.ImageUrl)).Url) }
$web.ApplyTheme($color,$font,$image, $false)
$web2 = Get-SPWeb $web.url
$web2.MasterUrl = $themeData.MasterUrl
$web2.CustomMasterUrl = $themeData.CustomMasterUrl
$web2.Update()
$web2.Dispose()
Write-Warning "Thème regénéré sur $($web.ServerRelativeUrl)"
}
if($Recurse){
$Web.Webs | % { Process-SPWeb $_ }
}
}
Process-SPWeb $actualWeb
.