Exemplo n.º 1
0
        public async void SampleModelAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient xClient = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    Contact contact = new Contact(new ContactIdentifier("website", "email@address", ContactIdentifierType.Known));

                    xClient.AddContact(contact);

                    var flyerInfo = new FrequentFlyerInfo("First1")
                    {
                        Status = FrequentFlyerInfo.FrequentFlyerStatus.Silver
                    };

                    var facetReference = new FacetReference(contact, FrequentFlyerInfo.DefaultFacetKey);

                    xClient.SetFacet(facetReference, flyerInfo);

                    await xClient.SubmitAsync();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
Exemplo n.º 2
0
        public static async void setConsented(Sitecore.Analytics.Tracking.Contact contact, SiteContext context, Boolean consent)
        {
            if (contact != null && context != null)
            {
                Item           rootItem            = Sitecore.Context.Database.GetItem(context.RootPath);
                ReferenceField xconsentPolicyField = rootItem.Fields["XConsent Policy"];
                Item           xconsentPolicy      = xconsentPolicyField.TargetItem;

                if (rootItem != null && xconsentPolicy != null)
                {
                    using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
                    {
                        try
                        {
                            var            contactReference = new ContactReference(contact.ContactId);
                            Task <Contact> contactTask      = client.GetContactAsync(contactReference, new ContactExpandOptions()
                            {
                            });

                            Contact XContact = await contactTask;

                            var facet = XContact.GetFacet <ConsentInfo>(ConsentInfo.DefaultFacetKey);


                            if (facet != null)
                            {
                                if (facet.Consents != null)
                                {
                                    ConsentObject obj = facet.Consents.FirstOrDefault <ConsentObject>(x => x.rootId.Equals(rootItem.ID) &&
                                                                                                      x.language.Equals(context.Language) &&
                                                                                                      x.policyId.Equals(xconsentPolicy.ID) &&
                                                                                                      x.policyVersion.Equals(xconsentPolicy.Version.Number));

                                    if (obj != null)
                                    {
                                        obj.consent = consent;
                                    }
                                    else
                                    {
                                        facet.Consents.Add(new ConsentObject(rootItem.ID, xconsentPolicy.ID, xconsentPolicy.Version.Number, context.Language, consent));
                                    }

                                    // Set the updated facet
                                    client.SetFacet(XContact, ConsentInfo.DefaultFacetKey, facet);
                                }
                            }
                            else
                            {
                                facet = new ConsentInfo();
                                facet.Consents.Add(new ConsentObject(rootItem.ID, xconsentPolicy.ID, xconsentPolicy.Version.Number, context.Language, consent));
                            }

                            await client.SubmitAsync();
                        }
                        catch (XdbExecutionException ex)
                        {
                            // Handle exception
                        }
                    }
                }
            }
        }