Programmatically Adding an Image to a Contact or Account Record from a URL in Creatio

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:

  1. Reads the response from the image URL
  2. Copies the response stream into a memory stream
  3. 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
  4. Save the image stream to the SysImage table using the ImageAPI object
  5. 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.

ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

Submit a Comment

Your email address will not be published.

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!