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 = 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);
}





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5224094.html,如需转载请自行联系原作者

相关文章
详解Vue3——#default=“scope”
详解Vue3——#default=“scope”
1244 0
|
Docker 容器
多功能秒达工具箱全开源源码,可自部署且完全开源的中文工具箱
多功能秒达开源工具箱源码,,可自部署且完全开源的中文工具箱,永远的自由软件,轻量级运行,全平台支持(包括ARMv8),完全类似 GPT 的支持,与高效的 UI 高度集成,可用的 Docker 映像和便携式版本,桌面版支持,开源插件库。
374 5
多功能秒达工具箱全开源源码,可自部署且完全开源的中文工具箱
|
存储 分布式计算 Serverless
阿里云 EMR Serverless Spark 版开启免费公测
EMR Serverless Spark 版免费公测已开启,预计于2024年06月25日结束。公测阶段面向所有用户开放,您可以免费试用。
1665 5
idea 2020.2及2020.3版本的安装和激活
idea 2020.2及2020.3版本的安装和激活
8918 3
|
Oracle 关系型数据库 中间件
|
机器学习/深度学习 数据采集 搜索推荐
10种数据分析的模型思维让你“灵光一闪”
推荐10种数据分析思维,让你在工作中带来“灵光一闪”的感觉 本文来源于阿里开发者公众号
1031 0
[MFC] A对话框调用B对话框
[MFC] A对话框调用B对话框
122 0
|
C语言
【C语言】数据类型
该文章简单的介绍了C语言的数据类型,学习了这篇文章可以让你学到数据类型的前世今生,加油探险家。
|
关系型数据库 MySQL 应用服务中间件
Docker 实战——部署 Nginx 镜像容器、Tomcat 镜像容器、MySQL 镜像容器
Docker 实战——部署 Nginx 镜像容器、Tomcat 镜像容器、MySQL 镜像容器
1271 0
Docker 实战——部署 Nginx 镜像容器、Tomcat 镜像容器、MySQL 镜像容器
|
存储 测试技术 开发工具
git【报错】this exceeds GitHub‘s file size limit of 100.00 MB
git【报错】this exceeds GitHub‘s file size limit of 100.00 MB