Create early bound proxy classes
Download the latest version of CRM SDK, open the "bin" folder and you will see an executable file with the name of "crmsvcutil.exe", we use it to generate our proxy classes to make the coding against CRM a lot easier.Open a command window and navigate to the bin folder, modify the following with the name of you CRM organization and authentication.
CrmSvcUtil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration" /out:Xrm.cs /url:http://YourCRMUrl/YourOrg/XRMServices/2011/Organization.svc /domain:YourCRMDomain /username:administrator /password:pass /namespace:Xrm /serviceContextName:XrmServiceContext
This will generate a file called Xrm.cs under the same folder which we will use it later on.
Create Visual Studio Console App Project
Open visual studio and create a new console app projectUsing "Add Existing Item", add Xrm.cs file that you have generated earlier to your project.
Add the following references to your project:
From the
SDK\bin
folder:- AntiXSSLibrary.dll
- Microsoft.Crm.Sdk.Proxy.dll
- Microsoft.Xrm.Client.dll
- Microsoft.Xrm.Sdk.dll
- System.Data.Services.dll
- System.Data.Services.Client.dll
- System.Runtime.Serialization.dll
Setup Configuration
We now need to add configurations to generate service context. Right click on the project name and click on Add new item. Select "Application Configuration File"Leave the name as it is and this will add App.config file to your project. Now add the following to the configuration file:
replace the above with the proper CRM information. The above will define the contaxt and name ot as Xrm context. this means you can easily generate service context by calling:
var xrm = new XrmServiceContext("Xrm");
We are now ready to retirve list of existing users in our CRM.
Use context to retirve users from CRM
click on prgram.cs file and update it as following:
static void Main(string[] args) { // generate service context var xrm = new XrmServiceContext("Xrm"); // Display users will return a stringbuilder of all the users Console.WriteLine(DisplayUsers(xrm)); Console.WriteLine("Press any key to exit."); Console.ReadKey(); } ////// Displays the users. /// /// The XRM service context ///list of users in CRM as stringbuilder public static StringBuilder DisplayUsers(XrmServiceContext xrm) { var str = new StringBuilder(); // This will give us a list of users var users = xrm.SystemUserSet.Where(c => c.FullName != null); // for each user, append the first name to the stringbuilder foreach (var user in users) { str.Append(string.Format("User Name: {0} {1} ", user.Salutation, user.FullName) + Environment.NewLine ); } return str; }
More Advanced Code
here is a code to return all the contacts and the tasks attached to each contact, notice the helper method to retirve the task status
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Metadata; using Xrm; namespace ConsoleApplication8 { class Program { static void Main(string[] args) { // generate service context var xrm = new XrmServiceContext("Xrm"); // Display users will return a stringbuilder of all the users Console.WriteLine(DisplayUsers(xrm)); Console.WriteLine("Press any key to exit."); Console.ReadKey(); } public static StringBuilder DisplayUsers(XrmServiceContext xrm) { var str = new StringBuilder(); var contacts = xrm.ContactSet.Where(c => c.FullName != null); foreach (var contact in contacts) { str.Append(string.Format("{0} {1} {2} From {3} - {4}", contact.Salutation, contact.FirstName, contact.LastName, contact.Address1_Country, contact.Address1_City) + Environment.NewLine ); var tasks = xrm.ActivityPointerSet.Where(t => t.RegardingObjectId.Id == contact.Id); foreach (var task in tasks) { str.Append("--------------- Tasks -------------------" + Environment.NewLine + string.Format("{0} - {1}", task.Subject, GetStateCodeValues(task.StatusCode.Value, xrm)) + Environment.NewLine); } } return str; } private static string GetStateCodeValues(int statusCodeValue, XrmServiceContext xrm) { var attributeRequest = new RetrieveAttributeRequest { EntityLogicalName = "task", LogicalName = "statuscode", RetrieveAsIfPublished = true }; var attributeResponse = (RetrieveAttributeResponse)xrm.Execute(attributeRequest); var attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata; var statusAttrMetadata = (StatusAttributeMetadata)attrMetadata; string statusCodeLabel = ""; // For every status code value within all of our status codes values // (all of the values in the drop down list) foreach (StatusOptionMetadata statusMeta in statusAttrMetadata.OptionSet.Options) { // Check to see if our current value matches if (statusMeta.Value == statusCodeValue) { // If our numeric value matches, set the string to our status code // label statusCodeLabel = statusMeta.Label.UserLocalizedLabel.Label; } } return statusCodeLabel; } } }
I followed your link but details related to app config file is missing after this sentence
ReplyDeleteLeave the name as it is and this will add App.config file to your project. Now add the following to the configuration file:
Can you please provide what we need to add in app config file.
Thanks,
Mihir
This comment has been removed by the author.
DeleteLooks something like:
Delete<?xml version="1.0"?>
<configuration>
<configSections>
<section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client" />
</configSections>
<connectionStrings>
<add connectionString="Server=http://Hostname/OrganizationName; Domain=MyDomain; Username=Administrator; Password=Pa55w0rd" name="Xrm" />
</connectionStrings>
<microsoft.xrm.client>
<contexts default="Xrm">
<add connectionStringName="Xrm" name="Xrm" type="Xrm.XrmServiceContext, Xrm" />
</contexts>
</microsoft.xrm.client>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
"Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.php jobs in hyderabad.
ReplyDelete"