Skip to content

Instantly share code, notes, and snippets.

@IvanLieckens
Created October 22, 2020 08:57
Show Gist options
  • Save IvanLieckens/c5d99be61404ca5cbc3c2bcf5b5da379 to your computer and use it in GitHub Desktop.
Save IvanLieckens/c5d99be61404ca5cbc3c2bcf5b5da379 to your computer and use it in GitHub Desktop.
SIF: Installing Language Pack
<%@ page language="C#" autoeventwireup="true" inherits="Sitecore.sitecore.admin.AdminPage" %>
<%@ import namespace="Sitecore.Abstractions" %>
<%@ import namespace="Sitecore.Jobs" %>
<%@ import namespace="Sitecore.Shell.Applications.Globalization.ImportLanguage" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
CheckSecurity(true);
string db = Request.QueryString["db"];
string file = Request.QueryString["file"];
string languages = Request.QueryString["lang"];
if (!string.IsNullOrEmpty(db) && !string.IsNullOrEmpty(file) && !string.IsNullOrEmpty(languages))
{
StartImport(db, file, languages.Split(new char[';'], StringSplitOptions.RemoveEmptyEntries).ToList());
}
}
protected void btnImportLanguage_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtDatabase.Text) && !string.IsNullOrEmpty(txtFile.Text) && !string.IsNullOrEmpty(txtLanguages.Text))
{
StartImport(txtDatabase.Text, txtFile.Text, txtLanguages.Text.Split(new char[';'], StringSplitOptions.RemoveEmptyEntries).ToList());
}
else
{
resultBox.Visible = true;
resultBox.Attributes["class"] = "wf-statebox-error";
ltlResult.Text = "All information must be filled.";
}
}
private void StartImport(string databaseName, string fileName, List<string> languages)
{
DefaultJobOptions defaultJobOptions = new DefaultJobOptions("ImportLanguage", "ImportLanguage", "Admin", new ImportLanguageForm.Importer(databaseName, fileName, languages), "Import");
defaultJobOptions.ContextUser = Sitecore.Context.User;
BaseJobOptions options = defaultJobOptions;
options.AfterLife = TimeSpan.FromMinutes(1.0);
options.WriteToLog = false;
BaseJob job = JobManager.Start(options);
resultBox.Visible = true;
resultBox.Attributes["class"] = "wf-statebox-success";
ltlResult.Text = "Job '" + job.DisplayName + "' started.";
}
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Import Language</title>
<link rel="shortcut icon" href="/sitecore/images/favicon.ico" />
<link rel="Stylesheet" type="text/css" href="/sitecore/shell/themes/standard/default/WebFramework.css" />
<style type="text/css">
.wf-configsection {
margin-bottom: 2em;
}
.wf-configsection h2 {
margin-bottom: 1em;
}
.action {
margin-top: 1em;
}
#button-container {
margin-top: 3em;
}
</style>
</head>
<body>
<form id="frmImportLanguage" runat="server" class="wf-container">
<div class="wf-content">
<h1>Import a language</h1>
<p class="wf-subtitle">
Import a new language for your user interface in Sitecore.
</p>
<div class="wf-statebox" id="resultBox" runat="server" visible="False">
<asp:Literal runat="server" ID="ltlResult"></asp:Literal>
</div>
<div class="wf-configsection">
<h2><span>Database</span></h2>
<asp:TextBox runat="server" ID="txtDatabase" placeholder="core"></asp:TextBox>
<h2><span>File</span></h2>
<asp:TextBox runat="server" ID="txtFile" placeholder="/de-DE.xml"></asp:TextBox>
<h2><span>Languages (; separated)</span></h2>
<asp:TextBox runat="server" ID="txtLanguages" placeholder="de-DE"></asp:TextBox>
</div>
<div id="button-container">
<asp:Button runat="server" ID="btnImportLanguage" Text="Import" OnClick="btnImportLanguage_Click" />
</div>
</div>
</form>
</body>
</html>
Set-StrictMode -Version 2.0
Function Invoke-SitecoreUrlFixedTask {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingUserNameAndPassWordParams','')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword','')]
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory=$true)]
[string]$SitecoreInstanceRoot,
[Parameter(Mandatory=$true)]
[string]$SitecoreActionPath,
[Parameter(Mandatory=$true)]
[string]$Username,
[Parameter(Mandatory=$true)]
[string]$Password
)
$actionPage = "$SitecoreInstanceRoot/$SitecoreActionPath"
if($PSCmdlet.ShouldProcess("$actionPage", "Invoke-SitecoreUrlFixedTask")) {
function GetAuthorizedSession {
WriteTaskInfo -MessageData $actionPage -Tag "Authenticating"
$uri = "$SitecoreInstanceRoot/sitecore/login?fbc=1"
$authResponse = Invoke-WebRequest -uri $uri -SessionVariable session -UseBasicParsing
TestStatusCode $authResponse
# Set login info
$fields = @{}
$authResponse.InputFields.ForEach({
if($_.HasMember('Value')){
$fields[$_.Name] = $_.Value
}
})
$fields.UserName = $Username
$fields.Password = $Password
$fields.Remove("ctl11")
# Login using the same session
$authResponse = Invoke-WebRequest -uri $uri -WebSession $session -Method POST -Body $fields -UseBasicParsing
TestStatusCode $authResponse
TestCookie $session.Cookies
return $session
}
function TestStatusCode {
param($response)
if($response.StatusCode -ne 200) {
throw "The request returned a non-200 status code [$($response.StatusCode)]"
}
}
function TestCookie {
param([System.Net.CookieContainer]$cookies)
$discovered = @($cookies.GetCookies($SitecoreInstanceRoot) |
Where-Object { $_.Name -eq '.ASPXAUTH' -Or $_.Name -eq '.AspNet.Cookies' })
if($discovered.Count -ne 1){
throw "Authentication failed. Check username and password"
}
}
try {
# Get an authorized session
$authSession = GetAuthorizedSession
# Use the session to perform the actual request
WriteTaskInfo -MessageData $actionPage -Tag "Requesting"
$actionResponse = Invoke-WebRequest -uri $actionPage -WebSession $authSession -UseBasicParsing
TestStatusCode $actionResponse
WriteTaskInfo -MessageData "Completed Request" -Tag "Success"
}
catch {
Write-Error -Message ("Error requesting $actionPage" + ": $($_.Exception.Message)")
}
}
}
{
"Parameters": {
"SiteName": {
"Type": "string",
"DefaultValue": "Sitecore",
"Description": "The name of the site to be deployed."
},
"SitePhysicalRoot": {
"Type": "String",
"Description": "Root folder of the CM site.",
"DefaultValue": ""
},
"DnsName": {
"Type": "string",
"Reference": "SiteName",
"Description": "The dns name of the CM site."
},
"SitecoreAdminPassword": {
"Type": "string",
"DefaultValue": "",
"Description": "The admin password for the Sitecore instance."
},
"AdminPage": {
"Type": "String",
"Description": "Location of the admin page to install.",
"DefaultValue": ""
},
"Database": {
"Type": "String",
"Description": "Name of the database to install the language pack.",
"DefaultValue": "core"
},
"File": {
"Type": "String",
"Description": "Location of the language pack XML file to use.",
"DefaultValue": ""
},
"Languages": {
"Type": "String",
"Description": "; separated list of languages to import (example: de-DE;en-US).",
"DefaultValue": ""
}
},
"Variables": {
"File.QueryString": "[concat('/', SplitPath(Path:parameter('File'), Leaf:true))]",
"Site.PhysicalPath": "[if(variable('Test.Site.PhysicalPath'),variable('Default.Site.PhysicalPath'),variable('Custom.Site.PhysicalPath'))]",
"Default.Site.PhysicalPath": "[joinpath(environment('SystemDrive'), 'inetpub', 'wwwroot', parameter('SiteName'))]",
"Custom.Site.PhysicalPath": "[joinpath(parameter('SitePhysicalRoot'), 'cm')]",
"Test.Site.PhysicalPath": "[equal(parameter('SitePhysicalRoot'),'')]",
"Site.AdminFolder": "[joinpath(variable('Site.PhysicalPath'), 'sitecore/admin')]"
},
"Tasks": {
"CopyAdminPage": {
"Description": "Install the ImportLanguage admin page.",
"Type": "Copy",
"Params": {
"Source": "[resolvepath(parameter('AdminPage'))]",
"Destination": "[variable('Site.AdminFolder')]"
},
"Skip": "[not(parameter('AdminPage'))]"
},
"CopyLanguagePack": {
"Description": "Copy the language pack file.",
"Type": "Copy",
"Params": {
"Source": "[resolvepath(parameter('File'))]",
"Destination": "[variable('Site.PhysicalPath')]"
},
"Skip": "[not(parameter('File'))]"
},
"ImportLanguagePack": {
"Description": "Import the language pack.",
"Type": "SitecoreUrlFixed",
"Params": {
"SitecoreInstanceRoot": "[concat('https://', parameter('DnsName'))]",
"SitecoreActionPath": "[concat('sitecore/admin/ImportLanguage.aspx?db=', parameter('Database'), '&file=', variable('File.QueryString'), '&lang=', parameter('Languages'))]",
"UserName": "admin",
"Password": "[parameter('SitecoreAdminPassword')]"
},
"Skip": "[or(not(parameter('File')), not(parameter('AdminPage')))]"
}
},
"Register": {
"ConfigFunction": {
"SplitPath": "Split-Path"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment