Hi ,
In this post I would like to share how to config , Wcf service with Crm service
Before starting Have a look  how to interact WCF Services.

Create WCF Application .
Refer Bellow Sample Code :


IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
    [OperationContract]
    string CreateLead(string FirstName , string LastName , string EmailId);
}

 


Service1.cs
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
    /// <summary>
    ///   username = Declaring Organization credential User Name (Online / on premis)
    ///   url = Organization Service Url
    /// </summary>

    #region Organization Credential
    OrganizationServiceProxy _oService;
    string username = "parthiban@dyn.onmicrosoft.com";
    string password = "********";
    string url = "https://dyn.api.mscrm.dynamics.com/XRMServices/2011/Organization.svc";
    #endregion

    #region Create Lead Record
    /// <summary>
    /// Create Lead Record
    /// </summary>
    /// <param name="FirstName"></param>
    /// <param name="LastName"></param>
    /// <param name="EmailId"></param>
    /// <returns>New Created Lead Guid </returns>
    public string CreateLead(string FirstName, string LastName, string EmailId)
    {

        Guid newContactId = Guid.Empty;
        //Check Weather Crm Authentication
        if (InitializeCRMService(username, password, url))
        {
            Entity LeadEntity = new Entity("lead");
            LeadEntity["firstname"] = FirstName;
            LeadEntity["lastname"] = LastName;
            LeadEntity["emailaddress1"] = EmailId;
            LeadEntity["subject"] = "Lead Created From Web Service";
            newContactId = _oService.Create(LeadEntity);

            return "New Lead Created Id : " + newContactId.ToString();
        }

        else
        {
            return "Record Could Not Be Created :" + newContactId.ToString();
        }
    }
    #endregion

    #region check for CRM Authentication Service
    /// <summary>
    /// Checking For Client Credential
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="passWord"></param>
    /// <param name="Url"></param>
    /// <returns>Bool Value Either True / False </returns>
    private bool InitializeCRMService(string userName, string passWord, string Url)
    {
        bool isSuccess = false;
        Uri organizationUrl = new Uri(Url);
        ClientCredentials credentials = new ClientCredentials();
        credentials.UserName.UserName = userName;
        credentials.UserName.Password = passWord;
        _oService = new OrganizationServiceProxy(organizationUrl, null, credentials, null);
        _oService.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
        //Validate Users Credential
        if (_oService != null)
            isSuccess = true;
        return isSuccess;
    }
    #endregion
}

Run Your Application Service1.svc

 

WCF Output Image

Advertisements

Leave a Reply

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s