SharePoint REST API - 基本操作(二)
博客地址:http://blog.csdn.net/FoxDave
上一节讲了SharePoint REST API的一些基本操作,本节将继续介绍一些关于SharePoint REST API的内容。
构建和发送HTTP请求常常会根据不同的语言、库和Add-in而产生变化,所以你需要在切换环境的时候对请求做相应的修改。例如,JQuery AJAX请求使用data和type参数来指定请求的主体和类型,但是跨域库请求使用body和method参数来指定这些值。
下面在讲一些公共的跨环境差异。
SharePoint Add-in获取和发送表单摘要认证的方式
当你发送一个POST请求时,请求必须在X-RequestDigest头中包含表单摘要认证。但是在SharePoint Add-in中则不是。
对于SharePoint承载的add-in,可以直接传递下面的头:
X-RequestDigest": $("__REQUESTDIGEST").val()
对于云承载的Add-in分两种情况:使用OAuth的,首先通过发送请求到contextinfo终结点来获取表单摘要认证的值,然后将它添加到请求中;使用JavaScript跨域库的,你不需要指定表单摘要认证的值。默认情况下,SP.RequestExecutor方法会为你自动处理它,也会处理content-length的值。
使用OAuth的SharePoint Add-ins必须在请求中传递访问令牌
云承载的Add-in使用OAuth或跨域库来授权访问SharePoint的数据。远程Web服务器执行的代码必须使用OAuth来授权访问SharePoint的数据。在这种情况下,你需要包含Authorization头来发送访问令牌。
注意用JavaScript写的云承载的Add-in组件必须使用跨域库中的SP.RequestExecutor对象来访问SharePoint数据。跨域库请求不需要包含访问令牌。
在跨域请求中使用SP.AppContextSite终结点来更改context
发送到资源终结点的请求在请求的url中被指定,使用如下格式:
_<site url>_/_api/ _<context>_/ _<resource>_ (example, https://contoso.com/_api/web/lists)
跨域库请求在访问Add-in的网站的数据时使用此种格式,是默认的上下文。但是如果要访问承载该Add-in的网站或者是其他的网站,请求需要初始化一个上下文对象。使用URI中的SP.AppContextSite端点,如下表:
| Add-in type | Cross-domain data access scenario | Example endpoint URI |
|---|---|---|
| Cloud-hosted | JavaScript add-in component accessing host web data by using the cross-domain library | /_api/SP.AppContextSite(@target)/web/lists?@target=' ' |
| Cloud-hosted | JavaScript add-in component accessing data in a site collection other than the host web by using the cross-domain library (tenant-scoped add-ins only) | /_api/SP.AppContextSite(@target)/web/title?@target=' ' |
| SharePoint-hosted | Add-in web component accessing data in another site collection (tenant-scoped add-ins only) | /_api/SP.AppContextSite(@target)/web/title?@target=' ' |
SharePoint Add-ins可以从查询字符串中获取Add-in网站的URL和承载网站的URL,下面的代码展示了如何获取。同时下面的代码也展示了如何引用在SP.RequestExecutor.js文件中定义的跨域库。
var hostweburl;
var appweburl; // Get the URLs for the add-in web the host web URL from the query string.
$(document).ready(function () {
//Get the URI decoded URLs.
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl")); // Load the SP.RequestExecutor.js file.
$.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", runCrossDomainRequest);
}); // Build and send the HTTP request.
function runCrossDomainRequest() {
var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists?@target='" + hostweburl + "'",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: successHandler,
error: errorHandler
});
} // Get a query string value.
// For production add-ins, you may want to use a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
var params = document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve) return singleParam[1];
}
}
… // success and error callback functions
REST请求中使用的属性
下表展示了通常在HTTP请求中使用的SharePoint REST服务的属性。
| Properties | When required | Description |
|---|---|---|
| url | All requests | The URL of the REST resource endpoint. Example: http://<site url>/_api/web/lists |
| method (or type) | All requests | The HTTP request method: GET for read operations and POST for write operations. POST requests can perform update or delete operations by specifying a DELETE, MERGE, or PUT verb in the X-HTTP-Method header. |
| body (or data) | POST requests that send data in the request body | The body of the POST request. Sends data (such as complex types) that can't be sent in the endpoint URI. Used with the content-length header. |
| Authentication header | Remote add-ins that are using OAuth to authenticate users. Does not apply when using JavaScript or the cross domain library. | Sends the OAuth access token (obtained from a Microsoft Access Control Service (ACS) secure token server) that's used to authenticate the user for the request. Example: "Authorization": "Bearer " + accessToken, where accessToken represents the variable that stores the token. Tokens must be retrieved by using server-side code. |
| X-RequestDigest header | POST requests (except SP.RequestExecutor requests) | Remote add-ins that use OAuth can get the form digest value from the http://<site url>/_api/contextinfo endpoint. SharePoint-hosted add-ins can get the value from the #__REQUESTDIGEST page control if it's available on the SharePoint page. See Writing data by using the REST interface. |
| accept header | Requests that return SharePoint metadata | Specifies the format for response data from the server. The default format is application/atom+xml. Example: "accept":"application/json;odata=verbose" |
| content-type header | POST requests that send data in the request body | Specifies the format of the data that the client is sending to the server. The default format is application/atom+xml. Example: "content-type":"application/json;odata=verbose" |
| content-length header | POST requests that send data in the request body (except SP.RequestExecutor requests) | Specifies the length of the content. Example: "content-length":requestBody.length |
| IF-MATCH header | POST requests for DELETE, MERGE, or PUT operations, primarily for changing lists and libraries. | Provides a way to verify that the object being changed has not been changed since it was last retrieved. Or, lets you specify to overwrite any changes, as shown in the following example: "IF-MATCH":"*" |
| X-HTTP-Method header | POST requests for DELETE, MERGE, or PUT operations | Used to specify that the request performs an update or delete operation. Example: "X-HTTP-Method":"PUT" |
| binaryStringRequestBody | SP.RequestExecutor POST requests that send binary data in the body | Specifies whether the request body is a binary string. Boolean. |
| binaryStringResponseBody | SP.RequestExecutor requests that return binary data | Specifies whether the response is a binary string. Boolean. |
SharePoint REST API - 基本操作(二)的更多相关文章
- SharePoint REST API - 基本操作(一)
博客地址:http://blog.csdn.net/FoxDave 本文讲述如何应用SharePoint的REST接口完成基本的增删查改操作. 使用SharePoint客户端API和REST服务进 ...
- TFS API:二、TFS 代码查询工作项
TFS API:二.TFS 代码查询工作项 首先我们需要认识TFS的两大获取服务对象的类. 他们分别为TfsConfigurationServer和TfsTeamProjectCollection, ...
- Windows 商店应用中使用 SharePoint REST API
前面一篇我们介绍了 Office 365 REST API 的官方工具的使用,本篇我们来看一下 SharePoint REST API 本身的描述.结构和使用方法,以及一些使用经验. 首先来看看Sha ...
- 使用IOS7原生API进行二维码条形码的扫描
使用IOS7原生API进行二维码条形码的扫描 IOS7之前,开发者进行扫码编程时,一般会借助第三方库.常用的是ZBarSDK,IOS7之后,系统的AVMetadataObject类中,为我们提供了解析 ...
- How to Call SharePoint 2013 API Service to Query The Lists
How to Call SharePoint 2013 API In SharePoint 2013, we can query the list by it owner service, then ...
- HOOK API(二)—— HOOK自己程序的 MessageBox
HOOK API(二) —— HOOK自己程序的 MessageBox 0x00 前言 以下将给出一个简单的例子,作为HOOK API的入门.这里是HOOK 自己程序的MessageBox,即将自己程 ...
- 【高德地图API】从零开始学高德JS API(二)地图控件与插件——测距、圆形编辑器、鼠标工具、地图类型切换、鹰眼鱼骨
原文:[高德地图API]从零开始学高德JS API(二)地图控件与插件——测距.圆形编辑器.鼠标工具.地图类型切换.鹰眼鱼骨 摘要:无论是控件还是插件,都是在一级API接口的基础上,进行二次开发,封装 ...
- [sharepoint]rest api文档库文件上传,下载,拷贝,剪切,删除文件,创建文件夹,修改文件夹属性,删除文件夹,获取文档列表
写在前面 最近对文档库的知识点进行了整理,也就有了这篇文章,当时查找这些接口,并用在实践中,确实废了一些功夫,也为了让更多的人走更少的弯路. 系列文章 sharepoint环境安装过程中几点需要注意的 ...
- [sharepoint]Rest api相关知识(转)
写在前面 最近又开始弄rest api了,通过sharepoint rest api获取站点信息,Items,fields非常方便,再结合OData查询,更是得心应手.这里记录学习的时候用到的知识点, ...
随机推荐
- C#特性-表达式树
表达式树ExpressionTree 表达式树基础 转载需注明出处:http://www.cnblogs.com/tianfan/ 刚接触LINQ的人往往觉得表达式树很不容易理解.通过这篇文章我希 ...
- 架构探险笔记12-安全控制框架Shiro
什么是Shiro Shiro是Apache组织下的一款轻量级Java安全框架.Spring Security相对来说比较臃肿. 官网 Shiro提供的服务 1.Authentication(认证) 2 ...
- PHP const关键字
常量是一个简单的标识符.在脚本执行期间该值不能改变(除了所谓的魔术常量,他们其实不是常量).常量默认大小写敏感.通常常量标识符总是大写的. 可以用define()函数来定义常量.在php5.3.0以后 ...
- Python操作excel的几种方式--xlrd、xlwt、openpyxl
openpyxl xlrd xlwt 在处理excel数据时发现了xlwt的局限性–不能写入超过65535行.256列的数据(因为它只支持Excel 2003及之前的版本,在这些版本的Excel中 ...
- Vue自动化工具(Vue-CLI)的安装
安装VUM 前面学习了普通组件以后,接下来我们继续学习单文件组件则需要提前先安装准备一些组件开发工具.否则无法使用和学习单文件组件. 一般情况下,单文件组件,我们运行在 自动化工具vue-CLI中,可 ...
- C# 4.0 可选参数 和 命名参数
可选参数 可选参数是 C# 4.0 提出来的,当我们调用方法,不给这个参数(可选参数)赋值时,它会使用我们定义的默认值. 需要注意的是: (1)可选参数必须位于所有必选参数的后面: (2)可选参数必须 ...
- ASP.NET controller TO view 数据传递
https://stackify.com/viewbag/ In the case of ASP.NET MVC, you have three ways to pass data from the ...
- Hive QL的操作
一.数据定义DDL操作 创建表: --create table为创建一个指定名字的表 create(external) table table_name --external关键字可以让用户创建一个外 ...
- 第2天【OS Linux发行版介绍、Linux系统基础使用入门、Linux命令帮助、Linux基础命令】
Logout 退出系统 Gedit 文本编辑器工具 Uname –r 查看内核版本信息,uname –a 比较详细 Cat /proc/cpuinfo 查看CPU Cat /p ...
- Qt Widgets——工具栏和状态栏
本文主要涉及QSizeGrip ,QStatusBar ,QToolBar QToolBar 工具栏默认位于菜单栏下方,其上添加一个个action按钮,用于执行动作 绝大多谢以前都涉及过,只列出 QT ...