---
title: Managing on-line marketing activities (API)
related:
  - https://docs.kentico.com/k81/on-line-marketing-features/contact-management/tracking-contact-activities.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

> **Info:** **Kentico EMS required**
>
> Features described on this page require the **Kentico EMS** license.

```csharp title="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;

```

```csharp title="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;
}

```

```csharp title="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;
}

```

```csharp title="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;
}

```

```csharp title="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;
}

```
