Managing on-line marketing activities (API)

Kentico EMS required

Features described on this page require the Kentico EMS license.

Using namespaces



using System;
using System.Data;

using CMS.OnlineMarketing;
using CMS.WebAnalytics;
using CMS.DataEngine;
using CMS.Helpers;
using CMS.SiteProvider;
using CMS.Base;


Logging activities



private bool CreateActivity()
{
    // Gets a dataset of contacts
    string where = "ContactLastName LIKE N'My New Contact%'";
    var contacts = ContactInfoProvider.GetContacts().Where(where).TopN(1);

    if (!DataHelper.DataSourceIsEmpty(contacts))
    {
        // Gets the contact from the dataset
        ContactInfo contact = contacts.First();

        // Gets the activity type
        ActivityTypeInfo activityType = ActivityTypeInfoProvider.GetActivityTypes().First();

        // Creates a new activity object
        ActivityInfo newActivity = new ActivityInfo()
                                        {
                                            ActivityType = activityType.ActivityTypeName,
                                            ActivityTitle = "My new activity",
                                            ActivitySiteID = SiteContext.CurrentSiteID,
                                            ActivityOriginalContactID = contact.ContactID,
                                            ActivityActiveContactID = contact.ContactID
                                        };
        // Saves the activity
        ActivityInfoProvider.SetActivityInfo(newActivity);

        return true;
    }

    return false;
}


Updating logged activities



private bool GetAndUpdateActivity()
{
    // Gets a dataset of contacts
    string where = "ContactLastName LIKE N'My New Contact%'";
    var contacts = ContactInfoProvider.GetContacts().Where(where).TopN(1);  

    if (!DataHelper.DataSourceIsEmpty(contacts))
    {
        // Gets the contact from the dataset
        ContactInfo contact = contacts.First<ContactInfo>();

        // Gets all activities associated with the contact
        var updateActivities = ActivityInfoProvider.GetActivities().WhereEquals("ActivityActiveContactID", contact.ContactID);        

        if (updateActivities.Any())
        {
            // Gets the first activity
            ActivityInfo activity = updateActivities.First();

            // Updates the activity
            activity.ActivityTitle = activity.ActivityTitle.ToLowerCSafe();

            // Saves the activity
            ActivityInfoProvider.SetActivityInfo(activity);

            return true;
        }
    }

    return false;
}


Updating multiple activities



private bool GetAndBulkUpdateActivities()
{
    // Gets a dataset of contacts
    string where = "ContactLastName LIKE N'My New Contact%'";
    var contacts = ContactInfoProvider.GetContacts().Where(where).TopN(1);  

    if (!DataHelper.DataSourceIsEmpty(contacts))
    {
        // Gets the contact from the dataset
        ContactInfo contact = contacts.First<ContactInfo>();

        // Gets all activities associated with the contact
        var updateActivities = ActivityInfoProvider.GetActivities().WhereEquals("ActivityActiveContactID", contact.ContactID);

        if (updateActivities.Any())
        {
            foreach (ActivityInfo activity in updateActivities)
            {
                // Updates the activity content
                activity.ActivityTitle = activity.ActivityTitle.ToUpper();

                // Saves the activity
                ActivityInfoProvider.SetActivityInfo(activity);
            }

            return true;
        }
    }

    return false;
}


Deleting logged activities



private bool DeleteActivity()
{
    // Gets a dataset of contacts
    string where = "ContactLastName LIKE N'My New Contact%'";
    var contacts = ContactInfoProvider.GetContacts().Where(where).TopN(1);

    if (!DataHelper.DataSourceIsEmpty(contacts))
    {
        // Gets the contact from the dataset
        ContactInfo contact = contacts.First<ContactInfo>();

        // Gets all activities associated with the contact
        var activities = ActivityInfoProvider.GetActivities().WhereEquals("ActivityOriginalContactID", contact.ContactID);    

        if (activities.Any())
        {
            foreach (ActivityInfo activity in activities)
            {
                // Deletes the contact's activities
                ActivityInfoProvider.DeleteActivityInfo(activity);
            }

            return true;
        }
    }

    return false;
}