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;
public ContactsServices(IInfoProvider<ContactInfo> contactInfoProvider)
{
this.contactInfoProvider = contactInfoProvider;
}
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);
}