Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create your custom web service and integrate with CRM 2011 Online

Posted on 2013-11-14
8
Medium Priority
?
1,044 Views
Last Modified: 2013-11-15
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
Comment
Question by:Moizsaif123
  • 4
  • 3
8 Comments
 
LVL 20

Accepted Solution

by:
Rikin Shah earned 2000 total points
ID: 39648603
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
 
LVL 20

Expert Comment

by:Rikin Shah
ID: 39648625
Hi,

Also, go through the comments on the same blog which throws some light on how people implement/want to implement.
0
 
LVL 4

Author Comment

by:Moizsaif123
ID: 39648652
when they give this comment below what do they mean by early bound

// This statement is required to enable early-bound type support.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 20

Expert Comment

by:Rikin Shah
ID: 39648679
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
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 39650367
whoa Rikin :)... Great Going!!! I am Impressed...
0
 
LVL 4

Author Comment

by:Moizsaif123
ID: 39650773
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
 
LVL 20

Expert Comment

by:Rikin Shah
ID: 39650811
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
 
LVL 4

Author Comment

by:Moizsaif123
ID: 39650975
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question