Question

Import photo / image via ImageAPIService

Hello community

We have a use case where we need to migrate photos (Eg Contact display photo) into Creatio. We are aware that images are stored in the SysImage table, but going via the Database is not an option for us. 

I was looking into the 0/ImageAPIService/upload which is used by the platform internally when a Contact photo is uploaded. Can we use call API to upload an image into the SysImage table?? Subsequently we can map this Image the relevant record using the out of the box OData APIs. Few related questions below - 

1. Is the above API approach feasible?

2. I looked into what exact request was being sent and I notice a few query parameters - fileapi, totalFileLength, fileId and mimeType. If we were to replicate this request via code, Which of these are mandatory and which are optional? 
3. What does the query parameter "fileapi16123676529964" refer to? And what is that number??
4. Believe the unique Guid() for the SysImage record is generated on the client side, sent via the fileId query param and gets stored in the Id column in the SysImage table. Is this right?

5. Besides using the 0/ImageAPIService/upload API and the DB approach, is there any other way to import/migrate images into Creatio and map them to specific records?

Like 0

Like

6 comments
Best reply

M Shrikanth,

You can create a C# code schema with the following to add an image for an account or contact from a Url. This uses the ImageAPI found in the Terrasoft.Core.ImageAPI namespace.

var url = "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(url) 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, url, fileId);
 
			using (var dbExecutor = UserConnection.EnsureDBConnection())
			{
				new Update(UserConnection, entityType)
					.Set(imageField, Column.Parameter(fileId))
					.Where("Id").IsEqual(Column.Parameter(entityId))
					.Execute(dbExecutor);
			}
		}
	}
}

This could be exposed as your own web service to simplify adding images to records. 

Hope this helps.

Ryan

Hello,

 

Please either use the database upload or use the standard approach of uploading files to the "Attachments and notes" detail. The ImageAPIService service is not supposed to be used for mass file upload and the database insert is the best approach in your case.

 

Best regards,

Oscar 

Oscar Dylan,

Hi Oscar. We do not want to migrate files into "Attachments & Notes" detail but rather migrate images so that they can be set as the profile picture (Ref screenshot https://prnt.sc/ylto0l). I don't think you can upload an image to the Attachments detail and then set it as the profile picture. Can you?

1. We have images stored in AWS S3 and not available in the file system to migrate. Hence, going via API is much more preferred than going via the DB.

2. Any specific reason why ImageAPIService should not or cannot be used? It is used by the platform when a user uploads a profile picture. Then why not use it from an external system??

3. Can you qualify what you mean by "mass file upload". Our use case is to migrate images before the system is used or goes live - We do not want to use this after the system has gone live or is being used by users. Given this context, Can you help with my questions above and also the original question of what the "fileapi" query parameter refers to?

4. Is the SysImage table exposed via OData? I verified that it is. But wasn't able to insert records into it via OData. I was able to do a GET request via OData and got the following results (https://prnt.sc/ylv178). When I tried to PATCH insert the image, I get a 405 Method not allowed (https://prnt.sc/ylvdor).

5. Is the SysImage table exposed via the Excel data import option? I could not find it being exposed. Am I missing something here?

Appreciate your help Oscar!

Oscar Dylan,

Hi Oscar, Can you help with the queries above? Thanks in advance.

M Shrikanth,

You can create a C# code schema with the following to add an image for an account or contact from a Url. This uses the ImageAPI found in the Terrasoft.Core.ImageAPI namespace.

var url = "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(url) 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, url, fileId);
 
			using (var dbExecutor = UserConnection.EnsureDBConnection())
			{
				new Update(UserConnection, entityType)
					.Set(imageField, Column.Parameter(fileId))
					.Where("Id").IsEqual(Column.Parameter(entityId))
					.Execute(dbExecutor);
			}
		}
	}
}

This could be exposed as your own web service to simplify adding images to records. 

Hope this helps.

Ryan

Ryan Farley,

Hi Ryan. Thanks much for the alternate approach. 

Ryan Farley,

userconnection not working
Show all comments