示例#1
0
        public JsonResult GetContactHubspotData(string contactId)
        {
            using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    Sitecore.XConnect.Contact contact = client.Get <Sitecore.XConnect.Contact>(new ContactReference(new Guid(contactId)),
                                                                                               new ContactExpandOptions(ScoreFacet.DefaultFacetKey, ScoreNameFacet.DefaultFacetKey)
                    {
                    });

                    if (contact != null)
                    {
                        // For each contact, retrieve the facet - will return null if contact does not have this facet set
                        var facetValue = contact.GetFacet <ScoreFacet>(ScoreFacet.DefaultFacetKey);
                        var facetName  = contact.GetFacet <ScoreNameFacet>(ScoreNameFacet.DefaultFacetKey);

                        if (facetValue != null && facetValue != null)
                        {
                            return(Json(new Tuple <string, int>(facetName.HubspotScoreName, facetValue.HubspotScore), JsonRequestBehavior.AllowGet));
                        }
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Handle exceptions
                }
            }
            return(null);
        }
        public bool UpdateContactPersonal(Contact contact, string firstName, string lastName)
        {
            using (var client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    // Retrieve contact
                    if (contact == null)
                    {
                        return(false);
                    }

                    // Retrieve facet (or create one)
                    var facet = contact.GetFacet <PersonalInformation>(PersonalInformation.DefaultFacetKey) ??
                                new PersonalInformation();

                    // Change facet properties
                    facet.FirstName = firstName;
                    facet.LastName  = lastName;

                    // Set the updated facet
                    client.SetFacet(contact, PersonalInformation.DefaultFacetKey, facet);
                    client.Submit();
                    return(true);
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exception
                    Sitecore.Diagnostics.Log.Error(
                        $"[FaceLogin] Error saving contact.",
                        ex, ex.GetType());
                    return(false);
                }
            }
        }
        public bool UpdateContactConsent(Contact contact, bool consent)
        {
            using (var client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    // Retrieve contact
                    if (contact == null)
                    {
                        return(false);
                    }

                    // Retrieve facet (or create one)
                    var facet = contact.GetFacet <AdhereToFaceLoginFacet>(AdhereToFaceLoginFacet.DefaultFacetKey) ??
                                new AdhereToFaceLoginFacet();

                    // Change facet properties
                    facet.FaceRecognitionAllowed = consent;

                    // Set the updated facet
                    client.SetFacet(contact, AdhereToFaceLoginFacet.DefaultFacetKey, facet);
                    client.Submit();
                    return(true);
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exception
                    Sitecore.Diagnostics.Log.Error(
                        $"[Bookshelf] Error updating avatar.",
                        ex, ex.GetType());
                    return(false);
                }
            }
        }
        /// <summary>
        /// Set emotion facet to contact
        /// </summary>
        /// <param name="contact">Contact to be updated</param>
        /// <param name="facet">Facet to be applied to Contact</param>
        public void SetEmotionFacet(Contact contact, EmotionFacet facet)
        {
            using (var client = SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var emotions = contact.GetFacet <EmotionFacet>(EmotionFacet.DefaultFacetKey);
                    if (emotions == null)
                    {
                        client.SetFacet(contact, EmotionFacet.DefaultFacetKey, facet);
                    }
                    else
                    {
                        UpdateFacetValues(facet, emotions);
                        client.SetFacet(contact, EmotionFacet.DefaultFacetKey, emotions);
                    }

                    client.Submit();
                }
                catch (XdbExecutionException ex)
                {
                    Log.Error($"Could not set facet for {contact.Id} contact", ex, this);
                }
            }
        }
        public bool UpdateContactAvatar(Contact contact, string avatar)
        {
            using (var client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    // Retrieve contact
                    if (contact == null)
                    {
                        return(false);
                    }

                    // Retrieve facet (or create one)
                    var facet = contact.GetFacet <Avatar>(Avatar.DefaultFacetKey);

                    // Change facet properties
                    var byteArray = _imageService.Base64ToByteArray(avatar);
                    if (facet == null)
                    {
                        facet = new Avatar("image/jpeg", byteArray);
                    }
                    else
                    {
                        facet.MimeType = "image/jpeg";
                        facet.Picture  = byteArray;
                    }

                    // Set the updated facet
                    client.SetAvatar(contact, facet);
                    client.Submit();
                    return(true);
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exception
                    Sitecore.Diagnostics.Log.Error(
                        $"[FaceLogin] Error updating avatar.",
                        ex, ex.GetType());
                    return(false);
                }
            }
        }
 public T GetFacet <T>(Sitecore.XConnect.Contact contact, string FacetKey) where T : Facet
 {
     using (XConnectClient client = SitecoreXConnectClientConfiguration.GetClient())
     {
         try
         {
             return(contact.GetFacet <T>(FacetKey));
         }
         catch (XdbExecutionException ex)
         {
             Log.Error($"XConnectContactRepository: Error reading facet '{FacetKey}' from contact '{contact.Id}'", ex, this);
             return(null);
         }
         catch (Exception ex)
         {
             Log.Error("XConnectContactRepository: Error communication with the xConnect colllection service", ex, this);
             return(null);
         }
     }
 }
        public Avatar GetContactAvatar(Contact contact = null)
        {
            using (var client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    if (contact == null)
                    {
                        var contactId = Sitecore.Analytics.Tracker.Current != null &&
                                        Sitecore.Analytics.Tracker.Current.Contact != null?
                                        Sitecore.Analytics.Tracker.Current.Contact.ContactId.ToString("N") : "";

                        if (!string.IsNullOrEmpty(contactId))
                        {
                            // Get from current contact
                            var trackerIdentifier = new IdentifiedContactReference(
                                Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource,
                                contactId);
                            contact = client.Get <Contact>(trackerIdentifier,
                                                           new ContactExpandOptions(Avatar.DefaultFacetKey));
                        }
                    }

                    var avatar = contact?.GetFacet <Avatar>(Avatar.DefaultFacetKey);
                    return(avatar);
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exception
                    Sitecore.Diagnostics.Log.Error(
                        $"[FaceLogin] Error Getting Contact Avatar",
                        ex, ex.GetType());
                }
            }
            return(null);
        }