---
title: Activities
---

> 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).

List of examples:

### Logging activities for contacts

```csharp

/* Note: The following approach allows you to log specific types of activities using specialized services:
IPagesActivityLogger, IMembershipActivityLogger, IEcommerceActivityLogger, INewsletterActivitityLogger,
IBlogsActivityLogger, IRatingActivityLogger */

// Gets the current user
UserInfo user = MembershipContext.AuthenticatedUser;

// Prepares an instance of the membership activity logging service
IMembershipActivityLogger membershipActivityLogger = CMS.Core.Service.Resolve<IMembershipActivityLogger>();

// Logs the "User registration" and "User login" activities for the specified user,
// based on the context of the current request
membershipActivityLogger.LogRegistration(user.UserName);
membershipActivityLogger.LogLogin(user.UserName);

```

[> Back to list of examples](#toc)

### Logging activities for contacts (general API)

```csharp

// Note: The following approach allows you to log any type of activity in general

// Prepares an instance of the general activity logging service
IActivityLogService service = CMS.Core.Service.Resolve<IActivityLogService>();

// Obtains parameters based on the current context in which the activity is being logged
UserInfo user = MembershipContext.AuthenticatedUser;
TreeNode currentPage = DocumentContext.CurrentDocument;
int contactId = ContactManagementContext.CurrentContactID;

// Prepares an initializer for logging activities of the "User login" type
var activityInitializer = new UserLoginActivityInitializer(user, currentPage, contactId);

// Logs the activity based on the context of the current request
service.Log(activityInitializer);

```

[> Back to list of examples](#toc)

### Updating logged activities

```csharp

// Gets the first contact whose last name is 'Smith'
ContactInfo contact = ContactInfo.Provider.Get()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .TopN(1)
                                    .FirstOrDefault();

if (contact != null)
{
    // Gets all activities logged for the contact
    var updateActivities = ActivityInfo.Provider.Get().WhereEquals("ActivityContactID", contact.ContactID);

    // Loops through individual activities
    foreach (ActivityInfo activity in updateActivities)
    {
        // Updates the activity title
        activity.ActivityTitle = activity.ActivityTitle.ToUpper();

        // Saves the modified activity to the database
        ActivityInfo.Provider.Set(activity);
    }
}

```

[> Back to list of examples](#toc)

### Deleting logged activities

```csharp

// Gets the first contact whose last name is 'Smith'
ContactInfo contact = ContactInfo.Provider.Get()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .TopN(1)
                                    .FirstOrDefault();

if (contact != null)
{
    // Gets all activities logged for the contact
    var activities = ActivityInfo.Provider.Get().WhereEquals("ActivityContactID", contact.ContactID);

    // Loops through individual activities
    foreach (ActivityInfo activity in activities)
    {
        // Deletes the activity
        ActivityInfo.Provider.Delete(activity);
    }
}

```

[> Back to list of examples](#toc)
