Kentico CMS 6.0 On-line Marketing Guide

Managing contacts

Managing contacts

Previous topic Next topic Mail us feedback on this topic!  

Managing contacts

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

The following example creates a new contact.

 

private bool CreateContact()

{

  // Create new contact object

  ContactInfo newContact = new ContactInfo()

       {

           ContactLastName = "My New Contact",

           ContactFirstName = "My New Firstname",

           ContactSiteID = CMSContext.CurrentSiteID,

           ContactIsAnonymous = true

       };

 

  // Save the contact

  ContactInfoProvider.SetContactInfo(newContact);

 

  return true;

}

 

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

 

private bool GetAndUpdateContact()

{

  // Get dataset of contacts

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

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

 

  if (!DataHelper.DataSourceIsEmpty(contacts))

   {

      // Get the contact from dataset

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

 

      // Update a property

       contact.ContactLastName = contact.ContactLastName.ToLower();

 

      // Save the changes

      ContactInfoProvider.SetContactInfo(contact);

 

      return true;

   }

 

  return false;

}

 

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

 

private bool GetAndBulkUpdateContacts()

{

  // Get dataset of contacts

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

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

 

  if (!DataHelper.DataSourceIsEmpty(contacts))

   {

      foreach (ContactInfo contact in contacts)

       {

          // Update a property of each contact

           contact.ContactLastName = contact.ContactLastName.ToUpper();

 

          // And save them

          ContactInfoProvider.SetContactInfo(contact);

       }

 

      return true;

   }

 

  return false;

}

 

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

 

private bool AddContactStatusToContact()

{

  // Get dataset of contacts

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

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

 

  // Get the contact status

  ContactStatusInfo contactStatus = ContactStatusInfoProvider.GetContactStatusInfo("MyNewStatus", CMSContext.CurrentSiteName);

 

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

   {

      // Get the contact from dataset

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

 

      // If relationship doesn't already exist

      if (contact.ContactStatusID != contactStatus.ContactStatusID)

       {

          // Add contact status to contact

           contact.ContactStatusID = contactStatus.ContactStatusID;

 

          // Save the changes

          ContactInfoProvider.SetContactInfo(contact);

 

          return true;

       }

   }

 

  return false;

}

 

The following example removes the contact status added by the example above from the contact.

 

private bool RemoveContactStatusFromContact()

{

  // Get dataset of contacts

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

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

 

  // Get the contact status

  ContactStatusInfo contactStatus = ContactStatusInfoProvider.GetContactStatusInfo("MyNewStatus", CMSContext.CurrentSiteName);

 

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

   {

      // Get the contact from dataset

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

 

      // If relationship exists

      if (contact.ContactStatusID == contactStatus.ContactStatusID)

       {

          // Remove the status

           contact.ContactStatusID = 0;

 

          // Save the changes

          ContactInfoProvider.SetContactInfo(contact);

 

          return true;

       }

   }

 

  return false;

}

 

The following example adds a membership (user) to the contact created by the first example in this topic.

 

private bool AddMembership()

{

  // Get dataset of contacts

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

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

 

  if (!DataHelper.DataSourceIsEmpty(contacts))

   {

      // Get the contact from dataset

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

 

      // Set relationship to user

       CMS.OnlineMarketing.MembershipInfoProvider.SetRelationship(

          CMSContext.CurrentUser.UserID,

          MemberTypeEnum.CmsUser,

           contact.ContactID,

           contact.ContactID,

          false);

 

      return true;

   }

 

  return false;

}

 

The following example removes the membership (user) added by the previous example from the contact.

 

private bool RemoveMembership()

{

  // Get dataset of contacts

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

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

 

  if (!DataHelper.DataSourceIsEmpty(contacts))

   {

      // Get the contact from dataset

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

 

      // Get the membership

       CMS.OnlineMarketing.MembershipInfo membership = CMS.OnlineMarketing.MembershipInfoProvider.GetMembershipInfo(contact.ContactID, contact.ContactID, CMSContext.CurrentUser.UserID, MemberTypeEnum.CmsUser);

 

      // Delete the membership

       CMS.OnlineMarketing.MembershipInfoProvider.DeleteRelationship(membership.MembershipID);

 

      return (membership != null);

   }

 

  return false;

}

 

The following example adds an IP address to the contact created by the first example in this topic.

 

private bool AddIPAddress()

{

  // Get dataset of contacts

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

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

 

  if (!DataHelper.DataSourceIsEmpty(contacts))

   {

      // Get the contact from dataset

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

 

      // Create new IP address

      IPInfo newIP = new IPInfo()

           {

               IPAddress = "127.0.0.1",

               IPOriginalContactID = contact.ContactID,

               IPActiveContactID = contact.ContactID

           };

 

      // Save the IP info

      IPInfoProvider.SetIPInfo(newIP);

 

      return true;

   }

 

  return false;

}

 

The following example removes the IP address added by the previous example from the contact.

 

private bool RemoveIPAddress()

{

  // Get dataset of contacts

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

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

 

  if (!DataHelper.DataSourceIsEmpty(contacts))

   {

      // Get the contact from dataset

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

 

      // Get contact's IP

       where = String.Format("IPOriginalContactID = '{0}' AND IPAddress = '{1}'", contact.ContactID, "127.0.0.1");

      InfoDataSet<IPInfo> deleteIPs = IPInfoProvider.GetIps(where, null, 1, "IPID");

 

      if (!DataHelper.DataSourceIsEmpty(deleteIPs))

       {

          // Delete IP

          IPInfoProvider.DeleteIPInfo(deleteIPs.First<IPInfo>());

 

          return true;

       }

   }

 

  return false;

}

 

The following example adds user agent information to the contact created by the first example in this topic.

 

private bool AddUserAgent()

{

  // Get dataset of contacts

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

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

 

  if (!DataHelper.DataSourceIsEmpty(contacts))

   {

      // Get the contact from dataset

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

 

      // Create new agent info

      UserAgentInfo agentInfo = new UserAgentInfo()

           {

               UserAgentActiveContactID = contact.ContactID,

               UserAgentOriginalContactID = contact.ContactID,

               UserAgentString = "My User Agent"

           };

 

      // Save the agent info

      UserAgentInfoProvider.SetUserAgentInfo(agentInfo);

 

      return true;

   }

 

  return false;

}

 

The following example removes the user agent information added by the previous example from the contact.

 

private bool RemoveUserAgent()

{

  // Get dataset of contacts

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

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

 

  if (!DataHelper.DataSourceIsEmpty(contacts))

   {

      // Get the contact from dataset

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

 

      // Get the user agent info

       where = String.Format("UserAgentOriginalContactID = '{0}' AND UserAgentString = '{1}'", contact.ContactID, "My User Agent");

      InfoDataSet<UserAgentInfo> deleteAgents = UserAgentInfoProvider.GetUserAgents(where, null, 1, null);

 

      if (!DataHelper.DataSourceIsEmpty(deleteAgents))

       {

          // Delete the user agent info

          UserAgentInfoProvider.DeleteUserAgentInfo(deleteAgents.First<UserAgentInfo>());

 

          return true;

       }

   }

 

  return false;

}

 

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

 

private bool DeleteContact()

{

  // Get dataset of contacts

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

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

 

  if (!DataHelper.DataSourceIsEmpty(contacts))

   {

      // Get the contact from dataset

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

 

      // Delete the contact

      ContactInfoProvider.DeleteContactInfo(contact);

 

      return true;

   }

 

  return false;

}