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
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"?>
<add name="SQLServerConnection" connectionString="Server=.;Database=events.website;Trusted_Connection=True;"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
By Now you will have your dataset in “result”; here is another piece of code to read the data:
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)
I hope this helps!
Following checklist should be completed in order to completely remove the all the dependency before exporting the solution and importing to your target environment.
- Make sure all relationships are removed ( i.e navigation from left hand side on form editor)
- Any Lookup field from that custom entity is removed
- 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:
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!
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.
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");
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!