Dynamics 365 for Sales: Web API Client编程辅助类
来源:互联网 发布:烟台龙口淘宝招聘网 编辑:程序博客网 时间:2024/06/06 00:14
最近博主写了一个Web API Client端的编程辅助类,我们可以用它在客户端层面调用Web API 完成CRUD操作。辅助类的Ajax请求是基于Jquery完成的,也就是说,大家在使用这个类的时候需要引用JQUERY,具体情况如下图所示:
JSHelper
var WEB_API_VERSION = "8.1";var JQUERY_URL = "/WebResources/new_jquery.1.11.js";function XrmHelper(){ var _clientUrl = Xrm.Page.context.getClientUrl(); this._webApiUrl = _clientUrl + "/api/data/v" + WEB_API_VERSION + "/";}XrmHelper.prototype.Create = function (url, content) { var data = null; $.ajax({ headers: { Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': content != null ? content.length : 0 }, type: "POST", url: url.indexOf("http") == -1 ? this._webApiUrl + url : url, async: false, cache: false, data:content, complete: function (context) { if (context.status>=200 && context.status<300) { try { data = context.getResponseHeader("OData-EntityId"); } catch (e) { } } } });//ajax return data;};XrmHelper.prototype.CreateAsync = function (url, content, callbackHandler) { $.ajax({ headers: { Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': content != null ? content.length : 0 }, type: "POST", url: url.indexOf("http") == -1 ? this._webApiUrl + url : url, async: false, cache: false, data: content, complete: function (context,result) { if (context.status >= 200 && context.status < 300) { try { //data = $.parseJSON(context.responseText).d; callbackHandler(context.getResponseHeader("OData-EntityId")); } catch (e) { } } } });//ajax};XrmHelper.prototype.Read = function (url) { var data = null; $.ajax({ headers: { Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8'}, type: "GET", url: url.indexOf("http")==-1? this._webApiUrl + url:url, async: false, cache: true, complete: function (context, result) { if (context.status >= 200 && context.status < 300) { try { data = context.responseJSON; } catch (e) { } } } });//ajax return data;};XrmHelper.prototype.ReadAsync = function (url, callbackHandler) { $.ajax({ headers: { Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8'}, type: "GET", url: url.indexOf("http") == -1 ? this._webApiUrl + url : url, async: false, cache: true, complete: function (context, result) { if (context.status >= 200 && context.status < 300) { try { //data = $.parseJSON(context.responseText).d; callbackHandler(context.responseJSON); } catch (e) { } } } });//ajax};XrmHelper.prototype.Update = function (url, content) { var data = null; $.ajax({ headers: { Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': content != null ? content.length : 0 }, type: "PATCH", url: url.indexOf("http") == -1 ? this._webApiUrl + url : url, async: false, cache: false, data:content, complete: function (context, result) { if (context.status >= 200 && context.status < 300) { try { data = result; } catch (e) { } } } });//ajax return data;};XrmHelper.prototype.UpdateAsync = function (url, content, callbackHandler) { $.ajax({ headers: { Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': content != null ? content.length : 0 }, type: "PATCH", url: url.indexOf("http") == -1 ? this._webApiUrl + url : url, async: false, cache: false, data:content, complete: function (context, result) { if (context.status >= 200 && context.status < 300) { try { callbackHandler(result); } catch (e) { } } } });//ajax};XrmHelper.prototype.Delete = function (url) { var data = false; $.ajax({ headers: { Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8'}, type: "DELETE", url: url.indexOf("http") == -1 ? this._webApiUrl + url : url, async: false, cache: false, complete: function (context, result) { if (context.status >= 200 && context.status < 300) { try { data = true; } catch (e) { } } } });//ajax return data;};XrmHelper.prototype.DeleteAsync = function (url, callbackHandler) { $.ajax({ headers: { Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8' }, type: "DELETE", url: url.indexOf("http") == -1 ? this._webApiUrl + url : url, async: false, cache: false, complete: function (context, result) { if (context.status >= 200 && context.status < 300) { try { callbackHandler(true); } catch (e) { } } else { callbackHandler(false); } } });//ajax};XrmHelper.prototype.ExecuteAPI = function (method,url, content) { var data = null; $.ajax({ headers: { Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': content != null ? content.length : 0 }, type: method, url: url.indexOf("http") == -1 ? this._webApiUrl + url : url, async: false, cache: false, data:content, complete: function (context, result) { if (context.status >= 200 && context.status < 300) { try { if (context.responseJSON != null) { data = context.responseJSON; } else{ data=result; } } catch (e) { } } } });//ajax return data;};XrmHelper.prototype.ExecuteAPIAsync = function (method,url, content, callbackHandler) { $.ajax({ headers: { Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': content != null ? content.length : 0 }, type: method, url: url.indexOf("http") == -1 ? this._webApiUrl + url : url, async: false, cache: false, data:content, complete: function (context, result) { if (context.status >= 200 && context.status < 300) { try { var data = null; if (context.responseJSON != null) { data = context.responseJSON; } else { data = result; } callbackHandler(data); } catch (e) { } } } });//ajax};
function xrmHelperUT() { debugger; //alert('onload'); var xrmHelper = new XrmHelper(); //test code xrmHelper.Create("contacts", { "firstname": "hello", "lastname": "webapi" }); //create account var entityUrl = xrmHelper.Create("accounts", "{'name':'create test account sync'}"); console.log(entityUrl + "create sync\n"); var entityAsyncUrl = null; xrmHelper.CreateAsync("accounts", "{'name':'create test account async'}", function (result) { entityAsyncUrl = result; console.log(result+ "create async\n") }); //read account var createdAcc = xrmHelper.Read(entityUrl); console.log(createdAcc.name+" Read \n"); var createdAccAsync = null; xrmHelper.ReadAsync(entityAsyncUrl, function (result) { console.log(result.name + " Read Async\n"); }); //update account var uptsyncObj = "{ 'telephone1': '15000303158' }"; xrmHelper.Update(entityUrl, uptsyncObj); console.log("update\n"); var uptAsyncObj = "{ 'telephone1': '15000303155' }"; xrmHelper.UpdateAsync(entityAsyncUrl, uptAsyncObj, function (c, r) { console.log("update async\n"); }) //delete account var isDeleted=xrmHelper.Delete(entityUrl); console.log("delete "+isDeleted+" \n"); xrmHelper.DeleteAsync(entityAsyncUrl, function (isDeleted) { console.log("delete " + isDeleted + " \n"); }) //execute API //who am I var userInfo=xrmHelper.ExecuteAPI("GET", "WhoAmI()", null); xrmHelper.ExecuteAPIAsync("GET", "WhoAmI()", null, function (r) { if (r != null) { console.log(r.UserId); } });}
1 0
- Dynamics 365 for Sales: Web API Client编程辅助类
- Dynamics 365 for Sales: Email Engagement
- Dynamics 365 for Sales: 门户的集成配置
- Dynamics 365 for Sales: 门户网页模板的制作
- Dynamics 365 for Sales:轻松搭建自己的企业门户
- Dynamics CRM 2011 编程系列 (8):JS编程之实体CRUD辅助类
- Dynamics CRM 2011编程系列(60):JS编程之CRUD辅助类(JQuery版)
- Dynamics CRM 2015/2016/365 Web API:用户模拟
- Dynamics 365Online 查询Web Api的请求WebUri
- Dynamics CRM 2015/2016 Web API:简介
- Dynamics CRM 2016 Web API 消息列表
- Dynamics CRM2016 Web API之创建记录
- Dynamics CRM2016 Web API之更新记录
- Dynamics CRM2016 Web API之删除
- Dynamics CRM2016 Web API之Retrieve Multiple
- Dynamics CRM2016 Web Api之分页查询
- Java Client API for RabbitMQ
- Dynamics CRM 2011:Client Form 编程参考文档
- web对象page、request、session、application的生命周期
- 【C++】 C++中的内联函数inline总结
- 用java模拟斗地主洗牌和发牌
- Android手势监听类GestureDetector的使用
- iOS 去掉tableview的cell线
- Dynamics 365 for Sales: Web API Client编程辅助类
- AJAX--异步校验实现
- 人,物,钱的理解
- mysql将日期转换为星期的三种方法
- HTML5 LocalStorage 本地存储JSON数据
- Java WebService_cxf (1) 入门案例
- ubuntu 12.04 系统密码破译
- JS实现JSON.stringify
- 黑客的攻击(UVA 11825)