locked
How to Create ServiceProxy for CRM 4.0 ? RRS feed

  • Question

  • I wanted to create serviceProxy in CRM 4.0 to perform CRUD operation from outside CRM..

    in CRM 5.0 it's present by default.. how about CRM 4.0 ?


    If it helps, Mark as answer to help others..else it's scrap


    • Edited by ViN.k.S Friday, November 9, 2012 7:02 AM spell mistake
    Friday, November 9, 2012 5:40 AM

Answers

  • Hi,

    try this, it should work:

    public static CrmService CRUDservice(string username, string domain, string password, string server)
            {
                // get the Discovery Service
                CrmDiscoveryService discoveryService = new CrmDiscoveryService();
                discoveryService.Credentials = new NetworkCredential(username, password, domain);
                discoveryService.Url = "http://" + server + "/MSCRMServices/2007/AD/CrmDiscoveryService.asmx";
                // Org Request 
                RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
                orgRequest.UserId = username;
                orgRequest.Password = password;
                // Org Response
                RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);
                OrganizationDetail orgInfo = null;
                // Org detail
                OrganizationDetail thisOrg = orgResponse.OrganizationDetails[0];
                string _svcUrl = thisOrg.CrmServiceUrl;
                string _metUrl = thisOrg.CrmMetadataServiceUrl;
                string _orgGuid = thisOrg.OrganizationId.ToString();
                string _orgName = thisOrg.OrganizationName;
                string _webUrl = thisOrg.WebApplicationUrl;
                string _frndName = thisOrg.FriendlyName;
    
                //== Create & Return CRUD ======================
    
                // Retrieve the ticket.
                RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
                ticketRequest.OrganizationName = _orgName;
                ticketRequest.UserId = domain + "\\" + username;
                ticketRequest.Password = password;
                RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)discoveryService.Execute(ticketRequest);
    
                //Create the CrmService Web service proxy.
                CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();
                sdktoken.AuthenticationType = 0; //2
                sdktoken.OrganizationName = _orgName;
                //sdktoken.CrmTicket = ticketResponse.CrmTicket;
    
                CrmService Cservice = new CrmService();
                Cservice.CrmAuthenticationTokenValue = sdktoken;
                Cservice.Url = _svcUrl;
                Cservice.PreAuthenticate = true;
                Cservice.Credentials = new NetworkCredential(username, password, domain);
                //WebApplicationUrl = _webUrl;
                // OrganizationId = orgdetail.OrganizationId;
                return Cservice;
            }

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    • Proposed as answer by Pavlos Panagiotidis Thursday, November 15, 2012 9:31 AM
    • Marked as answer by ViN.k.S Monday, November 26, 2012 5:41 AM
    Wednesday, November 14, 2012 7:41 AM

All replies

  • Hi,

    you can program against Dynamics CRM 4.0 using either the Web Service and adding a web service reference to your project or using the 4.0 assemblies provided in the Dynamics CRM 4.0 SDK. You can find some sample code on working with the web service here. Working with the assemblies is quite similar to it.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.


    Friday, November 9, 2012 7:57 AM
  • Hi ,

    I think this may help you.

    // Create and configure the CrmDiscoveryService Web service proxy.
    CrmDiscoveryService discoveryService = new CrmDiscoveryService();
    discoveryService.UseDefaultCredentials = true;
    discoveryService.Url = "http://localhost/MSCRMServices/2007/AD/CrmDiscoveryService.asmx";

    // Retrieve the list of organizations that the logged on user belongs to.
    RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
    RetrieveOrganizationsResponse orgResponse =
        (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);

    // Locate the target organization in the list.
    OrganizationDetail orgInfo = null;
    foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
    {
        if (orgDetail.OrganizationName.Equals("AdventureWorksCycle"))
        {
            orgInfo = orgDetail;
            break;
        }
    }

    // Check whether a matching organization was not found.
    if (orgInfo == null)
        throw new Exception("The specified organization was not found.");

    After the OrganizationDetail for the target organization has been obtained, the CrmService and CrmMetadataService Web services can be accessed through the provided URLs in OrganizationDetail to obtain the organization's business data.

    CrmAuthenticationToken token = new CrmAuthenticationToken();
    token.AuthenticationType = 0;
    token.OrganizationName = orgInfo.OrganizationName;

    CrmService crmService = new CrmService();
    crmService.Url = orgInfo.CrmServiceUrl;
    crmService.CrmAuthenticationTokenValue = token;
    crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;

    If you know the CrmService and CrmMetadataService Web service URLs for the target organization and the name of the organization, you do not have to use the CrmDiscoveryService Web service. You can configure the CrmAuthenticationToken and CrmService instances and invoke Web methods.

    Please ref the link http://msdn.microsoft.com/en-us/library/cc151012.aspx

    Cheers,

    Suresh Kumar D


    Warm Regards, Suresh Kumar D

    Friday, November 9, 2012 9:20 AM
  • Thanks both of you.. i got the answer 15 min after asking this question... Was about to update but caught by error

    Any updates on this..

    Any one have info about the Authentication Mecanisam of CRM 4.0 ?


    If it helps, Mark as answer to help others..else it's scrap

    Friday, November 9, 2012 9:47 AM
  • Hi,

    please share the code you use to initialize the CRM Service so that we can identify the problem. The service requires Windows Authentication for an On Premise installation.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    Friday, November 9, 2012 9:48 AM
  • Thanks Here is the code

        public static CrmService CRUDservice(string username, string domain, string password, string server)
        {
            // get the Discovery Service
            CrmDiscoveryService discoveryService = new CrmDiscoveryService();
            discoveryService.Credentials = new NetworkCredential(username,password,domain);
            discoveryService.Url = "http://" + server + "/MSCRMServices/2007/AD/CrmDiscoveryService.asmx";
            // Org Request 
            RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
            orgRequest.UserId = username;
            orgRequest.Password = password;
            // Org Response
            RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);
            OrganizationDetail orgInfo = null;
            // Org detail
            OrganizationDetail thisOrg = orgResponse.OrganizationDetails[0];
            string _svcUrl = thisOrg.CrmServiceUrl;
            string _metUrl = thisOrg.CrmMetadataServiceUrl;
            string _orgGuid = thisOrg.OrganizationId.ToString();
            string _orgName = thisOrg.OrganizationName;
            string _webUrl = thisOrg.WebApplicationUrl;
            string _frndName = thisOrg.FriendlyName;
    
            //== Create & Return CRUD ======================
            
            // Retrieve the ticket.
                    RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
                    ticketRequest.OrganizationName = _orgName;
                    ticketRequest.UserId = domain + "\\" + username;
                    ticketRequest.Password = password;
                    RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)discoveryService.Execute(ticketRequest);
                    
            //Create the CrmService Web service proxy.
                    CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();
                    sdktoken.AuthenticationType = 0; //2
                    sdktoken.OrganizationName = _orgName;
                    sdktoken.CrmTicket = ticketResponse.CrmTicket;
    
                    CrmService Cservice = new CrmService();
                    Cservice.CrmAuthenticationTokenValue = sdktoken;
                    Cservice.Url = _svcUrl;
                    Cservice.PreAuthenticate = true;
                    //WebApplicationUrl = _webUrl;
                   // OrganizationId = orgdetail.OrganizationId;
                    return Cservice;
        }

    If it helps, Mark as answer to help others..else it's scrap

    Wednesday, November 14, 2012 5:13 AM
  • Hi,

    try this, it should work:

    public static CrmService CRUDservice(string username, string domain, string password, string server)
            {
                // get the Discovery Service
                CrmDiscoveryService discoveryService = new CrmDiscoveryService();
                discoveryService.Credentials = new NetworkCredential(username, password, domain);
                discoveryService.Url = "http://" + server + "/MSCRMServices/2007/AD/CrmDiscoveryService.asmx";
                // Org Request 
                RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
                orgRequest.UserId = username;
                orgRequest.Password = password;
                // Org Response
                RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);
                OrganizationDetail orgInfo = null;
                // Org detail
                OrganizationDetail thisOrg = orgResponse.OrganizationDetails[0];
                string _svcUrl = thisOrg.CrmServiceUrl;
                string _metUrl = thisOrg.CrmMetadataServiceUrl;
                string _orgGuid = thisOrg.OrganizationId.ToString();
                string _orgName = thisOrg.OrganizationName;
                string _webUrl = thisOrg.WebApplicationUrl;
                string _frndName = thisOrg.FriendlyName;
    
                //== Create & Return CRUD ======================
    
                // Retrieve the ticket.
                RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
                ticketRequest.OrganizationName = _orgName;
                ticketRequest.UserId = domain + "\\" + username;
                ticketRequest.Password = password;
                RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)discoveryService.Execute(ticketRequest);
    
                //Create the CrmService Web service proxy.
                CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();
                sdktoken.AuthenticationType = 0; //2
                sdktoken.OrganizationName = _orgName;
                //sdktoken.CrmTicket = ticketResponse.CrmTicket;
    
                CrmService Cservice = new CrmService();
                Cservice.CrmAuthenticationTokenValue = sdktoken;
                Cservice.Url = _svcUrl;
                Cservice.PreAuthenticate = true;
                Cservice.Credentials = new NetworkCredential(username, password, domain);
                //WebApplicationUrl = _webUrl;
                // OrganizationId = orgdetail.OrganizationId;
                return Cservice;
            }

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    • Proposed as answer by Pavlos Panagiotidis Thursday, November 15, 2012 9:31 AM
    • Marked as answer by ViN.k.S Monday, November 26, 2012 5:41 AM
    Wednesday, November 14, 2012 7:41 AM
  • Thanks Pavlos Panagiotidis

    Marked as answer..


    If it helps, Mark as answer to help others..else it's scrap

    Monday, November 26, 2012 5:42 AM