/// <summary>
        /// Given an existing identifier, find and delete the Contact
        /// </summary>
        /// <param name="cfg">The configuration to use to load the Contact</param>
        /// <param name="twitterId">The identifier for the contact</param>
        public virtual async Task <Contact> DeleteContact(XConnectClientConfiguration cfg, string twitterId)
        {
            Logger.WriteLine("Deleting Contact with Identifier:" + twitterId);

            //Get the existing contact that we want to delete
            var contactLoader = new GetContactTutorial()
            {
                Logger = this.Logger
            };
            var existingContact = await contactLoader.GetContact(cfg, twitterId);

            if (existingContact != null)
            {
                using (var client = new XConnectClient(cfg))
                {
                    try
                    {
                        //Add the delete operation onto the client for the specified contact and execute
                        client.DeleteContact(existingContact);
                        await client.SubmitAsync();

                        Logger.WriteLine(">> Contact successfully deleted.");
                    }
                    catch (XdbExecutionException ex)
                    {
                        Logger.WriteError("Exception deleting the Contact", ex);
                    }
                }
            }
            else
            {
                Logger.WriteLine("WARNING: No Contact found with Identifier:" + twitterId + ". Cannot delete Contact.");
            }

            return(existingContact);
        }
Example #2
0
        /// <summary>
        /// This collection of single-contact tutorials shows how to use the API working with one Contact record.
        /// TUTORIALS INCLUDED:
        /// 1. Create and retrieve a Contact
        /// 2. Update an existing Contact
        /// 3. Register a goal (create an interaction) for an existing Contact
        /// 4. Access the Reference Data Manager and also create an entry
        /// 5. Retrieve a Contact with a full list of all its interactions
        /// 6. Search the interactions index for interactions in a specific date range
        /// 7. Take an interaction search result and expand it to get all the details
        /// 8. Delete a single existing contact (the one created in the first tutorial)
        /// </summary>
        /// <param name="cfg">The configuration used to open connections to xConnect</param>
        public static async Task SingleContactTutorials(XConnectClientConfiguration cfg)
        {
            //Initialize required handlers
            var configuration      = new Configuration();
            var outputHandler      = new OutputHandler();
            var interactionManager = new InteractionManager()
            {
                Logger = outputHandler
            };
            var referenceDataManager = new ReferenceDataManager()
            {
                Logger = outputHandler
            };

            /**
             * TUTORIAL: Create and retrieve a contact
             */
            //Create a contact
            var twitterId      = configuration.TwitterIdentifier + Guid.NewGuid().ToString("N");
            var contactCreator = new CreateContactTutorial()
            {
                Logger = outputHandler
            };
            var identifier = await contactCreator.CreateContact(cfg, twitterId);

            //Retrieve a contact that was created
            var contactLoader = new GetContactTutorial()
            {
                Logger = outputHandler
            };
            var contact = await contactLoader.GetContact(cfg, twitterId);

            /**
             * TUTORIAL: Update an existing contact
             */
            //Update the personal information about a Contact
            PersonalInformation updatedPersonalInformation = new PersonalInformation()
            {
                JobTitle = "Senior Programmer Writer"
            };
            var contactUpdater = new UpdateContactTutorial()
            {
                Logger = outputHandler
            };
            var updatedContact = await contactUpdater.UpdateContact(cfg, twitterId, updatedPersonalInformation);

            /**
             * TUTORIAL: Register a goal for the created contact
             */
            //Initialize IP information which will be used for tracking events.
            var ipInfo = new IpInfo("127.0.0.1")
            {
                BusinessName = "Home"
            };
            var interaction = await interactionManager.RegisterGoalInteraction(cfg, contact, configuration.OtherEventChannelId, configuration.InstantDemoGoalId, ipInfo);

            /**
             * TUTORIAL: Reference Data Manager
             */
            var definition = await referenceDataManager.GetDefinition(configuration.GoalTypeName, configuration.InstantDemoGoalId, configuration.XConnectUrl, configuration.Thumbprint);

            if (definition == null)
            {
                definition = await referenceDataManager.CreateDefinition(configuration.GoalTypeName, configuration.InstantDemoGoalId, configuration.InstantDemoGoalName, configuration.XConnectUrl, configuration.Thumbprint);
            }


            /**
             * TUTORIAL: Get a contact with its list of interactions
             */
            //Get a contact with the interactions
            contact = await contactLoader.GetContactWithInteractions(cfg, twitterId, DateTime.MinValue, DateTime.MaxValue);

            /**
             * TUTORIAL: Search Interactions
             */
            //Find all interactions created in a specific date range. Note that dates are required in UTC or local time
            var startDate    = new DateTime(configuration.SearchYear, configuration.SearchMonth, configuration.SearchStartDay).ToUniversalTime();
            var endDate      = startDate.AddDays(configuration.SearchDays);
            var interactions = await interactionManager.SearchInteractionsByDate(cfg, startDate, endDate);


            /**
             * TUTORIAL: Expand a single interaction search results
             */
            //Look for the first result in the search results
            var interactionResult = interactions != null?interactions.LastOrDefault() : null;

            //If the search result has sufficient contact and interaction ID data present, request the expanded details about the interaction
            if (interactionResult != null && interactionResult.Contact != null && interactionResult.Contact.Id.HasValue && interactionResult.Id.HasValue)
            {
                var triggeredGoal = await interactionManager.GetInteraction(cfg, interactionResult.Contact.Id.Value, interactionResult.Id.Value);
            }

            /**
             * TUTORIAL: Delete a single Contact from the database
             */
            var contactDeleter = new DeleteContactTutorial()
            {
                Logger = outputHandler
            };
            var deletedContact = await contactDeleter.DeleteContact(cfg, twitterId);
        }
Example #3
0
        /// <summary>
        /// Given an existing identifier and new personal information, find the contact and update the facet
        /// </summary>
        /// <param name="cfg">The configuration to use to load the Contact</param>
        /// <param name="twitterId">The identifier for the contact</param>
        /// <param name="updatedPersonalInformation">The new information to store</param>
        /// <returns></returns>
        public virtual async Task <Contact> UpdateContact(XConnectClientConfiguration cfg, string twitterId, PersonalInformation updatedPersonalInformation)
        {
            Logger.WriteLine("Updating personal information about Contact with Identifier:" + twitterId);

            //If no updated information was provided, we can shortcut exit and save the connections
            if (updatedPersonalInformation == null)
            {
                return(null);
            }

            //Get the existing contact that we want to update
            var contactLoader = new GetContactTutorial()
            {
                Logger = this.Logger
            };
            var existingContact = await contactLoader.GetContact(cfg, twitterId);

            if (existingContact != null)
            {
                //Get the existing personal information that needs to be updated
                var personalInfoFacet = existingContact.GetFacet <PersonalInformation>(PersonalInformation.DefaultFacetKey);
                if (personalInfoFacet != null)
                {
                    //Check for any changes. No need to send updates if it is the same!
                    bool hasFacetChanged = personalInfoFacet.HasChanged(updatedPersonalInformation);

                    //If any change has occurred, make the update.
                    if (hasFacetChanged)
                    {
                        //Update the current facet data with the new pieces
                        personalInfoFacet.Update(updatedPersonalInformation);

                        //Open a client connection and make the update
                        using (var client = new XConnectClient(cfg))
                        {
                            try
                            {
                                client.SetFacet <PersonalInformation>(existingContact, PersonalInformation.DefaultFacetKey, personalInfoFacet);
                            }
                            catch (XdbExecutionException ex)
                            {
                                Logger.WriteError("Exception updating personal information", ex);
                            }
                        }
                    }
                }
                else
                {
                    //If there is no personal information facet, we need to send all the data
                    using (var client = new XConnectClient(cfg))
                    {
                        try
                        {
                            client.SetFacet <PersonalInformation>(existingContact, PersonalInformation.DefaultFacetKey, updatedPersonalInformation);
                        }
                        catch (XdbExecutionException ex)
                        {
                            Logger.WriteError("Exception creating personal information for the Contact", ex);
                        }
                    }

                    //Update the current facet data with the new pieces
                    personalInfoFacet = updatedPersonalInformation;
                }

                //Output information about the updated contact
                Logger.WriteLine("Updated contact information:");
                Logger.WriteContact(existingContact);
            }
            else
            {
                Logger.WriteLine("WARNING: No Contact found with Identifier:" + twitterId + ". Cannot update personal information.");
            }

            return(existingContact);
        }