1

I've an excel sheet with list of contacts that I've uploaded to Sitecore using xconnect through stand alone console app.

                var client = new HRDataImport.ConsoleApp.XConnnectClient();

                Console.WriteLine("Step 1: Retrieve Sitecore XConnect certificate and Url from App.config");
                xconnectCertificate = ConfigurationManager.AppSettings.Get("XCONNECT_CLIENT_CERTIFICATE_THUMBPRINT");
                xconnectUrl = ConfigurationManager.AppSettings.Get("XCONNECT_CLIENT_SERVICE_URL");
                Console.WriteLine("XConnect Url: " + xconnectUrl);
                Console.WriteLine("XConnect Certificate: " + xconnectCertificate);
                Console.WriteLine();
                // connect to xconnect and submit contact 
                Console.WriteLine("Initialize Sitecore XConnect using HRInformation Model");
                client.Initialize(xconnectCertificate, xconnectUrl).GetAwaiter().GetResult();
                Console.WriteLine("Get/Create Sitecore contact ");
                foreach (var emp in Employess)
                {

                  Console.WriteLine("Get contactwith id :"+ emp.EmpMail);
                  var contact = client.GetContact(emp.EmpMail).GetAwaiter().GetResult();

                if (contact != null)
                {
                    Console.WriteLine("CONTACT FOUND");
                    var personalInfo = contact.GetFacet<PersonalInformation>();

                    Console.WriteLine("personalInfo.FirstName: " + personalInfo.FirstName);
                    Console.WriteLine("personalInfo.LastName: " + personalInfo.LastName);
                    Console.WriteLine("personalInfo.JobTitle: " + personalInfo.JobTitle);

                    }
                else
                {
                    Console.WriteLine("NO CONTACT FOUND");
                    Console.WriteLine("----------------");
                    Console.WriteLine("Create contact, set information and submit");
                    client.SetPersonalInformation(emp.FirstName, emp.LastName, emp.JobTitle);                        
                    client.Submit(emp.EmpMail).GetAwaiter().GetResult();
                }
                Console.WriteLine();
                Console.WriteLine("-------Done Emp Id --"+ emp.EmpMail);
                }

        public async Task Submit(string identifier, string source = CONTACT_SOURCE_IDENTIFIER)
        {
            if (_cfg == null)
                throw new Exception("Initialize must be called first.");

            using (var client = new XConnectClient(_cfg))
            {
                // Identifier for a 'known' contact
                var contactIdentifier = new ContactIdentifier(source, identifier, ContactIdentifierType.Known);
                var identifiers = new ContactIdentifier[] { contactIdentifier };

                // Create a new contact object from the identifier
                _contact = new Contact(identifiers);
                //Add to the client
                client.AddContact(_contact);
                client.SetFacet(_contact, PersonalInformation.DefaultFacetKey, personalInfo);
                client.SetFacet(_contact, EmailAddressList.DefaultFacetKey, emails);                    
                //Submit changes
                await client.SubmitAsync();
            }

            _contact = null;
        }

And I've done Azure AD integration to let the users sign in using their windows username/password.

Azure AD Integration

What I'm trying to do, is that after the user logs in using Azure AD to Sitecore; I need to map the existing contact to the user that is being logging in.

For reference, I've used Habitat's accounts feature and this is what I've done so far, but I'm facing lots of issues regarding the tracker and it's "isActive" is always set to false.

If you have better solution please bring it on, I don't mind starting over with the right approach.

<owin.cookieAuthentication.signedIn>
    <processor type="Accounts.Infrastructure.Pipelines.SignedIn.TrackSignedIn, Feature.Accounts" resolve="true" />
</owin.cookieAuthentication.signedIn>

TrackSignedIn.cs

        public override void Process(SignedInArgs args)
        {
            var provider = this.GetProvider(args.Context.Identity);
            if (provider.Name == Sitecore.Owin.Authentication.Constants.LocalIdentityProvider)
            {
                return;
            }

            if (Tracker.Current == null)
            {
                Tracker.Initialize();
            }
          this.AccountTrackerService.TrackLoginAndIdentifyContact(provider.Name, args.User.Id);

        }

AccountTrackerService.cs

        public virtual void TrackLoginAndIdentifyContact(string source, string identifier)
        {
            this.trackerService.TrackGoal(LoginGoalId, source);
            this.trackerService.IdentifyContact(source, identifier);
        }

TrackerService.cs

    public void IdentifyContact(string source, string identifier)
    {
        if (!this.IsActive) /*Tracker.Current.IsActive is Always False!!*/
        {
            return;
        }

        Tracker.Current.Session.IdentifyAs(source, identifier);
    }

After solving is IsActive issue, I was going to change the identifier to match the one I used in the console app so it can map the existing contact to the logged in user.

Again this could be the wrong way, please let me know your thoughts either by solving the IsActive issue or by bringing up whole new solution.

Your Answer

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

Browse other questions tagged or ask your own question.