I need to add a Keyword based on its Keyword title using the Core Service. Please help me which function of Core Service I can use along with some sample code if possible as well.

I have a Keyword field name called "familyKeyword" Here I just need to insert a Keyword to field name family keyword using Core Service.

string domainName = System.Configuration.ConfigurationSettings.AppSettings["domainName"];

        string userName = System.Configuration.ConfigurationSettings.AppSettings["userName"];

        string passowrd = System.Configuration.ConfigurationSettings.AppSettings["passowrd"];
        timestamp = DateTime.Now.ToString("dd:MM:yy h:mm:ss tt").Replace(":", "_").Replace(" ", "");

        string content = string.Empty;
        //string familyKeyword = "familyKeyword";
        //string keywordTitle = "Aromatics & Fibers";

        client = CoreServiceClientFactory.GetClient();

        var ComponentContent = client.Read("tcm:13-149625", new ReadOptions()) as ComponentData;
        content = ComponentContent.Content;

1 Answer 1


What you are looking for is to have the component tcm:13-149625 updated. Since you want the keyword Aromatics & Fibers inserted into the familyKeyword field, you would have to first read the keyword and update the component by inserting a new node which would insert the keyword title to the field.

// Parse the component's content into an XML.
var contentXml = XElement.Parse(component.Content);

// Read the "Aromatics & Fibers" keyword.
KeywordData keywordData = (KeywordData)client.Read("tcm:xx-xxx-1024", null);

// Create a new node for the "familyKeyword" field. 
XNamespace ns = "http://www.w3.org/1999/xlink";
XElement familyKeywordNode = new XElement(contentXml.GetDefaultNamespace() + "familyKeyword", 
    new XAttribute(XNamespace.Xmlns + "xlink", ns),
    new XAttribute(ns + "href", keywordData.Id),
    new XAttribute(ns + "title", keywordData.Title));
familyKeywordNode.Value = keywordData.Title;

// Add the  "familyKeyword" field into the content.

// Update the component.
component.Content = contentXml.ToString();
client.Update(component, null);

This stack exchange question How to update the component particular field node using core service about how to update components should also help you. Hope this helps!

  • the keyword is getting insert at the last in the source .i need to add the familykeyword in between two fields say "x Field" + family Keyword Field +"yField"
    – Jeevitha
    Commented Mar 13, 2020 at 11:28
  • This is just XML manipulation. You'd have to grab the node you want to insert after. Here in your case x Field and use AddAfterSelf on this object to insert the familykeyword. Commented Mar 13, 2020 at 16:16
  • can i get a sample code? I tried it was not working for me. Sample code is much appreciated. thanks!!
    – Jeevitha
    Commented Mar 15, 2020 at 15:53
  • This should help - stackoverflow.com/questions/29972412/…. Here a node is inserted after a specific node. Note: in your case, there are namespaces and you should factor that in too. Commented Mar 18, 2020 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.