Tag: dotnet-serviceapi-client
- identify the Record for which you want a new version,
- create a Record object using the original Record Uri,
- set the NewType property,
- update any properties you want to be different on the new version (for example: set the Notes to empty, then
- post the Record object as per usual.
- a communication must be related to a record, the Record property controls this.
- a communication must have a sender and recipient and these must be HPRM location objects, if you do not have a location for the recipient you must create one.
- the ChildDetails is a child list as described in this post.
- not having to write a file a file unnecessarily, and
- avoiding the non-thread safe approach in the previous post.
Sending service actions with PostFileWithRequest
The PostFileWithRequest operates differently to the normal Post request in that instead of sending a simple JSON object it sends a multi-part form request with the object (e.g. a Record) serialized not to JSON but as an HTML form. This means that some complex constructs, such as the Record.AccessControlList, do not translate well.
Service Actions, sent in ActionsToCall, nearly work except for the code that specifies the type of Service Action. The good news is that this can be tweaked on the client side by overriding the code to emit the type name. In the sample below the line JsConfig.TypeWriter... called after TrimClient is instantiated, should allow any Service Action to be de-serialized by the ServiceAPI.
TrimClient trimClient = new TrimClient("http://MyServer/ServiceAPI"); trimClient.AlwaysSendBasicAuthHeader = true; trimClient.UserName = "USERNAME"; trimClient.Password = "PASSWORD";
Attaching contacts to a Record using the ServiceAPI
A further update
Was not thinking as clearly as I could have been last time I posted. The best way to get the contacts is via the ChildLocations property as this will tell you the relationship type, for example:
Records request = new Records(); request.q = "REC_349"; request.Properties = new PropertyList("ChildLocations");
ServiceAPI - page through the entire result set
There are a few parameters that assist you in paging through an entire pageSet. If you have a good connection to the workgroup server (e.g. server to server) then make the pageSize larger rather than smaller. The response property HasMoreItems will return false when you are finished. The sample below assumes basic authentication, check out the ServiceAPI documentation for other authentication methods.
Sample
TrimClient trimClient = new TrimClient("http://localhost/ServiceAPI"); trimClient.AlwaysSendBasicAuthHeader = true; trimClient.UserName = "MY_USER_NAME"; trimClient.Password = "MY_PASSWORD";
Create a new Record version in the ServiceAPI
There is a sample for creating a new version of a Record in the ServiceAPI help, found here: /examples/RecordNewVersion in your ServiceAPI help. This sample uses a standard form post. The technique using the .Net wrapper classes is very similar. The process is:
Example
Record record = new Record(); record.Uri = 9000008003; record.NewType = NewType.Version; // this tells the request I want the Record Number property in the response. record.Properties = new List<string>() { "RecordNumber" };
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 creation with ACL in the ServiceAPI
I had a question the other day about creating a new record and setting the ACL at the same time. This led me to realise that the only sample for this in the ServiceAPI documentation was quite complex (and only for record update not creation). In an attempt to remedy this I have added a new sample. To use this copy the file into your ServiceAPI 'examples' folder and then open the URL: http://<myServer>/HPRMServiceAPI/examples/CreateWithACL_JSON.
Code
The actual code simply composes a JSON object to represent the new record and includes an AccessControlList property containing a valid ACL object. This object is documented in the ServiceAPI documentation. The JSON that is posted looks like this:
var acl = { "FunctionEnum": "RecordAccess", "FunctionProfiles": { "DestroyRecord": { "Setting": "Private", "ReferenceStyle": "NoRefNoCopy", "AccessLocations": [ { "FindBy": "me" } ] } } }
ServiceAPI - Attach an action
Background
Today's question of the day from the forums is how to attach an action from the ServiceAPI. Took me a few minutes to work out exactly what was going on here as the ServiceAPI inherits a little bit of the opacity of the SDK.
The Code
TrimClient trimClient = new TrimClient("http://david-pc/ServiceAPI"); trimClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
How to create a communication
Background
A common activity around a record is to send and receive communications in relation to it, be they physical letters or emails. If you want an audit trail of communications sent and received you may be interested in the communication object.
Create a communication
The code below uses the ServiceAPI .Net client library to create a new communication. Some points to note are:
TrimClient trimClient = new TrimClient("http://david-pc/ServiceAPI"); trimClient.Credentials = System.Net.CredentialCache.DefaultCredentials; Communication communication = new Communication(); communication.Record = new RecordRef() { Uri = 9000000018 }; communication.Direction = CommunicationDirection.Outgoing;
Today's question - creating record relationships from the ServiceAPI
Background
In HPRM records may have a variety of different relationships, from 'Redaction of' to 'Related to'. These relationships are managed in both the SDK and ServiceAPI via the child collection construct. To add a relationship you add a new item to the ChildRelationships collection, to remove a relationship you remove an item from this collection.
C#
This code will create a new relationship using the .Net client for the ServiceAPI.
TrimClient trimClient = new TrimClient("http://MyHost]/ServiceAPI"); trimClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
Simple document download
Background
In a previous post I showed how to patch the ServiceAPI TrimClient to fix a problem with downloading child documents (e.g. record revisions). A customer (Mark from WA) pointed out a way to download a file to a stream without having to write it to a file.
Examples
The following code downloads a document using a stream. I then save to a file but it could also be passed on as a stream or byte array to another system.
Stream stream = trimClient.Get<Stream>(string.Format("Record/{0}/File/Document", 1843)); using (FileStream fs = File.Create("d:\\junk\\myfile.xml")) { stream.CopyTo(fs); }
Below is the code to use to get a record revision as a stream.
Stream stream = trimClient.Get<Stream>(string.Format("Record/{0}/RecordRevision/{1}", 1843, 12));
Side benefits
Two side benefits of this approach are:
Deleting Records in the ServiceAPI from .Net
Background
Unfortunately deleting a Record (or other object type) is slightly different to other ServiceAPI requests. This is both because most requests assume a response and also because the delete service is not explicitly built into the .Net client.
Two ways to delete
Each HPRM object has a Delete service which can be posted to via a URL following this pattern: /Record/{Id}/Delete. Given that the .Net client classes do not have a facility to post to a specified URL you would need to write your own code to post to this URL.
The second way to delete is to post the deletion request to one of the built-in service endpoints. This can be done like this:
trimClient.Post((new DeleteRecord() { Id = 1837.ToString(), DeleteContents = true });
Or this:
trimClient.Post(new DeleteMainObject() { TrimType = BaseObjectTypes.Record, Id = "1838" });
The catch
Not every version of the ServiceAPI enables the built-in service endpoints by default. They can be switched on by adding PreDefinedRoutes to the serviceFeatures in the hptrim.config file.
<hptrim poolSize="1000" serviceFeatures="Json,PreDefinedRoutes,Razor,Html" ... >