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
https://nishantrana.wordpress.com/2012/12/06/sample-code-to-connect-office-365-users-to-crm-2011-online/
Bye.


					

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

35 thoughts on “Sample Code for using IOrganizationService in CRM 2011”

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

    1. Hi How to assing my custome guid while creating entity in CRM 2011 using IOrganizationService in CRM 2011

      Like

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

    Like

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

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

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

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

        Like

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

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

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

      Like

  6. Hello,

    How to avoid of using

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

    but use WindowsIdentity or pass NTLM authentication details?

    Like

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

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

        1. Try by adding the following line

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

          Like

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

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

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

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

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

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

  13. Fascinating blog! Is your theme custom made or did you download it from somewhere?

    A design like yours with a few simple tweeks would really make my blog stand out.
    Please let me know where you got your theme. Kudos

    Like

Please 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

Two Bite Tips

Valuable Tips To Grow Your Business

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

Dreaming to be a clean coder and TDD minded programmer.

Transform 365

We blog about problems we face and code we write to help others

%d bloggers like this: