Tuesday, December 17, 2013

Sample to Access CRM 2011 using WSDL


Steps:

SERVICE REFERENCES

Add the following service references, using your server name:

 

1) Example URL: http://myserver/XRMServices/2011/Organization.svc

   Namespace: OrgService

2) Change Username,password,domainname,OrganizationName

 

 

Sample Code


using System;

using System.Collections.Generic;

using System.Globalization;

using System.Net;

using System.ServiceModel;

using System.ServiceModel.Description;

 

namespace Microsoft.Crm.Samples

{

      

       internal static class Program

       {

              #region Constants

              /// <summary>

              /// User Domain

              /// </summary>

              private const string UserDomain = "domainname";

 

              /// <summary>

              /// User Name

              /// </summary>

              private const string UserName = "username";

 

              /// <summary>

              /// Password

              /// </summary>

              private const string UserPassword = "password";

 

              /// <summary>

              /// Unique Name of the organization

              /// </summary>

              private const string OrganizationUniqueName = "orgnanizationname";

 

              /// <summary>

              /// URL for the Discovery Service

              /// </summary>

              private const string DiscoveryServiceUrl = "http://myserver/XRMServices/2011/Discovery.svc";

        private const string OrganizationServiceUrl = "http://fhvmcrmupgd1:5555/Foresters/XRMServices/2011/Organization.svc";

              #endregion

 

              static void Main(string[] args)

              {

                     //Generate the credentials

                     ClientCredentials credentials = new ClientCredentials();

                     credentials.Windows.ClientCredential = new NetworkCredential(UserName, UserPassword, UserDomain);

            CreateAccount(credentials);

              }

 

        private static void CreateAccount(ClientCredentials credentials)

        {

            //Create Service Client

            OrgService.OrganizationServiceClient orgServiceclient = new OrgService.OrganizationServiceClient("CustomBinding_IOrganizationService", OrganizationServiceUrl);

            ApplyCredentials(orgServiceclient, credentials);

            //orgServiceclient.ClientCredentials.Windows.ClientCredential = credentials.Windows.ClientCredential;

            OrgService.AttributeCollection acollection = new OrgService.AttributeCollection();

            //you can use early bound by adding proxy class

            acollection.Add(new KeyValuePair<string, object>("name", "SampleCreatedByWSDl"));

            OrgService.Entity account= new OrgService.Entity()

            {

                LogicalName="account",

                Attributes=acollection

            };

            orgServiceclient.Create(account);

        }

        private static void ApplyCredentials<TChannel>(ClientBase<TChannel> client, ClientCredentials credentials)

            where TChannel : class

        {

            client.ClientCredentials.Windows.ClientCredential = credentials.Windows.ClientCredential;

        }

       }

}  

 

No comments: