Sample Code for using IOrganizationService in CRM 2011

Posted: November 3, 2010 in CRM, CRM 2011, Microsoft Dynamics CRM
Tags: ,

Just a sample code for quick reference !

Uri organizationUri = new Uri("http://crmservername/orgname/XRMServices/2011/Organization.svc");
Uri homeRealmUri = null;
ClientCredentials credentials = new ClientCredentials();
// set default credentials for OrganizationService
credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
// or
credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
IOrganizationService _service = (IOrganizationService)orgProxy;
try
{
    Entity myAccount = new Entity("account");
    myAccount["name"] = "Test Account";
    _service.Create(myAccount);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
}

In case of early-bound, don’t forget to add the following line of code

 OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
 // This statement is required to enable early-bound type support.
  _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
 

For Office 365 check this post

http://nishantrana.wordpress.com/2012/12/06/sample-code-to-connect-office-365-users-to-crm-2011-online/

Bye.


																
Comments
  1. I have an issue with IFD CRM. Am trying to connect to crm with network credentials. but it throwing as “The NetworkCredentials provided were unable to create a Kerberos credential, see inner exception for details.” and am able to connect to crm with username and password.

    Please help me….

  2. snehadeore says:

    hello Sir,

    I am using CRM 2011 and new to it.

    I have an webservice(object) which requires parameter ,the parameters will be the Enities properties which is created in CRM 2011.

    like if I have created Enitiy ‘order’ which has fields like pickup date,weight,total amount

    and these are parameters which will be required for webservice for Courier.

    how would I fetch data from CRM 2011 and pass to webservice .

    I known plug-in are used for that, but how to write? and how to access the database?

    please some one will explain me how to achieve this task?

    all sort of Information are welcomed.

    Thanks

  3. Sultan says:

    Hello,

    How to avoid of using

    new System.Net.NetworkCredential(“admin”,“pwd “,“domain”);

    but use WindowsIdentity or pass NTLM authentication details?

    • Nishant Rana says:

      Try this Sultan

      ClientCredentials credentials = new ClientCredentials();

      // set default credentials for OrganizationService

      credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;

      // or

      credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;

      Let me know it worked or not.

      • Sultan says:

        What are the reasons for the failure of ‘…/XRMServices/2011/Organization.svc’?
        I try to test but the CRM says but 20 minutes ago I was ok

        An error has occurred.

        Try this action again. If the problem continues, check the Microsoft Dynamics CRM Community for solutions or contact your organization’s Microsoft Dynamics CRM Administrator. Finally, you can contact Microsoft Support.

      • Sultan says:

        It didn’t work.

        • Anonymous says:

          Try by adding the following line

          serviceProxy.EnableProxyTypes();
          IOrganizationService service = (IOrganizationService)serviceProxy;

      • sander says:

        This worked for me. And thanks for the original post as well! Really helpful!

  4. TrongNguyen says:

    Hi!
    When I run a sample in CRM 2011 SDK to add a note with a document attachment to the contact’s record. It throws exception:
    “The request channel timed out while waiting for a reply after 00:01:59.9069947. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted tho this operation may have been a portion of a longer timeout.”
    Note: I use CRM 2011 online. File upload has size 6M, type zip. I set file size limit for attachments to 8M.
    I hope you can help me. Thanks!

    • Vaibhav Tiwari says:

      Hi TrongNguyen,

      Create the registry entries as mentioned in this KB article http://support.microsoft.com/kb/918609, also increase the timeout in CRM’s all web.config files, you will need to change these two keys in web.config files executionTimeout=”3600″ maxRequestLength=”20000″, restart your IIS and Async Services instance and I think you will fine.

  5. CRM Dev says:

    Hi,

    I tried running the code as is and I get an error saying:

    “base {System.ServiceModel.CommunicationException} = {“The token provider cannot get tokens for target ‘http://crm2011/CRM/XRMServices/2011/Organization.svc’.”}”

    Could some one please help as I cannot figure this out.

    Thanks in advance

  6. Dharm says:

    Have anyone solved the exception related to missing assembly Microsoft.IdentityModel 3.5.0.0 on win 7 64 bit os?

  7. NAAZ says:

    OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);

    If i am not mistaken in this statement the last parameter is device credentials ( i.e. deviceID and DevicePassword) , could you specify for what reson these are passed and in which senario we would pass them.

    I need to build connection using Windows live ID. Any specific points to note of it.

    • Sanu says:

      Hi,

      Were you able to connect your app using Windows Live Id ?
      In fact i get an exception which says ” Value cannot be null. Parameter name: DeviceCredentials ”
      Any idea what would be going wrong ?

      • Sanu says:

        Hi was able to successfully connect.
        Device Credentials are always needed while connecting to Online MS CRM.

        For this use the deviceidmanager.cs from the helper code. have the needed dlls added.
        Make sure the timestamp ( clock skew) is in synch with that of the online CRM.

        It should work. :)

      • Nishant Rana says:

        Hi Sanu,

        Take a look at soaplogger sample in CRM 2011 SDK

        ..\sdk\samplecode\cs\client\soaplogger\soaplogger

        It makes use of crmservicehelpers.cs which would be of interest to you.

  8. TrongNguyen says:

    Can you help me fix it?
    Metadata contains a reference that cannot be resolved: ‘http://server02/XRMServices/2011/Discovery.svc?wsdl’.

  9. TrongNguyen says:

    Please help me!
    I run example “quickstart” in SDK Microsoft Dynamic CRM 2011 and thrown exception:

    Could not load file or assembly ‘Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf38564e35′ or one of its dependencies. The system cannot find the file specified.

    Can you help me fix it?
    Note: My operation operating system is “Win xp sp3 build 2600″ -> I can’t setup Microsoft Identity Model.

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s