I have written the below code for Localizing and Updating a list of Components in a specific publication; this may help in getting some idea of handling the Keywords - So if a Keyword does not exists, it will create it through core service before creating the component and so there is no exception: (This code is for update but can provide help in getting startup code for handling Keyword scenario)
public List<string> LocalizeAndUpdateComponent(List<ComponentXMLs> components, string targetPubID)
{
List<string> successList = new List<string>();
foreach (ComponentXMLs xml in components)
{
try
{
string tcmURI = xml.TCMURI;
TcmUri oldURI = new TcmUri(tcmURI);
ItemType type = oldURI.ItemType;
if (type == ItemType.Component)
{
string oldPubURI = oldURI.ContextRepositoryId.ToString();
if (!string.IsNullOrEmpty(tcmURI))
{
tcmURI = tcmURI.Substring(tcmURI.IndexOf('-'));
}
string localizedURI = string.Concat("tcm:", targetPubID, tcmURI);
ComponentData component = (ComponentData)client.Read(localizedURI, new ReadOptions());
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml.ComponentXML);
if (doc.DocumentElement != null && doc.DocumentElement.Attributes != null)
{
doc.DocumentElement.RemoveAttribute("xmlns");
}
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("tcm", "http://www.tridion.com/ContentManager/5.0");
ns.AddNamespace("xlink", "http://www.w3.org/1999/xlink");
ns.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");
ns.AddNamespace("xmlns: tcmi", "http://www.tridion.com/ContentManager/5.0/Instance");
SchemaData schemaData = (SchemaData)client.Read(component.Schema.IdRef, new ReadOptions());
Dictionary<string, string> keywordFields = GetKeywordFields(component.Schema.IdRef, targetPubID);
foreach (KeyValuePair<string, string> keyValue in keywordFields)
{
var keywords = client.GetList(keyValue.Key, new KeywordsFilterData());
foreach (KeywordData keyword in keywords)
{
XmlNodeList nodes = doc.SelectNodes(string.Concat(schemaData.RootElementName, "/", keyValue.Value), ns);
keyword.Title = nodes != null && nodes.Count > 0 ? nodes[0].InnerText : keyword.Title;
if (keyword.BluePrintInfo.IsLocalized != null && keyword.BluePrintInfo.IsLocalized == false)
{
client.Localize(keyword.Id, new ReadOptions());
}
if (!keyword.LockInfo.LockType.Value.HasFlag(LockType.CheckedOut))
{
client.CheckOut(keyword.Id, true, new ReadOptions());
}
client.Save(keyword, new ReadOptions());
client.CheckIn(keyword.Id, new ReadOptions());
}
}
if (component.BluePrintInfo.IsLocalized != null && component.BluePrintInfo.IsLocalized == false)
{
client.Localize(localizedURI, new ReadOptions());
}
component.Content = xml.ComponentXML.Replace("tcm:" + oldPubURI, "tcm:" + targetPubID);
if (!component.LockInfo.LockType.Value.HasFlag(LockType.CheckedOut))
{
client.CheckOut(localizedURI, true, new ReadOptions());
}
client.Save(component, new ReadOptions());
client.CheckIn(localizedURI, new ReadOptions());
successList.Add(localizedURI + " - Localized Successfully");
}
}
catch (Exception ex)
{
successList.Add(xml.TCMURI + " - Error in Localizing");
}
}
return successList;
}