var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var data = '<?xml version="1.0" encoding="utf-8"?>';
data = data + '<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/">';
data = data + '<soap:Body>';
data = data + '<GetPicklist xmlns="http://tempuri.org/">';
data = data + '</GetPicklist>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';
var URL="/CustomServices/DynamicPicklist.asmx";
xmlhttp.open("POST", URL, false);
xmlhttp.setRequestHeader("Content-Type", "text/xml");
xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/GetPicklist");
xmlhttp.setRequestHeader("Length",data.length);
// Send the query.
xmlhttp.send(data);
// Get the responses.
var rx = xmlhttp.ResponseXML;
var dom=new ActiveXObject("Microsoft.XMLDOM");
dom.async="false"
dom.loadXML(rx.text);
var results = dom.selectNodes("//Table");
//Loop through the results generating List Item elements.
var oField=crmForm.all.new_payterm;
var optiondatas = document.all.new_payterm_d.getElementsByTagName("Option");
var n_results = results.length;
for (i=0; i<n_results; i++)
{
var Result=results.item(i)
optiondatas(i+1).text=Result.selectSingleNode("PicklistName").text;
}
for (i=n_results; i<=optiondatas.length; i++)
{
oField.DeleteOption(i+1);
}
C#的代碼部份
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
using
System.Web.Services.Protocols;
using
System.Xml.Linq;
using
Microsoft.Crm.Sdk;
using
Microsoft.Crm.SdkTypeProxy;
namespace
DynamicPicklist.CustomServices
{
using
System.Data.OleDb;
///
<summary>
///
Summary description for DynamicPicklist
///
</summary>
[WebService(Namespace
=
"
http://tempuri.org/
"
)]
[WebServiceBinding(ConformsTo
=
WsiProfiles.BasicProfile1_1)]
[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
DynamicPicklist : System.Web.Services.WebService
{
[WebMethod]
public
string
GetPicklist(
string
strCn)
{
//string strCn = @"Data Source=" + datasource + ";Initial Catalog="+database +";Integrated Security=True";
//
System.Configuration.ConfigurationManager.ConnectionStrings["SQLSeverTest"].ToString ();
OleDbConnection cn
=
new
OleDbConnection(strCn);
cn.Open();
OleDbCommand cmd
=
new
OleDbCommand();
cmd.CommandText
=
"
SELECT PicklistId, PicklistName FROM Picklist
"
;
cmd.CommandType
=
CommandType.Text;
cmd.Connection
=
cn;
//
OleDbDataReader reader = cmd.ExecuteReader();
//
System.IO.StringWriter sw=new System.IO.StringWriter ();
OleDbDataAdapter da
=
new
OleDbDataAdapter(cmd);
DataSet ds
=
new
DataSet(
"
Picklist
"
);
da.Fill(ds);
return
ds.GetXml();
}
}
}