Contacts


List of examples:

Contacts

Getting the current contact




// Gets the contact related to the currently processed request
ContactInfo currentContact = ContactManagementContext.CurrentContact;


> Back to list of examples

Creating a contact




// 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
ContactInfo.Provider.Set(newContact);


> Back to list of examples

Updating a contact




// Gets the first contact whose last name is 'Smith'
ContactInfo updateContact = ContactInfo.Provider.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
    ContactInfo.Provider.Set(updateContact);
}


> Back to list of examples

Updating multiple contacts




// Gets all contacts whose last name is 'Smith'
var contacts = ContactInfo.Provider.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
    ContactInfo.Provider.Set(contact);
}


> Back to list of examples

Linking contacts with user accounts




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

if (contact != null)
{
    // Links the contact with the current user
    Service.Resolve<IContactRelationAssigner>().Assign(
        MembershipContext.AuthenticatedUser.UserID,
        MemberTypeEnum.CmsUser,
        contact.ContactID);
}


> Back to list of examples

Removing user accounts from contacts




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

if (contact != null)
{
    // Gets the relationship between the contact and the current user
    ContactMembershipInfo contactMembership = ContactMembershipInfoProvider.GetMembershipInfo(
        contact.ContactID,
        MembershipContext.AuthenticatedUser.UserID,
        MemberTypeEnum.CmsUser);

    // Deletes the contact-user relationship
    ContactMembershipInfo.Provider.Delete(ContactMembershipInfo.Provider.Get(contactMembership.MembershipID));
}


> Back to list of examples

Deleting a contact




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

if (deleteContact != null)
{
    // Deletes the contact
    ContactInfo.Provider.Delete(deleteContact);
}


> Back to list of examples

Contact statuses

Creating a contact status




// Creates a new contact status object
ContactStatusInfo newStatus = new ContactStatusInfo()
{
    ContactStatusDisplayName = "New contact status",
    ContactStatusName = "NewContactStatus"
};

// Saves the contact status to the database
ContactStatusInfo.Provider.Set(newStatus);


> Back to list of examples

Updating a contact status




// Gets the contact status
ContactStatusInfo updateStatus = ContactStatusInfo.Provider.Get("NewContactStatus");

if (updateStatus != null)
{
    // Updates the contact status properties
    updateStatus.ContactStatusDisplayName = updateStatus.ContactStatusDisplayName.ToLowerCSafe();

    // Saves the modified contact status to the database
    ContactStatusInfo.Provider.Set(updateStatus);
}


> Back to list of examples

Updating multiple contact statuses




// Gets all contact statuses whose code name starts with 'New'
var statuses = ContactStatusInfo.Provider.Get()
                                                .WhereStartsWith("ContactStatusName","New");

// Loops through individual contact statuses
foreach (ContactStatusInfo contactStatus in statuses)
{
    // Updates the contact status properties
    contactStatus.ContactStatusDisplayName = contactStatus.ContactStatusDisplayName.ToUpper();

    // Saves the modified contact status to the database
    ContactStatusInfo.Provider.Set(contactStatus);
}


> Back to list of examples

Assigning a status to a contact




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

// Gets the contact status
ContactStatusInfo contactStatus = ContactStatusInfo.Provider.Get("NewContactStatus");

if ((contact != null) && (contactStatus != null))
{
    // Checks that the contact doesn't already have the given status
    if (contact.ContactStatusID != contactStatus.ContactStatusID)
    {
        // Assigns the status to the contact
        contact.ContactStatusID = contactStatus.ContactStatusID;

        // Saves the updated contact to the database
        ContactInfo.Provider.Set(contact);
    }
}


> Back to list of examples

Removing statuses from contacts




// Gets the contact status
ContactStatusInfo contactStatus = ContactStatusInfo.Provider.Get("NewContactStatus");

if (contactStatus != null)
{
    // Gets all contacts that have the specified status
    var contacts = ContactInfo.Provider.Get()
                                        .WhereEquals("ContactStatusID", contactStatus.ContactStatusID);

    // Loops through the contacts
    foreach (ContactInfo contact in contacts)
    {
        // Sets the status to 'None' for each contact
        contact.ContactStatusID = 0;

        // Saves the updated contact to the database
        ContactInfo.Provider.Set(contact);
    }
}


> Back to list of examples

Deleting a contact status




// Gets the contact status
ContactStatusInfo deleteStatus = ContactStatusInfo.Provider.Get("NewContactStatus");

if (deleteStatus != null)
{
    // Deletes the contact status
    ContactStatusInfo.Provider.Delete(deleteStatus);
}


> Back to list of examples