Upload of a branding package
With this I finally managed to upload a branding package
This stored procedure will read and write all Catalog
entries:
CREATE PROCEDURE InstallBrandingPackage
(
--Arguments
@BrandingZIPPath VARCHAR(300)
,@BrandingColorsPath VARCHAR(300)
,@BrandingLogopath VARCHAR(300)
,@BrandingType VARCHAR(100)
,@BrandingName VARCHAR(100)
,@BrandingVersion VARCHAR(10)
)
AS
BEGIN
BEGIN TRANSACTION;
SET XACT_ABORT ON;
DECLARE @BrandingZIPName VARCHAR(150)=RIGHT(@BrandingZIPPath,CHARINDEX('\',REVERSE(@BrandingZIPPath))-1);
DECLARE @BrandingZIP VARBINARY(MAX);
DECLARE @BrandingColorsJSON VARBINARY(MAX);
DECLARE @BrandingLogoPNG VARBINARY(MAX);
CREATE TABLE #tmpBranding(FileType VARCHAR(100),Content VARBINARY(MAX));
DECLARE @cmd VARCHAR(MAX);
SET @cmd=
'INSERT INTO #tmpBranding(FileType,Content)
SELECT ''zip'',BulkColumn FROM OPENROWSET(BULK ''' + @BrandingZIPPath + ''', SINGLE_BLOB) AS x';
EXEC(@cmd);
SET @cmd=
'INSERT INTO #tmpBranding(FileType,Content)
SELECT ''colors'',BulkColumn FROM OPENROWSET(BULK ''' + @BrandingColorsPath + ''', SINGLE_BLOB) AS x';
EXEC(@cmd);
SET @cmd=
'INSERT INTO #tmpBranding(FileType,Content)
SELECT ''logo'',BulkColumn FROM OPENROWSET(BULK ''' + @BrandingLogopath + ''', SINGLE_BLOB) AS x';
EXEC(@cmd);
SET @BrandingZIP=(SELECT Content FROM #tmpBranding WHERE FileType='zip');
SET @BrandingColorsJSON=(SELECT Content FROM #tmpBranding WHERE FileType='colors');
SET @BrandingLogoPNG=(SELECT Content FROM #tmpBranding WHERE FileType='logo');
--needed variables and IDs taken from System Resources
DECLARE @SystemResourceID UNIQUEIDENTIFIER
,@SystemResourcePath VARCHAR(500)
,@PolicyID UNIQUEIDENTIFIER
,@UserID UNIQUEIDENTIFIER;
SELECT TOP 1 @SystemResourceID=ItemID
,@SystemResourcePath=[Path]
,@PolicyID=PolicyID
,@UserID=CreatedByID
FROM ReportServer.dbo.[Catalog] WHERE [Name] = 'System Resources';
--Delete all existing
DELETE FROM ReportServer.dbo.[Catalog] WHERE [Path] LIKE '%' + @BrandingType + '%';
--New Variables
DECLARE @NewZipID UNIQUEIDENTIFIER = NEWID();
DECLARE @NewPathID UNIQUEIDENTIFIER = NEWID();
DECLARE @NewPath VARCHAR(100) = '/' + LOWER(CAST(NEWID() AS VARCHAR(100)));
DECLARE @NewPathName VARCHAR(100) ='fbac82c8-9bad-4dba-929f-c04e7ca4111f'; --It seems, that this special GUID is needed, otherwise no Logo is displayed
DECLARE @NewBrandID UNIQUEIDENTIFIER = NEWID();
DECLARE @ColorsID UNIQUEIDENTIFIER = NEWID();
DECLARE @LogoID UNIQUEIDENTIFIER = NEWID();
DECLARE @dt DATETIME=GETDATE();
DECLARE @BrandProperties NVARCHAR(MAX)=
CAST((
SELECT @BrandingType AS [Resource.Type]
,@BrandingName AS [Resource.Name]
,@BrandingVersion AS [Resource.Version]
,LOWER(CAST(@NewZipID AS VARCHAR(100))) AS [Resource.PackageId]
,LOWER(CAST(@ColorsID AS VARCHAR(100))) AS [Item.colors]
,LOWER(CAST(@LogoID AS VARCHAR(100))) AS [Item.logo]
FOR XML PATH('Properties'),TYPE
) AS NVARCHAR(MAX));
--Universal Brand
INSERT INTO ReportServer.dbo.[Catalog](ItemID,[Path],[Name],ParentID,[Type]
,Content
,Property
,[Hidden],CreatedByID,CreationDate,ModifiedByID,ModifiedDate
,MimeType,PolicyID,PolicyRoot,ExecutionFlag)
VALUES(@NewBrandID,@SystemResourcePath + '/' + @BrandingType, @BrandingType, @SystemResourceID,1
,NULL
,@BrandProperties
,0,@UserID,@dt,@UserID,@dt,NULL,@PolicyID,0,1);
--ZIP file dummy
DECLARE @currentPath VARCHAR(500) = @SystemResourcePath + '/' + @BrandingType + '/' + @BrandingZIPName;
INSERT INTO ReportServer.dbo.[Catalog](ItemID,[Path],[Name],ParentID,[Type]
,Content
,Property
,[Hidden],CreatedByID,CreationDate,ModifiedByID,ModifiedDate
,MimeType,PolicyID,PolicyRoot,ExecutionFlag)
VALUES(@NewZipID,@currentPath,@BrandingZIPName,@NewBrandID,3
,@BrandingZIP
,'<Properties />'
,0,@UserID,@dt,@UserID,@dt,'application/octet-stream',@PolicyID,0,1);
--Brand path
SET @currentPath = @SystemResourcePath + '/' + @BrandingType + '/' + @NewPathName;
INSERT INTO ReportServer.dbo.[Catalog](ItemID,[Path],[Name],ParentID,[Type]
,Content
,Property
,[Hidden],CreatedByID,CreationDate,ModifiedByID,ModifiedDate
,MimeType,PolicyID,PolicyRoot,ExecutionFlag)
VALUES(@NewPathID,@currentPath,@NewPathName,@NewBrandID,1
,NULL
,'<Properties />'
,0,@UserID,@dt,@UserID,@dt,NULL,@PolicyID,0,1);
--colors
INSERT INTO ReportServer.dbo.[Catalog](ItemID,[Path],[Name],ParentID,[Type]
,Content
,Property
,[Hidden],CreatedByID,CreationDate,ModifiedByID,ModifiedDate
,MimeType,PolicyID,PolicyRoot,ExecutionFlag)
VALUES(@ColorsID,@currentPath + '/colors','colors',@NewPathID,3
,@BrandingColorsJSON
,'<Properties />'
,0,@UserID,@dt,@UserID,@dt,'application/octet-stream',@PolicyID,0,1);
--logo
INSERT INTO ReportServer.dbo.[Catalog](ItemID,[Path],[Name],ParentID,[Type]
,Content
,Property
,[Hidden],CreatedByID,CreationDate,ModifiedByID,ModifiedDate
,MimeType,PolicyID,PolicyRoot,ExecutionFlag)
VALUES(@LogoID,@currentPath + '/logo','logo',@NewPathID,3
,@BrandingLogoPNG
,'<Properties />'
,0,@UserID,@dt,@UserID,@dt,'image/png',@PolicyID,0,1);
COMMIT;
END
And with this PowerShell lines I unzip the package, extract the parameters and call the procedure:
#Unzip BrandingPackage
Add-Type -AssemblyName System.IO.Compression.FileSystem
Add-Type -AssemblyName System.Collections
$BrandingPackagePath = ($PSScriptRoot + "\FrequentisReportCenter.zip")
$BrandingPackageUnzipped = ($PSScriptRoot + "\FrequentisReportCenter.zip.Unzipped")
[System.IO.Compression.ZipFile]::ExtractToDirectory($BrandingPackagePath,$BrandingPackageUnzipped)
Write-Host "BrandingPackage unzipped"
$xml = New-Object System.Xml.XmlDocument
$nsMngr = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$nsMngr.AddNamespace("ns","http://schemas.microsoft.com/sqlserver/reporting/2016/01/systemresourcepackagemetadata")
$xml.Load(($BrandingPackageUnzipped + "\" + "metadata.xml"))
$BrandingType=$xml["SystemResourcePackage"].Attributes["type"].Value
$BrandingVersion=$xml["SystemResourcePackage"].Attributes["version"].Value
$BrandingName=$xml["SystemResourcePackage"].Attributes["name"].Value
$PathColors=($BrandingPackageUnzipped + "\" + $xml.SelectNodes("//ns:Contents/ns:Item[@key='colors']",$nsMngr)[0].Attributes["path"].Value)
$PathLogo=($BrandingPackageUnzipped + "\" + $xml.SelectNodes("//ns:Contents/ns:Item[@key='logo']",$nsMngr)[0].Attributes["path"].Value)
#BrandingPackage values fetched
$cmd.CommandText="MyDatabase.dbo.InstallBrandingPackage `
@BrandingZIPPath=@ZIPPath,`
@BrandingColorsPath=@ColorsPath,`
@BrandingLogoPath=@LogoPath,`
@BrandingType=@Type,`
@BrandingName=@Name,`
@BrandingVersion=@Version"
$cmd.Parameters.AddWithValue("@ZIPPath",$BrandingPackagePath)
$cmd.Parameters.AddWithValue("@ColorsPath",$PathColors)
$cmd.Parameters.AddWithValue("@LogoPath",$PathLogo)
$cmd.Parameters.AddWithValue("@Type",$BrandingType)
$cmd.Parameters.AddWithValue("@Name",$BrandingName)
$cmd.Parameters.AddWithValue("@Version",$BrandingVersion)
$cmd.ExecuteNonQuery()
#BrandingPackage written