Share via


Create an Account and Related Contacts

This sample creates an account and some related contacts.

Class Reference

Schema Reference

  • account.xsd
  • contact.xsd

Example

[C#]

public void CreateAccountAndRelatedContacts()
{
   // strServer should be set with the name of the platform Web server
   string strServer = "MyServerName";

   // strVirtualDirectory should be set with the name of the Microsoft CRM
   // virtual directory on the platform Web server
   string strVirtualDirectory = "mscrmservices";
   string strDir = "https://" + strServer + "/" + strVirtualDirectory + "/";

   // BizUser proxy object
   Microsoft.CRM.Proxy.BizUser oBizUser = new Microsoft.CRM.Proxy.BizUser ();
   oBizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
   oBizUser.Url = strDir + "BizUser.srf";

   // Account proxy object
   Microsoft.CRM.Proxy.CRMAccount oAccount = new Microsoft.CRM.Proxy.CRMAccount ();
   oAccount.Credentials = System.Net.CredentialCache.DefaultCredentials;
   oAccount.Url = strDir + "CRMAccount.srf";

   // Contact proxy object
   Microsoft.CRM.Proxy.CRMContact oContact = new Microsoft.CRM.Proxy.CRMContact ();
   oContact.Credentials = System.Net.CredentialCache.DefaultCredentials;
   oContact.Url = strDir + "CRMContact.srf";
   string strErrorMsg;
   try
   {
      Microsoft.CRM.Proxy.CUserAuth oUserAuth = oBizUser.WhoAmI();

      string strAccount = "<account><name>Account One</name>";
      strAccount += "<ownerid type=\"";
      strAccount += Microsoft.CRM.Flags.ObjectType.otSystemUser.ToString();
      strAccount += "\">" + oUserAuth.UserId;
      strAccount += "</ownerid></account>";

      string strContact = "<contact><firstname>Dave/firstname>";
      strContact += "<lastname>Natsuhara</lastname>";
      strContact += "<telephone2>206-555-0100</telephone2>";
      strContact += "<emailaddress1>dave@northwindtraders.com</emailaddress1>";
      strContact += "<ownerid type=\"";
      strContact += Microsoft.CRM.Flags.ObjectType.otSystemUser.ToString() +"\">";
      strContact += oUserAuth.UserId + "</ownerid></contact>";

      string strAccountId = oAccount.Create(oUserAuth, strAccount);
      string strContactId = oContact.Create(oUserAuth, strContact);

      // There are two relationships between Accounts and Contacts:
      // Hierarchical and Primary Contact

      // Create a hierarchy where the Account is the parent
      // of the Contact
      oAccount.AddSubContact(oUserAuth, strAccountId, strContactId);

      // Make the the Contact a primary contact of the Account
      oAccount.SetPrimaryContact(oUserAuth, strAccountId, strContactId);
   }
   catch(System.Web.Services.Protocols.SoapException err)
   {
      // Process the platform error here
      strErrorMsg = ("ErrorMessage: " + err.Message + " " + err.Detail.OuterXml + " Source: " + err.Source );
   }
   catch(Exception err)
   {
      // Process other errors here
      strErrorMsg = ("ErrorMessage: " + err.Message + "Source: " + err.Source );
   }
}

© 2003 Microsoft Corporation. All rights reserved.