通过javascript使用Microsoft Dynamics CRM Web Services

最近在newsgroup经常有人问怎么在javascript使用Microsoft Dynamics CRM Web Services 获取mscrm数据,在mscrmsdk4.0中已经有Accessing Web Services in JScript相关介绍了,看了sdk后你是否还是觉得要写js访问webservice来解决自己的实际问题还是很困难? 下面我来介绍 stunnware写的一个程序Calling CRM Services from JavaScript,通过这个程序你可以很容易来构建你的js代码实现具体的需求.

在vs.net中打开项目,在Form1设计界面点击start按钮,在click事件中,写访问webservice的代码,下面我提供一个比较简单的通过产品guid获取productnumber和price.
       ///   <summary>
      
///  Starts the execution of a web service. Put your code inside the try block. You can use 
      
///  every web service you like, so you're not limited to query expressions. 
      
///   </summary>
      
///   <param name="sender"></param>
      
///   <param name="e"></param>
       private   void  buttonStart_Click( object  sender, EventArgs e) {

         buttonStart.Enabled 
=   false ;
         textBoxRequest.Text 
=   string .Empty;
         textBoxResponse.Text 
=   string .Empty;
         
try  
         {
             ColumnSet cols 
=   new  ColumnSet();
             cols.Attributes 
=   new   string [] {  " productnumber " , " price " };
             Guid productGuid 
=   new  Guid( " 066BC88D-01C4-DC11-A4B2-001D09006D68 " );
             CrmLogService service 
=   new  CrmLogService();
             service.Url 
=   " http://localhost/mscrmservices/2006/crmservice.asmx " ;
             service.Credentials 
=  System.Net.CredentialCache.DefaultCredentials();
             service.SoapMessageSerializationFinished 
+=   new  SoapMessageEventHandler(service_SoapMessageSerializationFinished);
             service.Retrieve(EntityName.product.ToString(), productGuid, cols);
         }
         
catch  (Exception x) 
         {
            ExceptionBox.Show(
this , x);
         }
         
finally  {
            buttonStart.Enabled 
=   true ;
         }
      }
运行程序,点start按钮,可以得到下面结果:
通过javascript使用Microsoft Dynamics CRM Web Services
把JavaScript Code 中的代码copy到onload事件中,在加入下面代码来获取具体属性的值
var doc = xmlHttpRequest.responseXML;
var user = doc.selectSingleNode("//BusinessEntity");

var productnumber = user.selectSingleNode("productnumber").text;
//var productnumber = user.selectSingleNode("q1:productnumber").text; // mscrm4.0
alert(productnumber);

var price = user.selectSingleNode("price").text;
//var price = user.selectSingleNode("q1:price").text; //mscrm4.0
alert(price);



你可能感兴趣的:(web services)