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" } ] } } } var formData = { 'RecordTitle': $("#recordForm :input[name=RecordTitle]").val(), 'RecordRecordType': $("#recordForm :input[name=RecordRecordType]").val(), 'AccessControlList': acl }
The jquery to post the JSON looks like this:
$.ajax({ url: action, type: 'POST', dataType: 'json', contentType: 'application/json', data: JSON.stringify(formData) })
Using the .Net libraries
Creation of a record while setting the ACL is also available via the .Net client libraries. The code below constructs an TrimAccessControlList object for the purpose of setting DestroyRecord to private.
TrimClient trimClient = new TrimClient("http://localhost/ServiceAPI"); trimClient.Credentials = System.Net.CredentialCache.DefaultCredentials; TrimAccessControlList acl = new TrimAccessControlList(); acl.FunctionProfiles = new FunctionProfilesDictionary(); acl.FunctionProfiles.Add("DestroyRecord", new TrimAccessControlFunction() { Setting = AccessControlSettings.Private, ReferenceStyle = AccessReferenceStyle.NoRefNoCopy, AccessLocations = new List<LocationRef>() { new LocationRef() { Uri = 1 } } }); Record request = new Record() { Title = "from client with ACL", RecordType = new RecordTypeRef() { Uri = 2 }, AccessControlList = acl }; RecordsResponse response = trimClient.ServiceClient.Post<RecordsResponse>(request);