Month: July 2018

SQL Connection to CRM Database

Dynamics CRM has very efficient web services using which you can retrieve data such as Organization service Or WebApi. However if you still need to retrieve data from database here is a quick code helper – which i am using in a console application.

//Initiate the connection
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString))
{
//create a command variable
using (var command = connection.CreateCommand())
{
//add query to your command
command.CommandText = "your SQLquery goes here";

// open the connection first and then execute the query using Execute Reader
connection.Open();
result = command.ExecuteReader();

here is the app.config which i have used , i am using the windows authentication hence you donot need to mention credentials.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="SQLServerConnection" connectionString="Server=.;Database=events.website;Trusted_Connection=True;"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
</configuration>

By Now you will have your dataset in “result”; here is another piece of code to read the data:

 if (result.HasRows)
{
 while (result.Read())
{
string contactid= result["columnName"].ToString();
string contact = result["columnName"].ToString();
DateTime createdonDate= (DateTime)result["createdon"];
string type = result["columnName"].ToString();
}

}
// close the result
 if (result != null)
result.Close();


 

I hope this helps!

Cheers!

System Form Dependency To A Custom Entity – CheckList

Following checklist should be completed in order to completely remove the all the dependency before exporting the solution and importing to your target environment.

  1. Make sure all relationships are removed ( i.e navigation from left hand side on form editor)
  2. Any Lookup field from that custom entity is removed
  3. The most important and neglected one and for which I find people scratching their heads is when this lookup is removed from the form and even entity is deleted. Still  Get dependency error something like below:dependency.PNG
    Resolution:  The lookup field was being referenced on another lookup which was acting as a “Dependent Lookup”. hence this entity was showing as system form dependency.  So i went to other lookup field —> changed properties and unchecked the option “Show related records”.You can also try to remove that field –> Publish —> add again —-publish.

Hope this helps!

 

Connect to Dynamics CRM 365 Online from Excel [Reporting]

I have seen a few blogs on how to connect to dynamics crm but most of them talks about connect with Odata endpoint. However with dynamics 365, Odata has  been deprecated hence you cannot use them and will get error.

Below is the step by step process to connect to dynamics crm 365 online from Excel.

  1. If you do not have power query option in your excel or its an old version. Please download the excel addin from here : https://www.microsoft.com/en-us/download/details.aspx?id=39379   and run the setup chose 32 or 64bit as per your excel.
  2. Once done, Launch Excel and select “Power Query” Tab –>”From Online Services” Option  and select “From Dynamics 365(Online) from the dropdown.
    1.png
  3. In the next tab add Web Api endpoint of your online CRM ( you can find it from Customizations–>Developer Resources–>Copy “Instance Web API” url and paste it below:
    2
  4. In the next authentication screen – select “Organizational Account” and then click on “Sign In”.
    3
  5. Following step 4 will take you to your organization sign page. Enter username and password and click Sign In.
    4.PNG
  6. If Signed In successfully; you will be landed back to the authentication page. Click “connect”.5.PNG
  7. Post this you will be presented with the connected org details( such as entities etc).
    6
    7.PNG
    I hope this helps.

How to connect to Dynamics CRM Onpremises AD/IFD from a Windows Form Application

Being a CRM developer, you often need to develop some external application. In this blog i will talk about and provide the code to connect to dynamics CRM Onpremises in a windows form application.

Login

public static ConnectToCrm(string username, string password, string domain, string Url)
{

ClientCredentials _clientCreds = new ClientCredentials();
_clientCreds.Windows.ClientCredential.UserName = username;
_clientCreds.Windows.ClientCredential.Password = password;
_clientCreds.Windows.ClientCredential.Domain = domain;

if (username == "" || password == "" || domain == "" || Url == "")
{
MessageBox.Show("Please Enter All Details To Login!");
}

_service = (IOrganizationService)new OrganizationServiceProxy(new Uri(Url), null, _clientCreds, null);

OrganizationServiceContext _orgContext = new OrganizationServiceContext(_service);
Guid orgId = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).OrganizationId;

if (orgId != null)
{
MessageBox.Show("Successfully Connected to CRM. Click OK");
}
else if(orgId==null)
{
MessageBox.Show("Could Not Connect to CRM. Click OK & Try Again!");
}
}

The _service can be utilized in your methods to retrieve the data and perform actions in dynamics CRM. i.e, below:

var fetchExpression = new FetchExpression(fetchXml);
EntityCollection fetchResult = _service.RetrieveMultiple(fetchExpression);

hope this helps!

cheers!