10/12/2012

Sample Code - CRM Using Organization Service Proxy

There are many ways to write code against CRM. One of the very easy ways is to use late-bound objects and use organization service proxy.

You first need to create a CRM organization proxy, then use late-bound entity object and set the attributes and then save it through the generated proxy.

With just a few lines in this sample code, you can create an "Account" in CRM 2011:

Create a Console App



Add References

Add the following references to your project

Micorosoft.Crm.Sdk.Proxy (from CRM SDK bin folder)

Micorosoft.Crm.Sdk (from CRM SDK bin folder)

System.ServiceModel

System.Runtime.Serialization


Add Using statements

The following namespaces should be added to the using section of your code

using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk.Client;

using Microsoft.Xrm.Sdk;


Sample code: Creating and Account


        static void Main(string[] args)
        {
            //Grab the organization service url by navigating to
            // Settings -> Customizations - > Developer Resources
            // The following is from online CRM org
            Uri oUri = new Uri("https://yourorg.api.crm5.dynamics.com/XRMServices/2011/Organization.svc");
            //** Your client credentials 
            ClientCredentials clientCredentials = new ClientCredentials();
            clientCredentials.UserName.UserName = "YourAccount.onmicrosoft.com";
            clientCredentials.UserName.Password = "YourAdminPassword";

            //Create your Organization Service Proxy
            OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(
                oUri,
                null,
                clientCredentials,
                null);

            //** Now simple use Entity and create a sample account
            Entity entity = new Entity("account");
            entity.Attributes["name"] = "This is my sample account";

            //use the service proxy to create the entity object
            _serviceProxy.Create(entity);

            Console.WriteLine("Welldone SuperNova!");
            Console.ReadKey(true);
        }

7 comments:

  1. I m searching sites in which I put a best link which performance very fast It do unblock sites in seconds you can utilize this link just single click
    Instagram uk proxy

    ReplyDelete
  2. hi can tell me the code for one plugin if we create Account it should create Contact automatically using sdk tool kit

    ReplyDelete
  3. thanks brother for this subject . i have some proxy web sit i want to hcar with you
    HEroproxy.ml
    1HEroproxy.ml
    proxysit.ml

    ReplyDelete
  4. Glad it is useful, life doesn't give chances to write more often :)

    ReplyDelete
  5. Hi,

    we are trying to create crm org programmatically c# - everything is working just fine IF we use http to access CRM deployment svc

    but getting this over https: The request for security token could not be satisfied because authentication failed. Type: System.ServiceModel.FaultException

    any ideas?

    CRM 2013 + ADFS 2.2

    ReplyDelete