
In Creatio some records have support for an image. For example, in the Contact section you can add a contact’s photo and in the Account section you can add a company logo. This article will outline the code needed to programmatically add an image to a contact or account from a URL. The code can be easily adapted to work with other sections as well, such as products or any other section that has an image.
This code will be in C#, meaning you’ll add it to a Source Code schema. From there, you can expose this as a configuration web service, use in a script task in a process, or in an entity event. This code uses the ImageAPI found in the Terrasoft.Core.ImageAPI namespace. The complete list of using directives you’ll need to include in the code are the following:
using System; using System.IO; using System.Net; using Terrasoft.Core; using Terrasoft.Core.DB; using Terrasoft.Core.ImageAPI;
The code you’ll use is the following. Note, all you’ll need to change are the variables at the top for the record you’re adding the image to, or if you’d like to add the image to a contact instead of an account.
var imageUrl = "https://somewebsite/someimage.png"; var entityType = "Account"; // or could be "Contact" var entityId = someAccountId; // or contact Id var imageField = "AccountLogoId"; // or if a contact, use "PhotoId" using (var response = (WebRequest.Create(imageUrl) as HttpWebRequest).GetResponse()) { using (var responseStream = response.GetResponseStream()) { using (var stream = new MemoryStream()) { var fileId = Guid.NewGuid(); responseStream.CopyTo(stream); var imageApi = new ImageAPI(UserConnection); imageApi.Save(stream, response.ContentType, imageUrl, fileId); using (var dbExecutor = UserConnection.EnsureDBConnection()) { new Update(UserConnection, entityType) .Set(imageField, Column.Parameter(fileId)) .Where("Id").IsEqual(Column.Parameter(entityId)) .Execute(dbExecutor); } } } }
What the code is doing is the following:
- Reads the response from the image URL
- Copies the response stream into a memory stream
- Creates an Id that the ImageAPI will use when it creates the record on the SysImage table for the image, we’ll also use this to relate it to the account or contact record
- Save the image stream to the SysImage table using the ImageAPI object
- Lastly, it updates the Account or Contact to associate the Id of the SysImage record (created by the ImageAPI)
That is it. Now, the image specified in the URL will be saved to the Account or Contact record.
Subscribe To Our Newsletter
Join our mailing list to receive the latest Infor CRM (Saleslogix) and Creatio (bpm'online) news and product updates!
You have Successfully Subscribed!