Logging activities through API

Brian Brown asked on October 20, 2021 15:10

I'm trying to create a new contact and logging activities to that contact. It's currently creating the contact correctly, however it's logging the activity to an anonymous contact. How can I assign the activity to the contact just created? Is it something with the last line of code? Thank you!

        ContactInfo currentContact = ContactManagementContext.CurrentContact;

        ContactInfo updateContact = ContactInfoProvider.GetContacts()
                                            .WhereEquals("ContactEmail", txtEmailAddress.Text)
                                            .TopN(1)
                                            .FirstOrDefault();

        if (updateContact != null)
        {
            // Updates the contact's properties
            updateContact.ContactFirstName = txtFirstName.Text;
            updateContact.ContactLastName = txtLastName.Text;

            // Saves the updated contact to the database
            ContactInfoProvider.SetContactInfo(updateContact);
        }
        else
        {
            // Creates a new contact object
            ContactInfo newContact = new ContactInfo()
            {
                ContactLastName = txtLastName.Text,
                ContactFirstName = txtFirstName.Text,
                ContactEmail = txtEmailAddress.Text,

                // Enables activity tracking for the new contact
                ContactMonitored = true
            };

            // Saves the contact to the database
            ContactInfoProvider.SetContactInfo(newContact);
        }

        // Gets an instance of the activity logging service
        var service = Service.Resolve<IActivityLogService>();

        // Prepares an initializer for logging the activity
        var activityInitializer = new Donation("Donation");

        // Logs the activity
        service.Log(activityInitializer, CMSHttpContext.Current.Request);

Recent Answers


Brian Brown answered on October 20, 2021 15:31

I think I got it, I pulled the current ID contact I and passed that in here. Sorry, still new to Kentico!

var activityInitializer = new Donation("Donation", contactID);

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.