Kentico CMS 7.0 On-line Marketing Guide

Managing accounts

Managing accounts

Previous topic Next topic Mail us feedback on this topic!  

Managing accounts

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

The following example creates a new account.

 

private bool CreateAccount()

{

  // Create new account object

  AccountInfo newAccount = new AccountInfo()

       {

           AccountName = "My New Account",

           AccountSiteID = CMSContext.CurrentSiteID

       };

 

  // Save the account

  AccountInfoProvider.SetAccountInfo(newAccount);

 

  return true;

}

 

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

 

private bool GetAndUpdateAccount()

{

  // Get the account

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

 

  if (updateAccount != null)

   {

      // Update a property

       updateAccount.AccountName = updateAccount.AccountName.ToLower();

 

      // And save it

      AccountInfoProvider.SetAccountInfo(updateAccount);

 

      return true;

   }

 

  return false;

}

 

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

 

private bool GetAndBulkUpdateAccounts()

{

  // Get dataset of accounts

  string where = "AccountName LIKE N'My New Account%'";

  InfoDataSet<AccountInfo> accounts = AccountInfoProvider.GetAccounts(where, null);

 

  if (!DataHelper.DataSourceIsEmpty(accounts))

   {

      foreach (AccountInfo account in accounts)

       {

          // Update each one's property

           account.AccountName = account.AccountName.ToUpper();

 

          // And save it

          AccountInfoProvider.SetAccountInfo(account);

       }

 

      return true;

   }

 

  return false;

}

 

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

 

private bool AddAccountStatusToAccount()

{

  // Get the account

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

 

  // Get the account status

  AccountStatusInfo accountStatus = AccountStatusInfoProvider.GetAccountStatusInfo("MyNewStatus", CMSContext.CurrentSiteName);

 

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

   {

      // Check that account doesn't have this status

      if (account.AccountStatusID != accountStatus.AccountStatusID)

       {

          // Set new status

           account.AccountStatusID = accountStatus.AccountStatusID;

 

          // Save changes to the object

          AccountInfoProvider.SetAccountInfo(account);

 

          return true;

       }

   }

 

  return false;

}

 

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

 

private bool RemoveAccountStatusFromAccount()

{

  // Get the account

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

 

  // Get the account status

  AccountStatusInfo accountStatus = AccountStatusInfoProvider.GetAccountStatusInfo("MyNewStatus", CMSContext.CurrentSiteName);

 

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

   {

      // Check if account has this status set

      if (account.AccountStatusID == accountStatus.AccountStatusID)

       {

          // Remove the status from account

           account.AccountStatusID = 0;

 

          // Save the object

          AccountInfoProvider.SetAccountInfo(account);

 

          return true;

       }

   }

 

  return false;

}

 

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

 

private bool AddContactToAccount()

{

  // Get dataset of contacts

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

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

 

  // Get the account

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

 

  // Get the role

  ContactRoleInfo role = ContactRoleInfoProvider.GetContactRoleInfo("MyNewRole", CMSContext.CurrentSiteName);

 

  if (!DataHelper.DataSourceIsEmpty(contacts) && (account != null) && (role != null))

   {

      // Get the contact from dataset

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

 

      // Create new account - contact relationship

      AccountContactInfo accountContact = new AccountContactInfo()

           {

               AccountID = account.AccountID,

               ContactID = contact.ContactID,

               ContactRoleID = role.ContactRoleID

           };

 

      // And save it

      AccountContactInfoProvider.SetAccountContactInfo(accountContact);

 

      return true;

   }

 

  return false;

}

 

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

 

private bool RemoveContactFromAccount()

{

  // Get dataset of contacts

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

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

 

  // Get the account

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

 

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

   {

      // Get the contact from dataset

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

 

      // Find account - contact relationship

      AccountContactInfo accountContact = AccountContactInfoProvider.GetAccountContactInfo(account.AccountID, contact.ContactID);

 

      if (accountContact != null)

       {

          // Delete the object

          AccountContactInfoProvider.DeleteAccountContactInfo(accountContact);

 

          return true;

       }

   }

 

  return false;

}

 

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

 

private bool DeleteAccount()

{

  // Get the account

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

 

  if (deleteAccount != null)

   {

      // Delete the account

      AccountInfoProvider.DeleteAccountInfo(deleteAccount);

 

      return true;

   }

 

  return false;

}