Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

See records of contact entity

Posted on by Microsoft Employee

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

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: See records of contact entity

    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;


  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: See records of contact entity

    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: See records of contact entity

    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");

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: See records of contact entity

    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.

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: See records of contact entity

    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);







  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: See records of contact entity

    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

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: See records of contact entity

    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: See records of contact entity

    It also asked me to remove IOrganizationService as unnecessary cast while I tried to debug, though I have mentioned private IOrganizationService _orgService;

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: See records of contact entity

    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 :(

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: See records of contact entity

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans