ServiceAPI - Attaching keywords

Overview

Keywords (otherwise known as Thesaurus terms) use the child collection pattern to establish a relationship with a record, this is similar to other related objects such as holds and locations.  One difference with keywords is that you cannot use the standard pattern of adding them to the child collection.

A fix in an upcoming release

Unfortunately existing releases (up to and including 8.2) do not throw an exception to tell you that the adding of the keyword to the ChildKeywords collection has not worked, they simply fail.  You should expect that an upcoming release will actually throw an exception and give you a clue how you proceed.

So, how should you proceed?

In 8.2 a keyword is added using the AttachKeyword service action, using the .Net client classes you would do something like this:

TrimClient trimClient = new TrimClient("http://david-pc/ServiceAPI82");
trimClient.Credentials = System.Net.CredentialCache.DefaultCredentials;

Record request = new Record();
request.Uri = 9000000015;

AttachKeyword attachKeyword = new HP.HPTRIM.ServiceModel.AttachKeyword() { 
    KeywordToAttach = new KeywordRef() { Uri = 9000000005 } 
};

request.AddAction(attachKeyword);
            
try
{
    var response = trimClient.Post<RecordsResponse>(request);               

}
catch (WebServiceException wex)
{
    Console.WriteLine(wex.ErrorMessage ?? wex.Message);
}

The code above posts a JSON object similar to this:

{
    "Uri": 9000000015,
    "ActionsToCall": [
        {
            "__type": "HP.HPTRIM.ServiceModel.AttachKeyword",
            "AttachKeywordKeywordToAttach": 9000000005
        }
    ]
}

What about earlier versions of RM?

Given that the AttachKeyword action was added in 8.2 options are limited for earlier versions, in 8.1 you may choose to write your own plugin, other than that you might have to avoid keywords.

Written on November 10, 2015