Simple connection example with the new Microsoft.Xrm.Tooling.Connector

So there is a new DLL that allows us to connect to CRM from 8.2 version forward (Dynamics 365).
To use this in the code you can use the EarlyBounds that you can generate using the SDK and then add it to your project. Afterwards you just need to insert the code and and references missing to your project:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;

namespace ConsoleApplication1
{
public static class Program
{
private static IOrganizationService _serviceProxy;

static void Main(string[] args)
{
try
{
string connectionString = GetServiceConfiguration();

CrmServiceClient conn = new CrmServiceClient(connectionString);
_serviceProxy = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

string fetchXml = @”<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’false’>
<entity name=’systemuser’>
<all-attributes />
<order attribute=’systemuserid’ descending=’false’ />
</entity>
</fetch>”;

EntityCollection result = _serviceProxy.RetrieveMultiple(new FetchExpression(fetchXml));

}
catch (Exception ex)
{

throw;
}
}

private static String GetServiceConfiguration()
{
// Get available connection strings from app.config.
int count = ConfigurationManager.ConnectionStrings.Count;

// Create a filter list of connection strings so that we have a list of valid
// connection strings for Microsoft Dynamics CRM only.
List<KeyValuePair<String, String>> filteredConnectionStrings =
new List<KeyValuePair<String, String>>();

for (int a = 0; a < count; a++)
{
if (isValidConnectionString(ConfigurationManager.ConnectionStrings[a].ConnectionString))
filteredConnectionStrings.Add
(new KeyValuePair<string, string>
(ConfigurationManager.ConnectionStrings[a].Name,
ConfigurationManager.ConnectionStrings[a].ConnectionString));
}

// No valid connections strings found. Write out and error message.
if (filteredConnectionStrings.Count == 0)
{
Console.WriteLine(“An app.config file containing at least one valid Microsoft Dynamics CRM ” +
“connection string configuration must exist in the run-time folder.”);
Console.WriteLine(“\nThere are several commented out example connection strings in ” +
“the provided app.config file. Uncomment one of them and modify the string according ” +
“to your Microsoft Dynamics CRM installation. Then re-run the sample.”);
return null;
}

// If one valid connection string is found, use that.
if (filteredConnectionStrings.Count == 1)
{
return filteredConnectionStrings[0].Value;
}

// If more than one valid connection string is found, let the user decide which to use.
if (filteredConnectionStrings.Count > 1)
{
Console.WriteLine(“The following connections are available:”);
Console.WriteLine(“————————————————“);

for (int i = 0; i < filteredConnectionStrings.Count; i++)
{
Console.Write(“\n({0}) {1}\t”,
i + 1, filteredConnectionStrings[i].Key);
}

Console.WriteLine();

Console.Write(“\nType the number of the connection to use (1-{0}) [{0}] : “,
filteredConnectionStrings.Count);
String input = Console.ReadLine();
int configNumber;
if (input == String.Empty) input = filteredConnectionStrings.Count.ToString();
if (!Int32.TryParse(input, out configNumber) || configNumber > count ||
configNumber == 0)
{
Console.WriteLine(“Option not valid.”);
return null;
}

return filteredConnectionStrings[configNumber – 1].Value;

}
return null;

}

private static Boolean isValidConnectionString(String connectionString)
{
// At a minimum, a connection string must contain one of these arguments.
if (connectionString.Contains(“Url=”) ||
connectionString.Contains(“Server=”) ||
connectionString.Contains(“ServiceUri=”))
return true;

return false;
}

app.config:

<configuration>
<connectionStrings>
<!– Online using Office 365 –>
<add name=”Server=CRM Online, organization=contoso, user=someone”
connectionString=”Url=https://<ORG>.crm4.dynamics.com; Username=<USERNAME>@<ORG>.onmicrosoft.com; Password=<PASSWORD>; authtype=Office365″/>

<!– On-premises with provided user credentials –>
<!– <add name=”Server=myserver, organization=AdventureWorksCycle, user=administrator”
connectionString=”Url=http://myserver/AdventureWorksCycle; Domain=mydomain; Username=administrator; Password=password; authtype=AD”/> –>

<!– On-premises using Windows integrated security –>
<!– <add name=”Server=myserver, organization=AdventureWorksCycle”
connectionString=”Url=http://myserver/AdventureWorksCycle; authtype=AD”/> –>

<!– On-Premises (IFD) with claims –>
<!–<add name=”Server=litware.com, organization=contoso, user=someone@litware.com”
connectionString=”Url=https://contoso.litware.com/contoso; Username=someone@litware.com; Password=password; authtype=IFD”/>–>
</connectionStrings>
<startup>
<supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.5.2″ />
</startup>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s