Skip to content

Instantly share code, notes, and snippets.

@dnstommy
Last active July 17, 2019 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnstommy/65a439899f6f85c4f5d436b6f9fc9f8f to your computer and use it in GitHub Desktop.
Save dnstommy/65a439899f6f85c4f5d436b6f9fc9f8f to your computer and use it in GitHub Desktop.
public async Task<bool> SetContactAvatar(string source, string identifier, MediaItem mediaItem)
{
using (XConnectClient client = SitecoreXConnectClientConfiguration.GetClient())
{
try
{
var reference = new IdentifiedContactReference(source, identifier);
var contact = client.GetAsync(
reference,
new ContactExpandOptions(CollectionModel.FacetKeys.Avatar)
);
Contact existingContact = await contact;
if (existingContact == null)
{
return false;
}
var avatar = existingContact.GetFacet<Avatar>(Avatar.DefaultFacetKey) ?? new Avatar();
avatar.MimeType = mediaItem.MimeType;
avatar.Picture = ReadBytes(mediaItem.GetMediaStream());
client.SetFacet(existingContact, Avatar.DefaultFacetKey, avatar);
await client.SubmitAsync();
return true;
}
catch
{
return false;
}
}
}
public static byte[] ReadBytes(Stream input)
{
using (var ms = new MemoryStream())
{
input.CopyTo(ms);
return ms.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment