2

I have a problem that when I try to add keywords to a parent keyword it add's it to the category instead (This because the webdav url is /Category/Keyword instead of /Category/ParentKeyword/Keyword)

Now I found that you have to create a LinkToKeywordData but when i try the following code, it will throw a nullpointerexception on the getWebdavUrl setvalue. What is the correct way of getting a LinkToKeywordData and ArrayOfLinkToKeywordData?

ArrayOfLinkToKeywordData array = new ArrayOfLinkToKeywordData();
for(Keyword parent : keyword.getParents()){
     LinkToKeywordData link = new LinkToKeywordData();
     link.getWebDavUrl().setValue(parent.getWebdavUrl());
     array.getLinkToKeywordData().add(link);
}
keywordData.getParentKeywords().setValue(array);
2

You can try this.

KeywordData keyword = (KeywordData)channel.GetDefaultData(ItemType.Keyword, "[CategoryId]", new ReadOptions());
keyword.Title = "Child Keyword";
keyword.Key = "Child Keyword";
keyword.ParentKeywords = new LinkToKeywordData[] { new LinkToKeywordData() { IdRef = "[ParentKeywordId]" } };

channel.Save(keyword, new ReadOptions());
  • Hmm I don't see how this would make a difference? You are still just creating a new object with all values set to null, there is no object initialization in java and you can only set values through the setValue method – Roy Teeuwen Sep 24 '13 at 8:07
0

See the below code it may help you and let me know if not work

 public static string CreateNavigationKeyword(string title, string linkTitle,string filepath, string categoryID,string ParentKeywordID, SessionAwareCoreServiceClient client,string schemaID)
    {
        string createdKey = string.Empty;
        try
        {
            KeywordData keyword = (KeywordData)client.GetDefaultData(ItemType.Keyword, categoryID);
            if (!string.IsNullOrEmpty(ParentKeywordID))
            {
                LinkToKeywordData[] _parentKeywords = new[] { new LinkToKeywordData { IdRef = ParentKeywordID } };
                keyword.ParentKeywords = _parentKeywords;
            }
            keyword.Id = "tcm:0-0-0";
            keyword.Title = title;
            keyword.Key = title;

            SchemaFieldsData schemaFields = client.ReadSchemaFields(schemaID, true, new ReadOptions());
            var metafields = Fields.ForMetadataOf(schemaFields, keyword);
            metafields["text"].Value = linkTitle;
            metafields["link"].Value = filepath;
            keyword.Metadata = metafields.ToString();

            createdKey = client.Create(keyword, new ReadOptions()).Id.ToString();
        }
        catch (Exception ex)
        {
        }
        return createdKey;
    }
0

I found the answer, in Java you need to use the ObjectFactory to create the LinkToKeywordData:

ArrayOfLinkToKeywordData aoltk = keywordData.getParentKeywords().getValue();
        List<LinkToKeywordData> list = aoltk.getLinkToKeywordData();
        ObjectFactory of = new ObjectFactory();
        for(Keyword parent : keyword.getParents()){
            LinkToKeywordData link = of.createLinkToKeywordData();
            link.setWebDavUrl(of.createLinkWebDavUrl(parent.getWebdavUrl()));
            aoltk.getLinkToKeywordData().add(link);
        }
        keywordData.getParentKeywords().setValue(aoltk)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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