第一种
js
var json = {};
json["EntityId"] = Xrm.Page.data.entity.getId();
json["EntityName"] = Xrm.Page.data.entity.getEntityName();
json["UserId"] = Xrm.Page.context.getUserId();
json["Name"] = "1";
var params = {};
params["json"] = JSON.stringify(json);
var request = {};
request["action"] = "ew_OutboundDeliveryAction";
request["params"] = params;
PostAction(request, function (data) {
var res = JSON.parse(data.value);
if (res.code == 0) {
alert(res.msg);
Xrm.Page.data.save().then(function () { parent.window.location.reload(); });
} else {
alert(res.msg);
}
}, function (err) {
alert(err.message);
});
/**
* @param {any} data 参数
* @param {any} callback 成功返回方法
* @param {any} errorcallback 失败返回方法
*/
function PostAction(data, callback, errorcallback, async = false) {
var requestURL = Xrm.Page.context.prependOrgName("/api/data/v9.0/" + data.action);
var req = new XMLHttpRequest()
req.open("post", encodeURI(requestURL), async);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status == 200) {
callback(JSON.parse(this.responseText));
} else {
errorcallback(JSON.parse(this.responseText));
}
}
};
req.send(window.JSON.stringify(data.params));
}
C#
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ride.Crm.Order
{
public class Order : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService serviceAdmin = serviceFactory.CreateOrganizationService(null);
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.Depth > 2) return;
try
{
string jsons = context.InputParameters["json"] as string;
var json = JsonConvert.DeserializeObject<Josn>(jsons);
var entity = serviceAdmin.Retrieve(json.EntityName, json.EntityId, new ColumnSet(true));
var value = "Success";
switch (json.Name)
{
case "1"://
Entity entity1 = new Entity(entity.LogicalName, entity.Id);
entity1["status"] = new OptionSetValue(20);
service.Update(entity1);
break;
}
context.OutputParameters["value"] = JsonConvert.SerializeObject(new { code = 0, msg = value });
}
catch (Exception ex)
{
context.OutputParameters["value"] = JsonConvert.SerializeObject(new { code = -1, msg = ex.Message });
throw new Exception(ex.Message);
}
}
public class Josn
{
public Guid UserId { get; set; }
public Guid EntityId { get; set; }
public string EntityName { get; set; }
public string Name { get; set; }
}
}
}
第二种
js
let url = Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_DOCX";
let data = {
json: Xrm.Page.data.entity.getId().replace('{', '').replace('}', '')//实体id;
};
let res = window.YWRequest(url, JSON.stringify(data), 'post');
if (res.value) {
alert("成功");
} else {
alert("失败");
}
/**
* Ajax请求
* @param {*} url
* @param {*} data
* @param {*} type
*/
function YWRequest(url, data = null, type = "get", async = false) {
let response;
$.ajax({
url: url,
type: type,
dataType: "json",
data: data,
async: async, // 同步
contentType: "application/json; charset=utf-8",
success: function (res) {
response = res;
}, error: function (error) {
console.error(error.responseJSON.error.message);
}
});
return response;
}
C#
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService serviceAdmin = serviceFactory.CreateOrganizationService(null);
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.Depth > 2) return;
try
{
string json = context.InputParameters["json"] as string;
var entity = serviceAdmin.Retrieve("xxx", Guid.Parse(json), new ColumnSet(true));
context.OutputParameters["value"] = "";
}
catch (Exception ex)
{
context.OutputParameters["value"] = ex.Message;
}
}
private class EntityJosn
{
/// <summary>
/// 当前用户id
/// </summary>
public Guid UserId { get; set; }
/// <summary>
/// 实体id
/// </summary>
public Guid EntityId { get; set; }
/// <summary>
/// 实体名
/// </summary>
public string EntityName { get; set; }
}
第三种
js
var entityId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");
var parameters = {
"Type": "xxxx",
"Data": entityId
};
RequestAction("ew_QuoteApplicationAction", parameters, true, function (res)
{
//var data = JSON.parse(res);
console.log(res);
});
/**
* 执行Action
* @param {any} actionName Action名字
* @param {any} data 参数
* @param {any} async 同步异步
* @param {any} success 成功执行方法
* @param {any} fail 失败执行方法
*/
function RequestAction(actionName, data, async, success)
{
var request = {
actionName: actionName,
parameters: data
};
var options = {
async: async
};
Xrm.WebApi.online.execute(request, options).then(success, function (res)
{
console.error(res);
});
}
C#
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService serviceAdmin = serviceFactory.CreateOrganizationService(null);
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.Depth > 2) return;
try
{
string json = context.InputParameters["json"] as string;
var entity = serviceAdmin.Retrieve("xxxxx", Guid.Parse(json), new ColumnSet(true));
context.OutputParameters["value"] = "";
}
catch (Exception ex)
{
context.OutputParameters["value"] = ex.Message;
}
}
private class EntityJosn
{
/// <summary>
/// 当前用户id
/// </summary>
public Guid UserId { get; set; }
/// <summary>
/// 实体id
/// </summary>
public Guid EntityId { get; set; }
/// <summary>
/// 实体名
/// </summary>
public string EntityName { get; set; }
}