首页 > 代码库 > crm使用soap创建下拉框
crm使用soap创建下拉框
//C#代码
//#region OptionMetadataCollection
//OptionMetadataCollection opCollection = new OptionMetadataCollection();
//opCollection.Add(new OptionMetadata(new Label("2000年", languageCode), 2000));
//opCollection.Add(new OptionMetadata(new Label("2001年", languageCode), 2001));
//opCollection.Add(new OptionMetadata(new Label("2002年", languageCode), 2002));
//opCollection.Add(new OptionMetadata(new Label("2003年", languageCode), 2003));
//opCollection.Add(new OptionMetadata(new Label("2004年", languageCode), 2004));
//opCollection.Add(new OptionMetadata(new Label("2005年", languageCode), 2005));
//#endregion
//OptionSetMetadata optionSet = new OptionSetMetadata(opCollection);
//optionSet.Name = "new_year";
//optionSet.Description = new Label("年份", languageCode);
//optionSet.DisplayName = new Label("年份", languageCode);
//optionSet.IsGlobal = true;
//optionSet.OptionSetType = OptionSetType.Picklist;
//CreateOptionSetRequest request = new CreateOptionSetRequest();
//request.OptionSet = optionSet;
//CreateOptionSetResponse response = (CreateOptionSetResponse)service.Execute(request);
//例子
function demo() {
//字段名称
var attrName = "new_year";
//标签
var labelName = "年份";
//语言编码
var code = 2052;
//集合
var dataArray = [];
dataArray.push(getOptionItem("2000年", 2000));
dataArray.push(getOptionItem("2001年", 2001));
dataArray.push(getOptionItem("2002年", 2002));
dataArray.push(getOptionItem("2003年", 2003));
dataArray.push(getOptionItem("2004年", 2004));
dataArray.push(getOptionItem("2005年", 2005));
dataArray.push(getOptionItem("2006年", 2006));
createOption(attrName, labelName, code, dataArray);
}
function getOptionItem(name, value) {
var item = new Object();
item.name = name;
item.value = http://www.mamicode.com/value;
return item;
}
function createOption(attrName,labelName,code,dataArray) {
var request = "<s:Envelope xmlns:s=‘http://schemas.xmlsoap.org/soap/envelope/‘>";
request += "<s:Body>";
request += "<Execute xmlns=‘http://schemas.microsoft.com/xrm/2011/Contracts/Services‘ xmlns:i=‘http://www.w3.org/2001/XMLSchema-instance‘>";
request += "<request i:type=‘a:CreateOptionSetRequest‘ xmlns:a=‘http://schemas.microsoft.com/xrm/2011/Contracts‘>";
request += "<a:Parameters xmlns:b=‘http://schemas.datacontract.org/2004/07/System.Collections.Generic‘>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<b:key>OptionSet</b:key>";
request += "<b:value i:type=‘c:OptionSetMetadata‘ xmlns:c=‘http://schemas.microsoft.com/xrm/2011/Metadata‘>";
request += "<c:MetadataId i:nil=‘true‘ />";
request += "<c:HasChanged i:nil=‘true‘ />";
request += "<c:Description>";
request += "<a:LocalizedLabels>";
request += "<a:LocalizedLabel>";
request += "<c:MetadataId i:nil=‘true‘ />";
request += "<c:HasChanged i:nil=‘true‘ />";
request += "<a:IsManaged i:nil=‘true‘ />";
request += "<a:Label>"+ labelName +"</a:Label>";
request += "<a:LanguageCode>2052</a:LanguageCode>";
request += "</a:LocalizedLabel>";
request += "</a:LocalizedLabels>";
request += "<a:UserLocalizedLabel i:nil=‘true‘ />";
request += "</c:Description>";
request += "<c:DisplayName>";
request += "<a:LocalizedLabels>";
request += "<a:LocalizedLabel>";
request += "<c:MetadataId i:nil=‘true‘ />";
request += "<c:HasChanged i:nil=‘true‘ />";
request += "<a:IsManaged i:nil=‘true‘ />";
request += "<a:Label>"+ labelName +"</a:Label>";
request += "<a:LanguageCode>2052</a:LanguageCode>";
request += "</a:LocalizedLabel>";
request += "</a:LocalizedLabels>";
request += "<a:UserLocalizedLabel i:nil=‘true‘ />";
request += " </c:DisplayName>";
request += "<c:IsCustomOptionSet i:nil=‘true‘ />";
request += "<c:IsCustomizable i:nil=‘true‘ />";
request += "<c:IsGlobal>true</c:IsGlobal>";
request += "<c:IsManaged i:nil=‘true‘ />";
request += "<c:Name>"+ attrName +"</c:Name>";
request += "<c:OptionSetType>Picklist</c:OptionSetType>";
request += "<c:Options> ";
//添加具体的每一小项
if (dataArray != null && dataArray.length > 0) {
var len = dataArray.length;
for (var i = 0; i < len; i++) {
var item = dataArray[i];
request += getItem(item.name, item.value, code);
}
}
request += "</c:Options>";
request += "</b:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "</a:Parameters>";
request += "<a:RequestId i:nil=‘true‘ />";
request += "<a:RequestName>CreateOptionSet</a:RequestName>";
request += "</request>";
request += "</Execute>";
request += "</s:Body>";
request += "</s:Envelope>";
execSoap(request);
}
function getItem(name,value,code) {
var request = "<c:OptionMetadata>";
request += "<c:MetadataId i:nil=‘true‘ />";
request += "<c:HasChanged i:nil=‘true‘ />";
request += "<c:Description i:nil=‘true‘ />";
request += "<a:IsManaged i:nil=‘true‘ />";
request += "<c:Label>";
request += "<a:LocalizedLabels>";
request += "<a:LocalizedLabel>";
request += "<c:MetadataId i:nil=‘true‘ />";
request += "<c:HasChanged i:nil=‘true‘ />";
request += "<a:IsManaged i:nil=‘true‘ />";
request += "<a:Label>" + name + "</a:Label>";
request += "<a:LanguageCode>"+ code +"</a:LanguageCode>";
request += "</a:LocalizedLabel>";
request += "</a:LocalizedLabels>";
request += "<a:UserLocalizedLabel i:nil=‘true‘ />";
request += "</c:Label>";
request += "<c:Value>"+ value +"</c:Value>";
request += "</c:OptionMetadata>";
return request;
}
//获取服务地址
function getWebUrl() {
var serverUrl = Xrm.Page.context.getServerUrl();
if (serverUrl.match(/\/$/)) {
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
return serverUrl + "/XRMServices/2011/Organization.svc/web";
}
//执行请求
function execSoap(request) {
var ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("POST", getWebUrl(), true)
ajaxRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
ajaxRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
ajaxRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
ajaxRequest.send(request);
}