Last active
July 17, 2019 14:58
-
-
Save dnstommy/65a439899f6f85c4f5d436b6f9fc9f8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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