Back to previous page

Automate Variations in SharePoint 2013 Using PowerShell & C#

“Thanks to Waldek Mastykarz for his post, he posted about it but for SP 2010 and only in C#” but then I was able to convert it to Powershell, also thanks to “Eric Soo” for his reply but I was done already but thank you both.”

Let’s Start.. Automating variations settings and configuration so whenever a deployment is needed, you will be ready with your PowerShell scripts.

Manual deployment is time consuming as it is repetitive task, in this post I will let you know how to automate one of those many tasks when it comes to deployment.

SharePoint Variations has a UI which I will show you its steps now;

The steps in this post is for automation of variation in UI, in C# & finally in PowerShell.

1.  Go to “Site Actions”à”Site settings”;

2.  Under “Site collection administration”, click “Variation Settings”,

Automate Variations

Site Setting > Variation Settings

3.  The code below is in C# for the page above to change or update the “Variation Settings”;

private static void ConfigureVariationsSettings(SPWeb rootWeb)


Guid varRelationshipsListId = new Guid(rootWeb.AllProperties[“_VarRelationshipsListId”] as string);

SPList varRelationshipsList = rootWeb.Lists[varRelationshipsListId];

SPFolder rootFolder = varRelationshipsList.RootFolder;

// Automatic creation

rootFolder.Properties[“EnableAutoSpawnPropertyName”] = “true”;

// Recreate Deleted Target Page; set to false to enable recreation

rootFolder.Properties[“AutoSpawnStopAfterDeletePropertyName”] = “false”;

// Update Target Page Web Parts

rootFolder.Properties[“UpdateWebPartsPropertyName”] = “true”;

// Resources

rootFolder.Properties[“CopyResourcesPropertyName”] = “true”;

// Notification

rootFolder.Properties[“SendNotificationEmailPropertyName”] = “false”;

rootFolder.Properties[“SourceVarRootWebTemplatePropertyName”] = “CMSPUBLISHING#0”;


SPListItem item = null;

if (varRelationshipsList.Items.Count > 0)


item = varRelationshipsList.Items[0];




item = varRelationshipsList.Items.Add();

item[“GroupGuid”] = new Guid(“F68A02C8-2DCC-4894-B67D-BBAED5A066F9”);


item[“Deleted”] = false;

item[“ObjectID”] = rootWeb.ServerRelativeUrl;

item[“ParentAreaID”] = String.Empty;



4.  The same code but this time in PowerShell

function ConfigureVariationsSettings



$site =Get-SPSitehttp://sp2013dev:30204/sites/Home3

$guid = [Guid]$rootWeb.GetProperty(“_VarRelationshipsListId”);

$list = $rootWeb.Lists[$guid];

$rootFolder = $list.RootFolder;

$rootFolder.Properties[“EnableAutoSpawnPropertyName”] =”false”;

$list.RootFolder.Properties[“AutoSpawnStopAfterDeletePropertyName”] = “false”;

$list.RootFolder.Properties[“UpdateWebPartsPropertyName”] = “false”;

$list.RootFolder.Properties[“CopyResourcesPropertyName”] = “true”;

$list.RootFolder.Properties[“SendNotificationEmailPropertyName”] = “false”;

$list.RootFolder.Properties[“SourceVarRootWebTemplatePropertyName”] = “CMSPUBLISHING#0”;


$item = $null;

if (($list.Items.Count -gt 0))


$item = $list.Items[0];




$item = $list.Items.Add();

$item[“GroupGuid”] =new-objectSystem.Guid(“F68A02C8-2DCC-4894-B67D-BBAED5A066F9”);


$item[“Deleted”] = $false;

$item[“ObjectID”] = $site.RootWeb.ServerRelativeUrl;

$item[“ParentAreaID”] = [System.String]::Empty;



5.  Now we need to create labels; go back to “Site Settings”, under “Site Collection Administration” click “Variation Settings”;

Automate Variations

Site Settings > Variation Labels

6.  I have 2 labels, currently if this is your first time, it will be empty, click now “New Label” (as if you are telling SharePoint List to create new item) since we will do this in the code 😉

public class VariationLabel


public string Title {get;set;}

public string Description {get; set;}

public string FlagControlDisplayName {get;set;}

public string Language {get; set;}

public uint Locale {get; set;}

public string HierarchyCreationMode {get; set;}

public string SourceVarRootWebTemplate {get;set;}

public bool IsSource{get;set;}


public static class CreationMode


public const string PublishingSitesAndAllPages = “Publishing Sites and All Pages”;

public const string PublishingSitesOnly =”Publishing Sites Only”;

public const string RootSitesOnly =”Root Sites Only”;


private static VariationLabel[] labels = {

new VariationLabel



FlagControlDisplayName=”Arabic Egypt”,






new VariationLabel


Title =”en-us”,

FlagControlDisplayName=”English US”,

Language =”en-US”,





7.  Now same code but PowerShell for creating variation labels;

$PublishingSitesAndAllPages = “Publishing Sites and All Pages”;

$PublishingSitesOnly = “Publishing Sites Only”;

$RootSitesOnly = “Root Sites Only”;


$label_AR_Description=”Arabic Language Pack”;

$label_AR_Name=”Arabic Egypt”;





$label_AR_sitecolurl = “/”;

$label_AR_SiteTemplate =”BLANKINTERNET#0″;


$label_EN_Description=”English Language Pack”;

$label_EN_Name=”English US”;





function CreateVariations



$guid = [Guid]$rootWeb.GetProperty(“_VarLabelsListId”);

$list = $rootWeb.Lists[$guid];

$item = $list.Items.Add();

$item[“Title”] = $label_AR_Title;

$item[“Description”] = $label_AR_Description;

$item[“Flag Control Display Name”] = $label_AR_FlagControlDisplayName;

$item[“Language”] = $label_AR_Language;

$item[“Locale”] = $label_AR_Locale;

$item[“Hierarchy Creation Mode”] = $label_AR_HierarchyCreationMode;

$item[“Is Source”] = $label_AR_IsSource;

$item[“Hierarchy Is Created”] = $false;


$item = $list.Items.Add();

$item[“Title”] = $label_EN_Title;

$item[“Description”] = $label_EN_Description;

$item[“Flag Control Display Name”] = $label_EN_FlagControlDisplayName;

$item[“Language”] = $label_EN_Language;

$item[“Locale”] = $label_EN_Locale;

$item[“Hierarchy Creation Mode”] = $label_EN_HierarchyCreationMode;

$item[“Is Source”] = $label_EN_IsSource;

$item[“Hierarchy Is Created”] = $false;



8.   Now to create hierarchies; you should see the labels added as below, click above them “Create Hierarchies”, before creating hierarchies, the values for the labels are “No”,

Automate Variations

Click ‘Create Hierarchies’

9.  After creating hierarchies, the values should be yes under “Hierarchy is Created”,

10.   To create Hierarchies in C#,

private static void CreateHierarchies(SPSite site, SPWeb rootWeb)


site.AddWorkItem(Guid.Empty, DateTime.Now.ToUniversalTime(), new Guid(“e7496be8-22a8-45bf-843a-d1bd83aceb25”),

rootWeb.ID, site.ID, 1, false, Guid.Empty, Guid.Empty,

rootWeb.CurrentUser.ID, null, String.Empty, Guid.Empty, false);

SPWebApplication webApplication = site.WebApplication;

SPJobDefinition variationsJob = (from SPJobDefinition job in


where job.Name == “VariationsCreateHierarchies”

select job).FirstOrDefault();

if (variationsJob != null)


DateTime startTime = DateTime.Now.ToUniversalTime();


// wait until the job is finished

while ((from SPJobHistory j in webApplication.JobHistoryEntries

where j.JobDefinitionId == variationsJob.Id && j.StartTime > startTime select j).Any() == false)






11.  Now let’s create hierarchies in PowerShell,

function CreateHierarchies


#param($site, $rootWeb);

$site =Get-SPSitehttp://sp2013dev:30204/sites/Home3

$rootWeb =Get-SPWebhttp://sp2013dev:30204/sites/Home3

$id = [Guid](“e7496be8-22a8-45bf-843a-d1bd83aceb25”);

$site.AddWorkItem([System.Guid]::Empty, [System.DateTime]::Now.ToUniversalTime(), $id, $rootWeb.ID, $site.ID, 1, $false,

[System.Guid]::Empty, [System.Guid]::Empty, $rootWeb.CurrentUser.ID, $null, [System.String]::Empty, [System.Guid]::Empty, $false);

$webApplication = $site.WebApplication;

$variationsJob = $webApplication.JobDefinitions |where{ $_.Name -match “VariationsCreateHierarchies” };



For more articles check out Mai Omar Desouki blog. For more from Mai Omar, check out her ESPC13 conference presentation on ‘What’s new in SharePoint 2013 Designer’. Download Now>>

Share this on...

Leave a Reply

Back to previous page