Kentico CMS 6.0 On-line Marketing Guide

Managing contact groups

Managing contact groups

Previous topic Next topic Mail us feedback on this topic!  

Managing contact groups

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

The following example creates a new contact group.

 

private bool CreateContactGroup()

{

  // Create new contact group object

  ContactGroupInfo newGroup = new ContactGroupInfo()

       {

           ContactGroupDisplayName = "My new group",

           ContactGroupName = "MyNewGroup",

           ContactGroupSiteID = CMSContext.CurrentSiteID,

           ContactGroupDynamicCondition = "{%Contact.ContactLastName.Contains(\"My new\")%}"

       };

 

  // Save the contact group to database

  ContactGroupInfoProvider.SetContactGroupInfo(newGroup);

 

  return true;

}

 

The following example gets and updates the contact group created by the example above.

 

private bool GetAndUpdateContactGroup()

{

  // Get the contact group

  ContactGroupInfo updateGroup = ContactGroupInfoProvider.GetContactGroupInfo("MyNewGroup", CMSContext.CurrentSiteName);

  if (updateGroup != null)

   {

      // Update contact group's properties

       updateGroup.ContactGroupDisplayName = updateGroup.ContactGroupDisplayName.ToLower();

 

      // Save the contact group

      ContactGroupInfoProvider.SetContactGroupInfo(updateGroup);

 

      return true;

   }

 

  return false;

}

 

The following example gets and bulk updates multiple contact groups specified by a WHERE condition.

 

private bool GetAndBulkUpdateContactGroups()

{

  // Get the contact groups

  string where = "ContactGroupName LIKE N'MyNewGroup%'";

  InfoDataSet<ContactGroupInfo> groups = ContactGroupInfoProvider.GetContactGroups(where, null);

 

  if (!DataHelper.DataSourceIsEmpty(groups))

   {

      foreach (ContactGroupInfo group in groups)

       {

          // Update a property

           group.ContactGroupDisplayName = group.ContactGroupDisplayName.ToUpper();

 

          // Save the contact group

          ContactGroupInfoProvider.SetContactGroupInfo(group);

       }

 

      return true;

   }

 

  return false;

}

 

The following example adds a contact to the contact group created by the first example in this topic.

 

private bool AddContactToGroup()

{

  // Get dataset of contacts

  string where = "ContactLastName LIKE N'My New Contact%'";

  InfoDataSet<ContactInfo> contacts = ContactInfoProvider.GetContacts(where, null, 1, null);

 

  // Get the contact group

  ContactGroupInfo group = ContactGroupInfoProvider.GetContactGroupInfo("MyNewGroup", CMSContext.CurrentSiteName);

 

  if (!DataHelper.DataSourceIsEmpty(contacts) && (group != null))

   {

      // Get the contact from dataset

      ContactInfo contact = contacts.First<ContactInfo>();

 

      // Create the contact - contactgroup relationship

      ContactGroupMemberInfo newContactGroupMember = new ContactGroupMemberInfo()

           {

               ContactGroupMemberContactGroupID = group.ContactGroupID,

               ContactGroupMemberType = ContactGroupMemberTypeEnum.Contact,

               ContactGroupMemberRelatedID = contact.ContactID,

               ContactGroupMemberFromManual = true

           };

 

      // Save the contact group

      ContactGroupMemberInfoProvider.SetContactGroupMemberInfo(newContactGroupMember);

 

      return true;

   }

 

  return false;

}

 

The following example removes the contact added by the the previous example from the group.

 

private bool RemoveContactFromGroup()

{

  // Get dataset of contacts

  string where = "ContactLastName LIKE N'My New Contact%'";

  InfoDataSet<ContactInfo> contacts = ContactInfoProvider.GetContacts(where, null, 1, null);

 

  // Get the contact group

  ContactGroupInfo group = ContactGroupInfoProvider.GetContactGroupInfo("MyNewGroup", CMSContext.CurrentSiteName);

 

  if (!DataHelper.DataSourceIsEmpty(contacts) && (group != null))

   {

      // Get the contact from dataset

      ContactInfo contact = contacts.First<ContactInfo>();

 

      // Get the contact - contactgroup relationship

      ContactGroupMemberInfo deleteContactGroupMember = ContactGroupMemberInfoProvider.GetContactGroupMemberInfoByData(group.ContactGroupID, contact.ContactID, ContactGroupMemberTypeEnum.Contact);

 

      if (deleteContactGroupMember != null)

       {

          // Delete the info

          ContactGroupMemberInfoProvider.DeleteContactGroupMemberInfo(deleteContactGroupMember);

 

          return true;

       }

   }

 

  return false;

}

 

The following example adds an account to the contact group created by the first example in this topic.

 

private bool AddAccountToGroup()

{

  // Get the account

  AccountInfo account = AccountInfoProvider.GetAccountInfo("My New Account", CMSContext.CurrentSiteName);

 

  // Get the contact group

  ContactGroupInfo group = ContactGroupInfoProvider.GetContactGroupInfo("MyNewGroup", CMSContext.CurrentSiteName);

 

  if ((account != null) && (group != null))

   {

      // Create new account - contact group relationship

      ContactGroupMemberInfo newContactGroupMember = new ContactGroupMemberInfo()

           {

               ContactGroupMemberContactGroupID = group.ContactGroupID,

               ContactGroupMemberType = ContactGroupMemberTypeEnum.Account,

               ContactGroupMemberRelatedID = account.AccountID

           };

 

      // Save the object

      ContactGroupMemberInfoProvider.SetContactGroupMemberInfo(newContactGroupMember);

 

      return true;

   }

 

  return false;

}

 

The following example removes the account added by the the previous example from the group.

 

private bool RemoveAccountFromGroup()

{

  // Get the account

  AccountInfo account = AccountInfoProvider.GetAccountInfo("My New Account", CMSContext.CurrentSiteName);

 

  // Get the contact group

  ContactGroupInfo group = ContactGroupInfoProvider.GetContactGroupInfo("MyNewGroup", CMSContext.CurrentSiteName);

 

  if ((account != null) && (group != null))

   {

      // Get the account - contactgroup relationship

      ContactGroupMemberInfo deleteContactGroupMember = ContactGroupMemberInfoProvider.GetContactGroupMemberInfoByData(group.ContactGroupID, account.AccountID, ContactGroupMemberTypeEnum.Account);

 

      if (deleteContactGroupMember != null)

       {

          // Delete the info

          ContactGroupMemberInfoProvider.DeleteContactGroupMemberInfo(deleteContactGroupMember);

 

          return true;

       }

   }

 

  return false;

}

 

The following example deletes the contact group created by the first example in this topic.

 

private bool DeleteContactGroup()

{

  // Get the contact group

  ContactGroupInfo deleteGroup = ContactGroupInfoProvider.GetContactGroupInfo("MyNewGroup", CMSContext.CurrentSiteName);

 

  if (deleteGroup != null)

   {

      // Delete the contact group

      ContactGroupInfoProvider.DeleteContactGroupInfo(deleteGroup);

 

      return true;

   }

 

  return false;

}