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

Invalid User Authorization

When you get an error like this, when you try to open a Workflow that is in Draft mode, check first if you are using Custom Workflow Activities in that workflow and if the Custom Workflow is update with the most recent dll’s for the version of your Dynamics CRM instance.

I had a issue like that and updating the dll’s solved the problem.

Method can not be reflected / There was an error reflecting method

Today I received an update from a customer of a url in order to execute the methods of a project, just the address changed from http to https. In this project we use a class in C # generated by wsdl.exe. I generated again to the new url and added to the project.

So far so good, but when I run the project could not pass the instantiation of the proxy class that I generated and gave me the following error: “Method can not be reflected.”

After googling and seeing some posts, I have concluded that the types that were being passed in the parameters of each method were not valid with what was supposed to receive. For example, the parameter was of type System.DateTime, but received the method parameter of type string, so it would not work.

I went through the proxy class looking for anything that might have these types (which was not string) and replaced it for string.
In this case was simple.

Code exemple:

What I had when generated the proxy class:

[System.Xml.Serialization.XmlElementAttribute(System.DateTime)] out string timeParameter,

What I had to do:

[System.Xml.Serialization.XmlElementAttribute()] out string timeParameter,