Contact Profile Image Using Avatar Facet in Sitecore

Sitecore xConnect provides several default facets like personal information, AddressList, EmailAddressList, etc. Avatar is one of them and it comes under the IContactPicture Legacy facet.

If you are new with Sitecore xDB and xConnect and need more insight on the Custom facet, How to create and set/update the facet then you can refer blog post Work with Custom Facet in Sitecore and Create Custom Facet in Sitecore.

In this article, I am going to describe how to set the contact picture in the Avatar facet which we can see in the details tab in the experience profile window.

 private bool SetAvatarFacets(byte[] imageBytes, string mimeType, Contact contact, IXdbContext client)  
     {  
       var pictureData = imageBytes;  
       var pictureMimeType = mimeType;  
       var avatar = contact.GetFacet<Avatar>(Avatar.DefaultFacetKey);  
       //Facet is null  
       if (avatar == null)  
       {  
         avatar = new Avatar(pictureMimeType, pictureData);  
       }  
       //Facet has same picture data and mime type  
       else if (avatar.Picture == pictureData && avatar.MimeType == mimeType)  
       {  
         return false;  
       }  
       else  
       {  
         avatar.MimeType = pictureMimeType;  
         avatar.Picture = pictureData;  
       }  
       client.SetFacet(contact, Avatar.DefaultFacetKey, avatar);  
       return true;  
     }  

Mime Type can be image/jpeg, image/jpeg, etc.

If you have an Image URL then use the below code to convert the Image URL into the bytes:

 var webClient = new System.Net.WebClient();  
 byte[] webClient.DownloadData(imageURL);  
Sitecore

Happy Sitecoreing ðŸ˜Š

Comments

Popular posts from this blog

Sitecore Installation Error: Failed to Start Service 'Sitecore Marketing Automation Engine'

Import CSV Data in Sitecore Using PowerShell: Part-3

Sitecore : The database principal owns a schema in the database, and cannot be dropped message