SBX - Search With Button

SBX - Forum Post Title

See records of contact entity

Microsoft Dynamics CRM Forum

Suchisuchi asked a question on 14 Jun 2018 1:47 AM

Question Status

Verified

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.

Reply
David Jennaway responded on 14 Jun 2018 5:47 AM
My Badges
Verified Answer

A simple and performant way to do this is with an aggregate Fetch query - see https://msdn.microsoft.com/en-us/library/gg309565.aspx  for examples

Reply
Suchisuchi responded on 14 Jun 2018 7:33 AM

I am getting an error :-

system.typeinitializationexception' occurred in the .exe

Can anyone please help

Reply
Suchisuchi responded on 14 Jun 2018 8:20 AM

I am getting the error while i am trying to connect to CRM, here is my code:

IOrganizationService _service = null;
try
{

ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = "n.com";
credentials.UserName.Password = "XX";
Uri serviceUri = new Uri("https://***************.svc");
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
MessageBox.Show("Connection established");
proxy.EnableProxyTypes();
_service = (IOrganizationService)proxy;
MessageBox.Show("connect2 to CRM");
return _service;

}

catch (Exception ex)
{
//return _service;

throw ex;
}

Kindly help me

Reply
CRM Develope responded on 15 Jun 2018 12:35 AM
My Badges

Hi,

In addition to that you can also follow:

stackoverflow.com/.../system-typeinitializationexception-occurred

Reply

Hi ,

Refer below article to connect dynamics 365 using console-

goutamdascrm.wordpress.com/.../connecting-dynamics-365-online-using-console-application-c

Reply
Nitin Meria responded on 15 Jun 2018 1:17 AM
My Badges
Verified Answer

Hi Suchisuchi,

Your error itself states the problem. System.TypeInitializationException occurs when you are using a specific type without actually initiating it.

In your code,

You have Initialized the IOrganizationService type to null :

IOrganizationService _service = null;

and then directly used it at :

_service = (IOrganizationService)proxy;

Try putting

IOrganizationService _service;

instead of

IOrganizationService _service = null;

Reply
Suchisuchi responded on 15 Jun 2018 1:48 AM

Thanks so much for your reply. When I use IOrganizationService _service; 

then I am getting error as _service does not exist in current context

now my code is :

IOrganizationService _service;
try
{

ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = "a.com";
credentials.UserName.Password = "XX";
Uri serviceUri = new Uri("https://****************************.svc");
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
proxy.EnableProxyTypes();
_service = (IOrganizationService)proxy;
MessageBox.Show("Connection established");

I am also trying to call this function from another class's buttonclick event. Just nothing is happening . Am in soup, please help me someone.

Reply
Nitin Meria responded on 15 Jun 2018 1:58 AM
My Badges
Suggested Answer

Hi Suchisuchi,

You can use Xrm.Tooling which is an easy way of connecting to Dynamics CRM Service from external applications.

Below is a link for the same :

msdn.microsoft.com/.../jj602970.aspx

Please note : you can establish connection to CRM only using two lines below which are :

// Connect to the CRM web service using a connection string.

               CrmServiceClient conn = new Xrm.Tooling.Connector.CrmServiceClient(connectionString);

               // Cast the proxy client to the IOrganizationService interface.

               _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

Reply
Suchisuchi responded on 15 Jun 2018 2:02 AM

It got resolved, thanks. Now I have a new problem just in case you can help me. I have 2 classes one is abc.cs where the connection to crm is mentioned. I have another form.cs class with a button click event. If I click the button abc.cs should trigger and connect me to CRM. Here is my code:

public void btnConnectToCRM_Click(object sender, EventArgs e)

       {

           IOrganizationService _service = null;

           abc ch = new abc();

             _service = ch.ConnectToMSCRM();

          MessageBox.Show("Connected");

           ch.ConnectToMSCRM(_service);

       }

In another class I have 

public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
{
IOrganizationService _service;

try
{

ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = "n.com";
credentials.UserName.Password = "XX";
Uri serviceUri = new Uri("https://***************.svc");
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
MessageBox.Show("Connection established");
proxy.EnableProxyTypes();
_service = (IOrganizationService)proxy;
MessageBox.Show("connect2 to CRM");
return _service;

}

catch (Exception ex)
{


throw ex;
}

Reply
Nitin Meria responded on 15 Jun 2018 4:36 AM
My Badges

So what is the problem?

Reply
Suchisuchi responded on 15 Jun 2018 5:06 AM

Problem is : when I debug the form.cs file on btnConnectToCRM_Click ; it pops up message connected, but I dont think it is really connecting to the CRM. And so I was thinking am I missing on anything... If you could [;ease see the code and tell me as you did last time please.

Reply
Nitin Meria responded on 15 Jun 2018 5:58 AM
My Badges

Its difficult to say by looking at what you have pasted. What makes you feel the code is not connecting to CRM?

Also, have to tried using the _service object to Create, Update, Delete,etc?

Reply
Suchisuchi responded on 15 Jun 2018 7:30 AM

Thanks for trying to help me and keeping patience.

I tried to create account but it is thowing error: object reference not set to an instance of an object

My code is :

private void btnConnectToMSCRM_Click(object sender, EventArgs e)
{
IOrganizationService _service = null;
try
{
ABC ch = new ABC();
_service = ch.ConnectToMSCRM();
ch.ConnectToMSCRM(_service);
MessageBox.Show("Connected");
Entity acc = new Entity("account");
acc["name"] = "Suchis Account";
_accountID = _service.Create(acc);
MessageBox.Show("Account is created");
}
catch (Exception ex)
{
throw ex;
}
}

Is it possible to tell me why am i getting this error?

Reply
Suchisuchi responded on 15 Jun 2018 9:48 AM

Hi, I am getting either System.TypeInitializationException error or Nullreference error for below code:-

private void btnConnectToMSCRM_Click(object sender, EventArgs e)

       {

           IOrganizationService _service ;

           try

           {

               CRMHelper ch = new CRMHelper();

               _service = ch.ConnectToMSCRM();

               ch.ConnectToMSCRM(_service);

               MessageBox.Show("Connected");

               Entity acc = new Entity("account");

               acc["name"] = "S Account";

               _accountID = _service.Create(acc);

               MessageBox.Show("Account is created");

           }

           catch (Exception ex)

           {

               throw ex;

           }

Will you be able to help me please

Reply
Suchisuchi responded on 17 Jun 2018 2:02 AM

I am getting error "Type of namespace could not be found for CrmServiceClient , IOrganizationService And for connectionString - the name does not exist in current contect. I have app.config file where I have mentioned connectionstring.

Reply
David Jennaway responded on 14 Jun 2018 5:47 AM
My Badges
Verified Answer

A simple and performant way to do this is with an aggregate Fetch query - see https://msdn.microsoft.com/en-us/library/gg309565.aspx  for examples

Reply
Nitin Meria responded on 15 Jun 2018 1:17 AM
My Badges
Verified Answer

Hi Suchisuchi,

Your error itself states the problem. System.TypeInitializationException occurs when you are using a specific type without actually initiating it.

In your code,

You have Initialized the IOrganizationService type to null :

IOrganizationService _service = null;

and then directly used it at :

_service = (IOrganizationService)proxy;

Try putting

IOrganizationService _service;

instead of

IOrganizationService _service = null;

Reply
Adrian Begovich responded on 17 Jun 2018 2:55 AM
Verified Answer

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.

Reply
Adrian Begovich responded on 17 Jun 2018 3:55 AM
Verified Answer

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.

Reply
Nitin Meria responded on 15 Jun 2018 1:58 AM
My Badges
Suggested Answer

Hi Suchisuchi,

You can use Xrm.Tooling which is an easy way of connecting to Dynamics CRM Service from external applications.

Below is a link for the same :

msdn.microsoft.com/.../jj602970.aspx

Please note : you can establish connection to CRM only using two lines below which are :

// Connect to the CRM web service using a connection string.

               CrmServiceClient conn = new Xrm.Tooling.Connector.CrmServiceClient(connectionString);

               // Cast the proxy client to the IOrganizationService interface.

               _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

Reply
Adrian Begovich responded on 17 Jun 2018 2:12 AM
Suggested Answer

Hi Suchisuchi,

Have you included Microsoft.Xrm.Tooling.Connector, and Microsoft.Xrm.Sdk references in your project? It sounds like these might be missing.

Reply
Adrian Begovich responded on 17 Jun 2018 2:17 AM
Suggested Answer

Hi Suchisuchi,

One way to see the number of records on the Contact entity is by building a console application with a FetchXml query and paging to count the number of retrieved records. However, there are other easier ways to get record counts such as the View Record Counter Plugin found in XRMToolbox.

Reply
Adrian Begovich responded on 17 Jun 2018 2:29 AM
Suggested Answer

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.

Reply
Adrian Begovich responded on 17 Jun 2018 3:33 AM
Suggested Answer

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







Reply
Adrian Begovich responded on 17 Jun 2018 4:01 AM
Suggested Answer

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;


Reply

SBX - Two Col Forum

SBX - Migrated JS