Tags
C-sharp, Microsoft, Microsoft SharePoint, Multilingual, Multilingual sites, PowerShell, SharePoint, SharePoint 2013, Variations, Windows PowerShell
“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.
- Go to “Site Actions” à“Site settings”;
-
Under “Site collection administration”, click “Variation Settings”,
-
The code below is in C# for the page above to change or update the “Variation Settings”;
-
The same code but this time in PowerShell,
-
Now we need to create labels; go back to “Site Settings”, under “Site Collection Administration” click “Variation Settings”;
-
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 😉
-
Now same code but PowerShell for creating variation labels;
-
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”,
- After creating hierarchies, the values should be yes under “Hierarchy is Created”,
-
To create Hierarchies in C#,
-
Now let’s create hierarchies in PowerShell,
Thank you and please write your comments or in case you have any questions.
Excellent !!! But the CreateVariations method in c# is missing
Thank you.
ya you are right.. there you go..
private static void CreateVariations(SPWeb rootWeb)
{
Guid varListId = new Guid(rootWeb.AllProperties[“_VarLabelsListId”] as string);
SPList varList = rootWeb.Lists[varListId];
foreach(VariationLabel label in labels)
{
SPListItem item = varList.Items.Add();
item[SPBuiltInFieldId.Title]=label.Title;
item[“Description”] = label.Description;
item[“Flag Control Display Name”]=label.FlagControlDisplayName;
item[“Language”]=label.Language;
item[“Locale”]=label.Locale.ToString();
item[“Hierarchy Creation Mode”]=label.HierarchyCreationMode;
item[“Is Source”]=label.IsSource.ToString();
item[“Hierarchy Is Created”]=false;
item.Update();
}
}
And the 2 projects are here
https://csharpspvariations.codeplex.com/
https://powershellspvariations.codeplex.com/
Good luck!
Hello. Thank you for the posting!
When I run the PowerShell code, the source variation is never created. The UI status for “Hierarchy Is Created” is always “No”, even after I manually run the “Variations” jobs.
Do you know how I can resolve this?
As troubleshooting, could u try it manually & make sure it works?
If yes, comment parts of the code & test step by step.
First is variation settings.. Not a problem, keep default. Second is create variation label.. Plz check this step carefully & that you gave it the correct web url..
And update here again plz
This is the code that I’m using to create the source variation:
$PublishingSitesAndAllPages = “Publishing Sites and All Pages”;
$PublishingSitesOnly = “Publishing Sites Only”;
$RootSitesOnly = “Root Sites Only”;
$label_US_Title=”en-us”;
$label_US_Description=”English Language Pack”;
$label_US_Name=”English US”;
$label_US_Language=”en-US”;
$label_US_Locale=1033;
$label_US_HierarchyCreationMode=$PublishingSitesAndAllPages;
$label_US_IsSource=$true;
$label_US_sitecolurl = “/”;
$label_US_SiteTemplate =”BLANKINTERNET#0″;
$rootWeb = Get-SPWeb “http://test.myserver.com”
$guid = [Guid]$rootWeb.GetProperty(“_VarLabelsListId”);
$list = $rootWeb.Lists[$guid];
$item = $list.Items.Add();
$item[“Title”] = $label_US_Title;
$item[“Description”] = $label_US_Description;
$item[“Flag Control Display Name”] = $label_US_FlagControlDisplayName;
$item[“Language”] = $label_US_Language;
$item[“Locale”] = $label_US_Locale;
$item[“Hierarchy Creation Mode”] = $label_US_HierarchyCreationMode;
$item[“Is Source”] = $label_US_IsSource;
$item[“Hierarchy Is Created”] = $false;
$item.Update();
After I run this, the source variation line appears in the Variation Labels page. But I can’t get the “Hierarchy Is Created” to change to “Yes”.
For create hierarchy.. In my code there is another function to do so
Are you sure u r running the Variation create hierarchy job
The only modifications I made to your code were:
1. Changed the “_AR_” variables to be “_US_” and updated their values using the US values.
2. Executed the code in CreateVariations() directly (as provided above).
I also run ConfigureVariationsSettings() before creating the source variation, and there are no errors reported.
I am certain that I am running the Variation create hierarchy job after creating the source variation.
If language code is incorrect .. Hierarchies won’t be created. How about keeping it the same for test?
Also plz tell me the language code exactly that u r using not just part of it
Hi Mai Omar,
In your code, you have never used the variable “$label_AR_sitecolurl”. If that is not set, hierarchies cannot be created.
AFAIK, that value is stored in a separate list.
Please let us know if we are missing something.
Thanks
Yes it was missing but look the comments as I have added it in the comments, I will add it to the post.
Sorry I just forgot it.
Tell me if you didn’t find it in the comments.
There is a comment by me on 18 Sep 2013 at 09:08 AM where I added the missing part
Hey,
Thank you for your posting. Its really helpful for me to create variation programmatically.
I have used same code for create english and arabic(UAE) Lable. after running the code its only create the “en-us” English label and not arabic. I dont know what i am missing. Please find my code below which i am using to create english label as source and arabic for target. Please advise if i am missing something to write in code.
private static VariationLabel[] labels =
{
new VariationLabel
{
Title = “en-us”,
FlagControlDisplayName = “English”,
Language = “en-US”,
Locale = 1033,
HierarchyCreationMode = CreationMode.PublishingSitesAndAllPages
IsSource = true
},
new VariationLabel
{
Title = “ar-ae”,
FlagControlDisplayName = “Arabic (U.A.E.)”,
Language = “ar-AE”,
Locale = 1025,
HierarchyCreationMode = CreationMode.PublishingSitesAndAllPages
}
};
Thanks.
Thank you very much for this article. I have a question. Do yo know if there a possibility to change the target label behavior for a label by using powershell? Thank you.
Hi Christina,
Did you got any answer to update target label behaviour using powershell or c#. I have the same requirement and looking for solution for this. Please reply.
Regards,
Vikas
Once I have created the site with variations. The variance target sites are created with Page update behavior is set to false. So none of target sites are getting the updates from source. How do I add this feature into the script?
Hi,
Awesome Article!!! Thanks for posting!!!
Do anybody have sample code to update “Label Contact” under “Target Label Behavior” grammatically.
Thanks in Advance.
Dear Mai;
Using the attached script of power shell i faced the following issue :
$item[“Flag Control Display Name”] = $label_AR_FlagControlDisplayName;
$item[“Flag_x0020_Control_x0020_Display”] = $label_AR_FlagControlDisplayName;
Column “Flag Control Display Name” value not updated.
??
But over all it is worked perfectly, and thanks for your time.
I had EN (Source) and AR (Destination) labels on DEV and variations were working, alter on which were deleted somehow. Now both EN and AR have content and i want to set variations again with same labels. Please assist.
As if i create the label with EN, it says ‘The destination web http://********/EN already exists.