Activities


List of examples:

Logging activities for contacts




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

// 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

Logging activities for contacts (general API)




// 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, CMSHttpContext.Current.Request);


> Back to list of examples

Updating logged activities




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

if (contact != null)
{
    // Gets all activities logged for the contact
    var updateActivities = ActivityInfoProvider.GetActivities().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
        ActivityInfoProvider.SetActivityInfo(activity);
    }
}


> Back to list of examples

Deleting logged activities




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

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

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


> Back to list of examples