When I am connecting to CRM through C# windows application, what am I supposed to do to see the number of records in the Contact entity. Please someone help me.
*This post is locked for comments
Hi Suchisuchi,
Where are you getting the system.nullreference exception?
It could be a problem with this code.
IOrganizationService _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
Hi Succisucci,
I would actually recommend against having the button in your code, but you can use it if you want. Instead I would use a Visual Studio Console Application template and have the code run cleanly from within that. That is my personal preference though, so do what works best for you.
For creating the test account you will need to place the code after your connection code. You can place this code in your existing method or call another method. The important thing is that the account updating code is called after the connection code.
I tried the following and am getting system.nullreference exception :(
string ConnectionString = ConfigurationManager.ConnectionStrings["Office365"].ConnectionString;
CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(ConnectionString);
IOrganizationService _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
MessageBox.Show("Connected");
Entity account = new Entity("account");
account["name"] = "Suchisuchi";
_accountId = _orgService.Create(account);
MessageBox.Show("Account Created");
Hi Adrian, once again thanks for the sample code. As I have a button event that helps me to connect to CRM as per the link that you shared and it had no errors and after debug also went well, here is the code:-
private void button1_Click(object sender, EventArgs e)
{
string ConnectionString = ConfigurationManager.ConnectionStrings["Office365"].ConnectionString;
CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(ConnectionString);
IOrganizationService _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
MessageBox.Show("Connected");
}
So in order to create account for testing purpose do I have to create another button event or anything as such or can I use it in existing event. Will you be able to please suggest.
Hi Suchisuchi,
Lets try a basic example. Add similar code into your console application to create an account record.
_service = (IOrganizationService)_serviceProxy; // Instaniate an account object. Entity account = new Entity("account"); // Set the required attributes. For account, only the name is required. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. account["name"] = "Suchisuchi"; // Create an account record named Suchisuchi. _accountId = _service.Create(account);
// Create a column set to define which attributes should be retrieved. ColumnSet attributes = new ColumnSet(new string[] { "name", "ownerid" }); // Retrieve the account and its name and ownerid attributes. account = _service.Retrieve(account.LogicalName, _accountId, attributes); _service.Update(account);
I tried the part II as I am using dynamics online/365 , am so obliged for the link. It will be of great help if I could be sure that it is connecting to instance if I could create an account or retrieve contact entity...
It has no errors, and debug also successful. But if I could create an account for testing then it could have been great. I used below code for account creation. but I tested nothing happened:-
Entity acc = new Entity("account");
acc["name"] = "Testing";
_orgService.Create(acc);
MessageBox.Show("Account Created");
It shows account created message, but when I go to instance I dint find account named as testing
Hi Suchisuchi,
Lets try this instead.
Aric Levin posted some great articles on connecting to Dynamics CRM from a Console Application. Follow his steps, but use your own credentials for the connection string. Once you have the connection working it will give you a great starting point for building the rest of your code.
It also asked me to remove IOrganizationService as unnecessary cast while I tried to debug, though I have mentioned private IOrganizationService _orgService;
Thanks Adrian, I tried this earlier, but i get so many errors. First error is -new Xrm.Tooling.Connector which asks me to add xrm namespace. After I do that it changes to new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString); and Microsoft.Xrm.Tooling.Connector greys out. So basically, I failed to find the link helpful atleast for me :(
Hi Suchisuchi,
Here is a link that teaches you how to create a simple connection to Dynamics 365. You will need to modify the code to suit your needs.
You should find this helpful, but if you have any questions let me know.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156