Skip and go to main content

Design & Technology June 22, 2011

Creating Keywords using the Core Service in SDL Tridion 2011

Image

Creating keywords in SDL Tridion 2011 is a very easy thing to do. This example shows you how you can import the .Net culture information into a specified category using the Core Service:

[code lang=”c-sharp”]
public static void CreateCultures()
{
using (Tridion.CoreService2010Client client = new Tridion.CoreService2010Client())
{
//get the Id of the publication to import into
string publicationUri = ConfigurationManager.AppSettings[“MasterContentPublicationId”].ToString();

//open the category that is already created in Tridion
CategoryData category = (CategoryData)client.Read(ConfigurationManager.AppSettings[“CultureCategoryId”].ToString(), null);

//loop through all the cultures in the .Net framework
foreach (System.Globalization.CultureInfo ci in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures))
{
//create a new keyword
Tridion.KeywordData keyword = (Tridion.KeywordData)client.GetDefaultData(Tridion.ItemType.Keyword, category.Id);
//set the id to 0 to notify Tridion that it is new
keyword.Id = “tcm:0-0-0”;
//set the required fields
keyword.Title = ci.TextInfo.CultureName;
keyword.Key = ci.TextInfo.CultureName;
//create the keyword
client.Create(keyword, null);
}
}
}
[/code]

This is extremely handy when you are creating multilingual websites to allow users to specify the culture for each site. This code could also be used for populating a date format category or a currency category etc

Questions? We are here to help!