locked
Webservice RRS feed

  • Question

  • Hi all,

    I have created a webservice for retrieve the limit from the 'setup'  entity. Its working fine. Now I want to call this webservice from CRM Form Onload Event and capture the result also. Can any one provide code for this.

    Regards,
    Rajsekhar.
    Tuesday, May 12, 2009 7:23 AM

Answers

  • Hi, Rajsekhar.

    Look this article . I hope it will help you.
    Truth opens to prepared mind. My blog - http://a33ik.blogspot.com
    Tuesday, May 12, 2009 7:32 AM
    Moderator
  • you can use javascript on form onload to call your custom webservice.


    Mahain
    Monday, May 25, 2009 5:53 AM
    Moderator
  • Hi, Rajsekhar.

    First - you have to publish this web service.

    If you want use your custom web service from Plugins, Cutom Web Page Code Behind or Custom workflow - just add web reference to your web service and all required proxy types will be created automatically.

    If you want use your custom web service from Java Script - create SOAP request and send it to your web service like in article .
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 5:56 AM
    Moderator
  • Sekhar,

    you can develop this webservice anywhere, in case you develop your webservice other then the crm server just publish you webservice through VS in a folder and then paste this folder in crm server and create a virtual directory under ISV folder through IIS and direct url to the published folder.

    hope it will help.
    Mahain
    Monday, May 25, 2009 6:22 AM
    Moderator
  • Hi.

    • It is not necessary to develop web service on CRM Server.
    • It is strongly recomended to put customizations to ISV folder and this is supported.
    • To publish web service open your project with webservice and make right click on project - click publish. Select some folder on your hard disk and click ok.
    • Create in ISV folder of your ISV folder like ...\ISV\<OrgName>\<WebServiceName>\ and copy your published asmx and web.config files
    • Copy dll file(s) from bin subdirectory of publish directory to C:\Inetpub\wwwroot\bin\

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 6:24 AM
    Moderator
  • Hi.

    I've created a little sample which illustrates how to use webservices (this sample retrieves formatted server datetime in defined format):

    Web Service code:

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    
    namespace WebService1
    {
        /// <summary>
        /// Summary description for Service1
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        public class Service1 : System.Web.Services.WebService
        {
    
            [WebMethod]
            public string GetFormattedServerDateTime(string format)
            {
                return DateTime.Now.ToString(format);
            }
        }
    }
    
    Client JavaScript Code:

    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    
    //I Form SOAP request to WebService
    var query = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"+
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
    "	<soap:Body>"+
    "		<GetFormattedServerDateTime xmlns=\"http://tempuri.org/\">"+
    "			<format>yyyy.MM.dd hh:mm:ss</format>"+//Fill a parameter
    "		</GetFormattedServerDateTime>"+
    "	</soap:Body>"+
    "</soap:Envelope>";
    
    xmlhttp.open("POST", "/ISV/test/Service1.asmx", false);//WebService Url
    xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/GetFormattedServerDateTime");//Select a method
    xmlhttp.setRequestHeader("Content-Type", "text/xml");
    xmlhttp.setRequestHeader("Content-Length", query.length);
    
    xmlhttp.send(query);
    
    alert(xmlhttp.responseXML.text);
    Also look this article .
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 10:17 AM
    Moderator
  • Hi,

    where can i paste the .dll file. there is no bin folder in wwwroot. can i create? or can i paste it in CRMWeb\bin folder?.
    Hi.

    Hmmmm. Does your Microsoft CRM web sites hosts not on default port? If on default - there must be Bin folder C:\Inetpub\wwwroot\bin...
    If your Microsoft CRM web site hosts on another port just go to the Microsoft CRM website folder.

    All steps to deploy web service must be done on Microsoft CRM server, not on your development computer.

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 11:24 AM
    Moderator
  • Hi.

    I've just recreated your WebService and a little bit change your JavaScript. This works on my computer.

    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    
    var query = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
    "  <soap:Body>"+
    "    <HelloWorld xmlns=\"http://tempuri.org/\">"+
    "      <name>a33ik</name>"+
    "    </HelloWorld>"+
    "  </soap:Body>"+
    "</soap:Envelope>";
    
    xmlhttp.open("POST", "/ISV/helloname/Service1.asmx", false);
    xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/HelloWorld");
    xmlhttp.setRequestHeader("Content-Type", "text/xml");
    xmlhttp.setRequestHeader("Content-Length", query.length);
    
    xmlhttp.send(query);
    
    alert(xmlhttp.responseXML.xml);
    And the result alert box with text:

    <?xml version="1.0"?>
    
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
    <HelloWorldResponse xmlns="http://tempuri.org/">
    <HelloWorldResult>Hello a33ik</HelloWorldResult></HelloWorldResponse>
    </soap:Body>
    </soap:Envelope>
    I hope this will help you.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Tuesday, May 26, 2009 9:45 AM
    Moderator

All replies

  • Hi, Rajsekhar.

    Look this article . I hope it will help you.
    Truth opens to prepared mind. My blog - http://a33ik.blogspot.com
    Tuesday, May 12, 2009 7:32 AM
    Moderator
  • Hi,

    I have created webservice through visual studio. how can i call that .asmx page from crm?.

    Regards,
    Rajsekhar
    Monday, May 25, 2009 5:01 AM
  • you can use javascript on form onload to call your custom webservice.


    Mahain
    Monday, May 25, 2009 5:53 AM
    Moderator
  • Hi, Rajsekhar.

    First - you have to publish this web service.

    If you want use your custom web service from Plugins, Cutom Web Page Code Behind or Custom workflow - just add web reference to your web service and all required proxy types will be created automatically.

    If you want use your custom web service from Java Script - create SOAP request and send it to your web service like in article .
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 5:56 AM
    Moderator
  • Hi,

    . How can i publish the webservice?
    . Is it require to develop webservice in CRM server or can i develop it some where else and use it in server?
    . Is it require to paste the webservice in ISV folder?

    plz give me a clear idea.
    Monday, May 25, 2009 6:02 AM
  • Sekhar,

    you can develop this webservice anywhere, in case you develop your webservice other then the crm server just publish you webservice through VS in a folder and then paste this folder in crm server and create a virtual directory under ISV folder through IIS and direct url to the published folder.

    hope it will help.
    Mahain
    Monday, May 25, 2009 6:22 AM
    Moderator
  • Hi.

    • It is not necessary to develop web service on CRM Server.
    • It is strongly recomended to put customizations to ISV folder and this is supported.
    • To publish web service open your project with webservice and make right click on project - click publish. Select some folder on your hard disk and click ok.
    • Create in ISV folder of your ISV folder like ...\ISV\<OrgName>\<WebServiceName>\ and copy your published asmx and web.config files
    • Copy dll file(s) from bin subdirectory of publish directory to C:\Inetpub\wwwroot\bin\

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 6:24 AM
    Moderator
  • Hi a33ik,

    I did all the above things and the following is my code. I didn't get the result. Is there any change in my code. There is no bin folder in wwwroot. I created manually and paste the dll in that.



    var url="/PublishedRetLimit/Service1.asmx/RetreiveFromSetupentity";
    var msg = "";
     try
     {
        // Use xmlhttp connection to web server
        var oXmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");   
        oXmlHTTP.Open("POST", url, false);   
        oXmlHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");   
        var result = "";
        // Check to see if we have a valide response
        if ((oXmlHTTP.responseXML.xml) != null && (oXmlHTTP.responseXML.xml.toString().length > 0))
        {
          // The service will return the result in a string node.
          result = oXmlHTTP.responseXML.selectSingleNode("string").text;
        }
        msg = result;
     }
     catch(e)
     {
        msg = "There was an error with your submission. Please try again. Error:" + e.message;
     }  // Display our results back to the user.
     if(msg.length > 0)
      {
      alert(msg);
      }
    Monday, May 25, 2009 6:43 AM
  • Hi.

    Of cource it doesn't work.

    var url="/PublishedRetLimit/Service1.asmx"; must be.

    And there are no SOAP request you form. Look article how to do this .
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 6:46 AM
    Moderator
  • Hi a33ik,

    As per ur suggestion I used that article and write the following code. I dont understand that appid,offset, etc.,. Check the code once.

    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    var query = "<?xml version='1.0' encoding='UTF-8'?>";
    query += "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://";
    query += "schemas.xmlsoap.org/soap/envelope/'";
    query += " xmlns:xsi=";
    query += "'http://www.w3.org/1999/XMLSchema-instance'";
    query += " xmlns:xsd=";
    query += "'http://www.w3.org/1999/XMLSchema'> ";
    query += "<SOAP-ENV:Body><ns1:Search xmlns:ns1='";
    query += "http://schemas.microsoft.com/";
    query += "MSNSearch/2005/09/fex'>";
    query += "</ns1:Search>";
    query += "</SOAP-ENV:Body></SOAP-ENV:Envelope>";

    var uri = "/PublishedRetLimit/Service1.asmx/RetreiveFromSetupentity";
    xmlhttp.open("get", uri, false);
    xmlhttp.setRequestHeader("Content-Type", "text/xml");
    xmlhttp.setRequestHeader("SOAPAction", "Search");
    xmlhttp.send(query);
    var rx = xmlhttp.ResponseXML;
    var sr = rx.getElementsByTagName("SourceResponse");
    var results = sr.item(0).getElementsByTagName("Result");
    var res = results.item(0).selectSingleNode("string").text;
    alert(res);

    I am getting the following error.

    Error: item(...) is null or not an object

    Monday, May 25, 2009 8:29 AM
  • Hi.

    I've created a little sample which illustrates how to use webservices (this sample retrieves formatted server datetime in defined format):

    Web Service code:

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    
    namespace WebService1
    {
        /// <summary>
        /// Summary description for Service1
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        public class Service1 : System.Web.Services.WebService
        {
    
            [WebMethod]
            public string GetFormattedServerDateTime(string format)
            {
                return DateTime.Now.ToString(format);
            }
        }
    }
    
    Client JavaScript Code:

    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    
    //I Form SOAP request to WebService
    var query = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"+
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
    "	<soap:Body>"+
    "		<GetFormattedServerDateTime xmlns=\"http://tempuri.org/\">"+
    "			<format>yyyy.MM.dd hh:mm:ss</format>"+//Fill a parameter
    "		</GetFormattedServerDateTime>"+
    "	</soap:Body>"+
    "</soap:Envelope>";
    
    xmlhttp.open("POST", "/ISV/test/Service1.asmx", false);//WebService Url
    xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/GetFormattedServerDateTime");//Select a method
    xmlhttp.setRequestHeader("Content-Type", "text/xml");
    xmlhttp.setRequestHeader("Content-Length", query.length);
    
    xmlhttp.send(query);
    
    alert(xmlhttp.responseXML.text);
    Also look this article .
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 10:17 AM
    Moderator
  • Hi,

    where can i paste the .dll file. there is no bin folder in wwwroot. can i create? or can i paste it in CRMWeb\bin folder?.
    Monday, May 25, 2009 11:20 AM
  • Hi,

    where can i paste the .dll file. there is no bin folder in wwwroot. can i create? or can i paste it in CRMWeb\bin folder?.
    Hi.

    Hmmmm. Does your Microsoft CRM web sites hosts not on default port? If on default - there must be Bin folder C:\Inetpub\wwwroot\bin...
    If your Microsoft CRM web site hosts on another port just go to the Microsoft CRM website folder.

    All steps to deploy web service must be done on Microsoft CRM server, not on your development computer.

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 11:24 AM
    Moderator
  • hi

    we did all the steps suggested by you. we did the same example given by you and published the project and placed the .asmx and web.config file in ISV/<webservice name> folder and palced the dll in bin folder but the alert shows a empty value. Its not returing any thing.
    Monday, May 25, 2009 11:47 AM
  • Hi.

    To access to webservice trough browser. Something like url http://<servername>/ISV/<organizationname>/test/Service1.asmx

    If you have done everything right you will retrieve following page:

    http://lh6.ggpht.com/_73OmG38HHME/ShqHOAJHZPI/AAAAAAAAAHw/w9KTu9plBX0/s512/webservice.jpg

    If not - you will retrieve error description.

    One thing i didn't wrote that you need correct web.config for your web service:

    <?xml version="1.0"?>
    <configuration>
      <appSettings/>
      <connectionStrings/>
      <system.web>
        <compilation debug="false">
        </compilation>
        <identity impersonate="true"/>
      </system.web>
    </configuration>
    

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 11:57 AM
    Moderator
  • hi

    there is no change in the result. Its still displaying empty alert with no data.
    Monday, May 25, 2009 12:11 PM
  • Hi.

    Try to access to WebService trough web browser.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 12:15 PM
    Moderator
  • its working fine and getting result also
    Monday, May 25, 2009 12:19 PM
  • Hi.

    Give your JavaScript for investigation.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 12:20 PM
    Moderator
  • Hi,


    The following is the webservice what I have written

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.Linq;

    using

     

    System.Web;

    using

     

    System.Web.Services;

    namespace

     

    hello

    {

     

    /// <summary>

     

    /// Summary description for Service1

     

    /// </summary>

    [

    WebService(Namespace = "http://tempuri.org/")]

    [

    WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [System.ComponentModel.

    ToolboxItem(false)]

     

    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

     

    // [System.Web.Script.Services.ScriptService]

     

    public class Service1 : System.Web.Services.WebService

    {

    [

    WebMethod]

     

    public string HelloWorld(string name)

    {

     

    return "Hello "+name;

    }

    }

    }

     




    The following is the javascript code

    var xmlToSend = "<?xml version='1.0' encoding='utf-8'?>";
    xmlToSend += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
    xmlToSend += "xmlns:xsd='http://www.w3.org/2001/XMLSchema' ";
    xmlToSend += "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
    xmlToSend += "<soap:Body><HelloWorld xmlns='http://tempuri.org/' >";
    xmlToSend += "<name>phani</name>";
    xmlToSend += "</HelloWorld>";
    xmlToSend += "</soap:Body></soap:Envelope>";
    var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
    xmldoc.loadXML(xmlToSend);
    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange = state_Change;
    xmlhttp.open("POST", "/ISV/helloname/Service1.asmx", false);
    xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/HelloWorld");
    xmlhttp.setRequestHeader("Content-Type", "text/xml");
    xmlhttp.send(xmldoc);
    alert(xmlhttp.responseXML.xml);

    function state_Change()
    {
        // if xmlhttp shows "loaded"
        if (xmlhttp.readyState==4)
        {
            // if "OK"
            if (xmlhttp.status==200)
            {
                alert("OK");
            }
            else
            {
                alert("Problem retrieving XML data");
            }
        }
    }

    Tuesday, May 26, 2009 3:36 AM
  • Hi.

    I've just recreated your WebService and a little bit change your JavaScript. This works on my computer.

    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    
    var query = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
    "  <soap:Body>"+
    "    <HelloWorld xmlns=\"http://tempuri.org/\">"+
    "      <name>a33ik</name>"+
    "    </HelloWorld>"+
    "  </soap:Body>"+
    "</soap:Envelope>";
    
    xmlhttp.open("POST", "/ISV/helloname/Service1.asmx", false);
    xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/HelloWorld");
    xmlhttp.setRequestHeader("Content-Type", "text/xml");
    xmlhttp.setRequestHeader("Content-Length", query.length);
    
    xmlhttp.send(query);
    
    alert(xmlhttp.responseXML.xml);
    And the result alert box with text:

    <?xml version="1.0"?>
    
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
    <HelloWorldResponse xmlns="http://tempuri.org/">
    <HelloWorldResult>Hello a33ik</HelloWorldResult></HelloWorldResponse>
    </soap:Body>
    </soap:Envelope>
    I hope this will help you.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Tuesday, May 26, 2009 9:45 AM
    Moderator