From last tutorial, we created an Account with these information: name, accountnumber, address1_postalcode, address1_city, emailaddress1, address1_telephone1, description, telephone1. And the GUID for the Account is: 0b00108f-8757-e011-a444-e61f13c072b3.
In this tutorial, I will show how to retrieve an Account entity by using CrmService.Retrieve method of Microsoft Dynamics CRM Web Services.
If you lost our last tutorial, please read this post first: http://4rapiddev.com/csharp/create-account-with-microsoft-dynamics-crm-web-services-by-c/
The C# source code below will:
- Create a CRM Service instance
- Create a column set object and set the properties of the column set
- Retrieve the Account Entity by calling the Retrieve method
- Display the account information
Here you go – file retrieve_account.aspx in the source code attachment:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.Crm.Sdk; using Microsoft.Crm.Sdk.Query; using Microsoft.Crm.SdkTypeProxy; using CRM; public partial class retrieve_account : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string organization = "your-organization"; string server = "your-server:your-port-number-if-any"; string domain = "your-domain"; string username = "your-username"; string password = "your-password"; //Create CRM Service instance CrmService service = crm_demo.IFDConnectionDemo(organization, server, domain, username, password); //Create the column set object that indicates the properties to be retrieved. //And set the properties of the column set. ColumnSet cset = new ColumnSet(new string[] { "name", "accountnumber", "address1_postalcode", "address1_city", "emailaddress1", "address1_telephone1", "description", "telephone1" }); // accountGuid is the GUID of the record being retrieved. Guid accountGuid = new Guid("0b00108f-8757-e011-a444-e61f13c072b3"); //Retrieve the Account account account = (account)service.Retrieve(EntityName.account.ToString(), accountGuid, cset); //Display the Account Detail if (account.name != null) Response.Write("name: " + account.name.ToString() + "<br>"); if (account.accountnumber != null) Response.Write("accountnumber: " + account.accountnumber.ToString() + "<br>"); if (account.address1_postalcode != null) Response.Write("address1_postalcode: " + account.address1_postalcode.ToString() + "<br>"); if (account.address1_city != null) Response.Write("address1_city: " + account.address1_city.ToString() + "<br>"); if (account.emailaddress1 != null) Response.Write("emailaddress1: " + account.emailaddress1.ToString() + "<br>"); if (account.address1_telephone1 != null) Response.Write("address1_telephone1: " + account.address1_telephone1.ToString() + "<br>"); if (account.description != null) Response.Write("description: " + account.description.ToString() + "<br>"); if (account.telephone1 != null) Response.Write("telephone1: " + account.telephone1.ToString() + "<br>"); } } |
Note:
- You can use AllColumns to retrieve all informations instead of listing attributes one by one.
- When display/use these attributes, you should check to if it is not null, ex: if (account.name != null)
Download the source code above