Contacts
List of examples:
Dependency injection
Initialize required services
C#
// Initializes all services and provider classes used within
// the API examples on this page using dependency injection.
private readonly IInfoProvider<ContactInfo> contactInfoProvider;
private readonly IContactsBulkDeletionService contactDeletionService;
public ContactsServices(IInfoProvider<ContactInfo> contactInfoProvider, IContactsBulkDeletionService contactDeletionService)
{
this.contactInfoProvider = contactInfoProvider;
this.contactDeletionService = contactDeletionService;
}
Contacts
Get the current contact
C#
// Gets the contact related to the currently processed request
ContactInfo currentContact = ContactManagementContext.CurrentContact;
Create contacts
C#
// Creates a new contact object
ContactInfo newContact = new ContactInfo()
{
ContactLastName = "Smith",
ContactFirstName = "John",
// Enables activity tracking for the new contact
ContactMonitored = true
};
// Saves the contact to the database
contactInfoProvider.Set(newContact);
Update contacts
C#
// Gets the first contact whose last name is 'Smith'
ContactInfo updateContact = contactInfoProvider.Get()
.WhereEquals("ContactLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (updateContact != null)
{
// Updates the contact's properties
updateContact.ContactCompanyName = "Company Inc.";
// Saves the updated contact to the database
contactInfoProvider.Set(updateContact);
}
Update multiple contacts
C#
// Gets all contacts whose last name is 'Smith'
var contacts = contactInfoProvider.Get()
.WhereEquals("ContactLastName", "Smith");
// Loops through individual contacts
foreach (ContactInfo contact in contacts)
{
// Updates the properties of the contact
contact.ContactCompanyName = "Company Inc.";
// Saves the updated contact to the database
contactInfoProvider.Set(contact);
}
Delete contacts
C#
// Gets the first contact whose last name is 'Smith'
ContactInfo deleteContact = contactInfoProvider.Get()
.WhereEquals("ContactLastName", "Smith")
.TopN(1)
.FirstOrDefault();
if (deleteContact != null)
{
// Deletes the contact
contactInfoProvider.Delete(deleteContact);
}
Delete contacts in bulk
C#
var dateOneWeekAgo = DateTime.Now.AddDays(-7);
// WhereCondition that selects contacts that are at least 7 days old and have an empty email address
var condition = new WhereCondition()
.WhereLessThan(nameof(ContactInfo.ContactCreated), dateOneWeekAgo)
.And()
.WhereEmpty(nameof(ContactInfo.ContactEmail));
// Deletes contacts matching the specified condition
// Doesn't specify a batch size, which means that contacts are deleted in batches of 1000 (automatically repeats)
await contactDeletionService.BulkDelete(condition);