Activities
List of examples:
- Logging activities for contacts
- Logging activities for contacts (general API)
- Updating logged activities
- Deleting logged activities
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 */
// 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);
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);
Updating logged activities
// 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);
}
}
Deleting logged activities
// 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);
}
}