Sample Code for using IOrganizationService in CRM 2011

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.



32 thoughts on “Sample Code for using IOrganizationService in CRM 2011

  1. Hi Nishant,
    I am working with asp.net website connecting to CRM 2011. I am stuck up with idea of passing credentials of the logged in user to crm 2011. If I surpass following lines of code I get SSPI error while updating

    ClientCredentials credentials = new ClientCredentials();
    // set default credentials for OrganizationService
    credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
    // or
    credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;

    Please suggest on this.

    Thanks

    Like

  2. In fact, after securing a job and getting funded, management is easily the most important part of the job, and construction software is a management
    must-have. Absolutely no distinct software program is fitted to most venture requirements.
    and its USA IT project management training services, visit Myriad – Inc.

    Like

  3. It is the outburst of flavours that people become inclined towards eating different types of cuisines.
    ) And in ancient Greek society, the revealing of a foot by a woman was tantamount to making
    an overt sexual advance. Ishoo is the first and only surgeon
    qualified to perform the VASER� Hi-Def Lipo – Sculpture in New England.

    Like

  4. If you’ve ever been accused of being too intelligent for certain people’s tastes, you may have
    even been called “Einstein” in a sarcastic or derogatory sort of way, as
    well. Wondering how much money you can expect to make as an athletic trainer.
    For interested readers we have some very handy and related information on our website about Taylor Refrigerator Thermometer and
    Digital Refrigerator Freezer Thermometer.

    Like

  5. 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….

    Like

  6. 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

    Like

  7. Hello,

    How to avoid of using

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

    but use WindowsIdentity or pass NTLM authentication details?

    Like

    • 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.

      Like

      • 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.

        Like

        • Try by adding the following line

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

          Like

  8. 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!

    Like

  9. 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

    Like

  10. 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.

    Like

    • 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 ?

      Like

      • 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. :)

        Like

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

    Like

  12. 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.

    Like

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 )

Google+ photo

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

Connecting to %s