• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1051
  • Last Modified:

Create your custom web service and integrate with CRM 2011 Online

i have a question about this blog, why is it necessary to create web services to communicate as per my understanding crm is a middle tier to which you are connecting through a webservice,is tht rite...

http://blogs.msdn.com/b/apurvghai/archive/2011/08/26/create-your-custom-web-service-and-integrate-with-crm-2011-online.aspx
0
Moizsaif123
Asked:
Moizsaif123
  • 4
  • 3
1 Solution
 
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

The first line says- "I hope this article can help you understand when you need to create a middleware between CRM system and a Legacy system/or custom application."

Suppose your organization already have a legacy system and you brought CRM 2011 into picture and now you want both systems to be synchronized with each other.

Thus making a bridge by adding a new web service between both system from either way you can refer to the newly created web service(different methods) and update data and keep the data synchronized.

Make sense?!?
0
 
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

Also, go through the comments on the same blog which throws some light on how people implement/want to implement.
0
 
Moizsaif123Author Commented:
when they give this comment below what do they mean by early bound

// This statement is required to enable early-bound type support.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
The short answer is that early (or static) binding refers to compile time binding and late (or dynamic) binding refers to runtime binding (for example when you use reflection).

Late Binding:
type is unknown until the variable is exercised during run-time; usually through assignment but there are other means to coerce a type; dynamically typed languages call this an underlying feature, but many statically typed languages have some method of achieving late binding

Implemented often using [special] dynamic types, introspection/reflection, flags and compiler options, or through virtual methods by borrowing and extending dynamic dispatch

Early Binding:
type is known before the variable is exercised during run-time, usually through a static, declarative means

Implemented often using standard primitive types

Early bound is nothing but to have all the classes ready with your code when you refer CRM 2011 Entities at compile time.

For example- Account, Contact, Incident, ... and so on.
And while coding you can create direct object of these classes like-
Account account = new Account();

Where in case of Late Bound- you do not have any classes like Account, Contact.
In Late Bound you will have a common class which is Entity and your code will look like this-

Entity account = new Entity("account");
0
 
Chinmay PatelEnterprise ArchitectCommented:
whoa Rikin :)... Great Going!!! I am Impressed...
0
 
Moizsaif123Author Commented:
I agree one last question, I understand that this is a 2 party webservice call, for example if we are using Microsoft Dynamics Nav 2009 R2 and it needs to communicate with ms dynamics crm then .net  needs to refer to the crm classes to communicate with CRM and uses webservice calls to push that data to the navision(legacy)


One question I have why do we need GetCredentials, is this for the CRM login credentials? and where does it come from actualy, @ the bottom of the page below the method "GetCredentials" is shown

http://blogs.msdn.com/b/apurvghai/archive/2011/08/26/create-your-custom-web-service-and-integrate-with-crm-2011-online.aspx
0
 
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
It creates the object of ClientCredentials which enables the user to configure client and service credentials as well as service credential authentication settings for use on the client side of communication.

In a simple answer; yes, it requires credentials of a user who has the access to entities for the proposed operation in code.

Thanks @Chinmay!
0
 
Moizsaif123Author Commented:
Getting en error on screenshot below while connecting to the crm connector in the for each in the getOrders() method in the code. Do I have to have MS Dynamics CRM open for  this to work,any ideas

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Security;
using System.Runtime.InteropServices;
using System.DirectoryServices.AccountManagement;
using System.ServiceModel;

//crm
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using EssaeXrm;

namespace Wpf_to_CRM
{
    class config
    {
        public Uri DiscoveryUri ;
        public Uri OrganizationUri ;
        DiscoveryServiceProxy discserviceProxy ;
        OrganizationServiceProxy orgserviceProxy ;
        public ClientCredentials clientcred ;
        public DateTime datm;
        string usr, pwd, reg, organisation , crmver;
        public ClientCredentials devicecred ;


        public void usrpwd(string x, string y , string z , string c)
        {
            this.usr = x;
            this.pwd = y;
            this.reg = z;
            this.crmver = c;
        }

  
        public virtual ClientCredentials GetCredentials()
        {
            ClientCredentials credentials = new ClientCredentials();
            credentials.UserName.UserName = usr;
            credentials.UserName.Password = pwd;
            return credentials;
        }

        protected virtual ClientCredentials GetDeviceCredentials()
        {
            return Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
        }

        public OrganizationDetailCollection DiscoverOrganizations(IDiscoveryService service)
        {
            if (service == null) throw new ArgumentNullException("service");
            RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
            RetrieveOrganizationsResponse orgResponse =
                (RetrieveOrganizationsResponse)service.Execute(orgRequest);

            return orgResponse.Details;
        }

        public OrganizationDetailCollection allorgs()
        {
            try
            {
                this.clientcred = this.GetCredentials();
                this.devicecred = this.GetDeviceCredentials();
                this.DiscoveryUri = new Uri(String.Format("https://{0}.{1}.dynamics.com/XRMServices/2011/Discovery.svc", this.crmver, this.reg));
                using (discserviceProxy = new DiscoveryServiceProxy(DiscoveryUri, null, clientcred, devicecred))
                {
                    OrganizationDetailCollection allorg = this.DiscoverOrganizations(discserviceProxy);
                    return allorg;
                }
            }
            catch
            {
                return new OrganizationDetailCollection();
            }
        }

        public List<string> orgdetail()
        {
            OrganizationDetailCollection orgs = this.allorgs();
            List<string> orgname = new List<string>();
            int i = 0;
            while(i < orgs.Count)
            {
                orgname.Add(orgs[i].UrlName);
                i++;
            }
            return orgname;
        }

        public void selectedorg(string retorg , DateTime datme)
        {
            this.organisation = retorg;
            this.datm = datme;
        }

        public DataTable querylist()
        {
            try
            {
                //Organization URL
                OrganizationUri = new Uri(String.Format("https://{0}.api.{1}.dynamics.com/XRMServices/2011/Organization.svc", this.organisation, this.reg));
                using (orgserviceProxy = new OrganizationServiceProxy(OrganizationUri, DiscoveryUri, clientcred, devicecred))
                {
                    // This statement is required to enable early-bound type support.
                    orgserviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                    IOrganizationService orgservice = (IOrganizationService)orgserviceProxy;
                    using (ServiceContext svcContext = new ServiceContext(orgserviceProxy))
                    {
                        DateTime d = datm.AddDays(1);
                        //DateTime d1 = d.AddDays(-1);
                        //DateTime d2 = d.AddDays(1);
                        var query = from a in svcContext.SalesOrderSet join b in svcContext.AccountSet on a.CustomerId.Id equals b.Id where a.SubmitDate < d && a.SubmitDate > datm && a.StatusCode == 3 select new { A = a, B = b };
                        //StreamWriter sw = new StreamWriter(@"C:\excelfile\excel.csv", true);
                        DataTable dt = new DataTable();
                        dt.Columns.Add("SO Id");
                        dt.Columns.Add("Order Number");
                        dt.Columns.Add("Name");
                        dt.Columns.Add("Submitted Date");
                        dt.Columns.Add("Customer");
                        dt.Columns.Add("Total Tax");
                        dt.Columns.Add("Total Line Item Amount");
                        dt.Columns.Add("Total Amount");
                        dt.Columns.Add("Structure");
                        foreach (var e in query)
                        {
                            dt.Rows.Add(e.A.Id.ToString(), e.A.OrderNumber, e.A.Name, e.A.SubmitDate, e.B.AccountNumber, e.A.TotalTax, e.A.TotalLineItemAmount, e.A.TotalAmount, e.A.new_Structure.Name);

                            //sw.WriteLine(e.A.Id.ToString() + "," + e.A.OrderNumber + "," + e.A.Name + "," + e.A.TotalAmount.ToString() + "," + e.A.CustomerId.Id.ToString() + "," + e.B.AccountNumber + "," + e.B.AccountId.Value.ToString() + "," + e.B.AccountId.ToString() + "," + e.B.Name);
                        }
                        //sw.Close();
                        return dt;

                    }
                }
            }
            catch
            {
                return new DataTable();
            }
        }

        public virtual ClientCredentials newcred(string usr, string pass)
        {
            ClientCredentials cred = new ClientCredentials();
            cred.UserName.UserName = usr;
            cred.UserName.Password = pass;
            return cred;
        }
        public DataTable prodquery(List<string> str)
        {
            DiscoveryUri = new Uri(String.Format("https://{0}.{1}.dynamics.com/XRMServices/2011/Discovery.svc", str[1], str[4]));
            OrganizationUri = new Uri(String.Format("https://{0}.api.{1}.dynamics.com/XRMServices/2011/Organization.svc",str[0],str[4]));
            this.newcred(str[2],str[3]);
            using(OrganizationServiceProxy orgprox = new OrganizationServiceProxy(OrganizationUri,DiscoveryUri,this.newcred(str[2],str[3]),this.GetDeviceCredentials()))
            {
                // This statement is required to enable early-bound type support.
                orgprox.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                using(ServiceContext svCon = new ServiceContext(orgprox))
                {
                    Guid ID = new Guid(str[5]);
                    var prodq = from e in svCon.SalesOrderDetailSet join d in svCon.ProductSet on e.ProductId.Id equals d.Id where e.SalesOrderId.Id == ID select new { E = e, D = d };
                    DataTable prdet = new DataTable();
                    prdet.Columns.Add("Product Name");
                    prdet.Columns.Add("Quantity");
                    prdet.Columns.Add("Unit Price");
                    foreach(var q in prodq)
                    {
                        prdet.Rows.Add(q.D.ProductNumber,q.E.Quantity,q.E.PricePerUnit);
                    }
                    return prdet;
                }
            }
        }

        public DataTable getOrders(DateTime datm)
        {
            try
            {
                DiscoveryUri = new Uri(String.Format("https://{0}.{1}.dynamics.com/XRMServices/2011/Discovery.svc", "dev", "crm5"));
                OrganizationUri = new Uri(String.Format("https://{0}.api.{1}.dynamics.com/XRMServices/2011/Organization.svc", "essaetechnologyspvtlimited", "crm5"));
                using (OrganizationServiceProxy orgproxy = new OrganizationServiceProxy(OrganizationUri, DiscoveryUri, this.newcred("essaecrm@live.in", "Essae@123"), this.GetDeviceCredentials()))
                {
                    orgproxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                    using (ServiceContext svcContext = new ServiceContext(orgproxy))
                    {
                        DateTime d = datm.AddDays(1);
                        var query = from a in svcContext.SalesOrderSet join b in svcContext.AccountSet on a.CustomerId.Id equals b.Id join c in svcContext.SystemUserSet on a.OwnerId.Id equals c.Id where a.SubmitDate < d && a.SubmitDate > datm && a.StatusCode == 3 select new { A = a, B = b, C = c };
                        DataTable dt = new DataTable();
                        dt.Columns.Add("SO Id");
                        dt.Columns.Add("Order Number");
                        dt.Columns.Add("Name");
                        dt.Columns.Add("Submitted Date");
                        dt.Columns.Add("Customer");
                        dt.Columns.Add("Address Contact");
                        dt.Columns.Add("Ship to Address 1");
                        dt.Columns.Add("Ship to Address 2");
                        dt.Columns.Add("Structure");
                        dt.Columns.Add("Ship to City");
                        dt.Columns.Add("Purchase Order No");
                        dt.Columns.Add("Purchase Order Date");
                        dt.Columns.Add("Remarks");
                        dt.Columns.Add("SO Series");
                        dt.Columns.Add("Core details");
                        dt.Columns.Add("Salesperson Code");
                        dt.Columns.Add("Request Delivery Date");
                        string SOseries = String.Empty;
                        string CoreDetails = String.Empty;
                        foreach (var e in query)
                        {
                            try
                            {
                                switch (e.A.new_SOseries)
                                {
                                    case 100000000: SOseries = "HO";
                                        break;
                                    case 100000001: SOseries = "LD";
                                        break;
                                    case 100000002: SOseries = "MH";
                                        break;
                                    case 100000003: SOseries = "AP";
                                        break;
                                    default: SOseries = "";
                                        break;
                                }

                                switch (e.A.new_CoreDetails)
                                {
                                    case 100000000: CoreDetails = "1\"";
                                        break;
                                    case 100000001: CoreDetails = "0.5\"";
                                        break;
                                    case 100000002: CoreDetails = "40mm";
                                        break;
                                    case 100000003: CoreDetails = "3\"";
                                        break;
                                    default: CoreDetails = "";

                                        break;
                                }
                                dt.Rows.Add(e.A.Id.ToString(), e.A.OrderNumber, e.A.Name, e.A.SubmitDate, e.B.AccountNumber, e.A.BillTo_ContactName, e.A.ShipTo_Line1, e.A.ShipTo_Line2, e.A.new_Structure.Name, e.A.ShipTo_City, e.A.new_PONumber, e.A.new_PurchaseOrderDate, e.A.Description, SOseries, CoreDetails, e.C.dynamics_erpsystemuserid.Name, e.A.RequestDeliveryBy);
                            }
                            catch
                            {
                            }
                        }
                        return dt;
                    }
                }
            }
            catch
            {
                return new DataTable();
            }
        }
    }
}

Open in new window

Err.docx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now