0

I'm using Sitecore 9.1.1 and I am creating a Contact programmatically and set its facets.

My problem is when I set the Avatar facet which is the picture of the contact.

The following code is where I set the Avatar facet and I get the Image from media library:

private void SetContactAvatar(XConnectClient client, Contact contact, Item account)
    {
        MultilistField mlField = new MultilistField(account.Fields["photos"]);
        Item[] mlFieldItems = mlField.GetItems();
        if (mlFieldItems.Count() != 0)
        {
            MediaItem mediaItem = mlFieldItems[0];
            if (mediaItem != null)
            {
                string profileImageUrl = Sitecore.Globals.ServerUrl + Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem);
                System.Net.WebClient objWebClient = new System.Net.WebClient();
                byte[] profileImageBytes = objWebClient.DownloadData(profileImageUrl);

                //Stream stream = mediaItem.GetMediaStream();
                //Byte[] profileImageBytes = new Byte[stream.Length];

                client.SetFacet(contact, Avatar.DefaultFacetKey, new Avatar(mediaItem.MimeType, profileImageBytes)
                {
                    MimeType = mediaItem.MimeType,
                    Picture = profileImageBytes
                });
            }
        }
    }

When the contact is created I get a broken Image with the following error:

{"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException","StackTrace":"   at Sitecore.Cintel.Endpoint.Plumbing.NegotiateLanguageFilter.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
   at System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.ActionFilterAttribute.d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.ActionFilterAttribute.d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Web.Http.Filters.ActionFilterAttribute.d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.ActionFilterAttribute.d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Web.Http.Filters.ActionFilterAttribute.d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ActionFilterResult.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.AuthorizationFilterAttribute.d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.AuthorizationFilterAttribute.d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()"}

Can someone advise me, please?

1

This error is not related to contact facets. You can see how to resolve this error here Sitecore 9 Experience Profile An Error has occurred.

How to set contact avatar from media item:

This is my working code snippet:

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;
            }

            client.SetFacet(existingContact, Avatar.DefaultFacetKey, new Avatar(mediaItem.MimeType, ReadBytes(mediaItem.GetMediaStream())));
            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();
    }
}

enter image description here

  • I would only change one thing. If the Avatar exists, you need to update it, not add a new one. If you try and add a new facet model, it would be missing the ConcurrencyToken and throw "Facet already exists" error. Here is an update of your code. gist.github.com/dnstommy/65a439899f6f85c4f5d436b6f9fc9f8f – Chris Auer Jul 17 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.