SharePoint 2013的REST编程基础
1. SharePoint 2013对REST编程的支持
自从SharePoint2013开始, SharePoint开始了对REST 编程的支持,这样除了.NET , Silverlight, Powershell之外, 又多了一种可以和SharePoint Server进行CSOM编程的方式。那么,问题来了:什么是REST呢?什么是ODATA?为什么这么多产品都开始支持这个了?
2. 什么是REST & ODATA?
如果这个世界上只有一家IT公司就好了,这样就不需要REST了 :) 但是很显然,这是不可能的。不同IT公司有自己提出的技术标准及解决方案。以Microsoft为例,其实现分布式处理的技术和解决方案发展历程如下:COM -> DCOM -> COM+ ,跨入.NET时代,又出现了 Enterprise Component -> Web Service -> Web Service Enhancemant -> WCF ...
可以看到,仅仅一家公司,其提出的技术和解决方案就如此之多。在加上IBM, Google等等大鳄,就形成了各自为政,群雄混战的局面。这样的解决就是:在各自的系统和技术内,大家各自搞自己的。很显然这是不符合时代发展的 :) 比如没有任何一家公司,只用某一家IT公司的解决方案。不同公司的系统之间,也需要互相通讯。
那怎么解决这个问题呢?大家握手言和吧:制定一个基于HTTP协议的标准。HTTP协议是目前为止,最通用和成功的协议。如果某个公司说我就是不遵守HTTP协议,那他就没法转了。 因此,只要大家遵循这个标准,系统之间都可以互相通信。如果利用Microsoft的技术开发了一个服务,只要这个服务遵循这个标准,采用IBM技术的客户也可以consume这个服务,那不是很好吗? 因此这就是REST服务出现的现实需要。
言归正传,什么是REST 服务呢?
REST的全称是: Representational state transfer (有点绕口 :)), 她的基本思想是:可以通过基于HTTP协议发出的不同 操作:Create, Read, Update, and Delete (CRUD), 来实现对数据的操作。其有三大基本特征:
1) 客户端- 服务端:
所以数据都存储在服务端,客户端不知道也无需知道服务端如何处理的,只需要发送一个HTTP请求。
2) 无状态:
这个比较抽象。打个比方:
你申明一个proxy去调用服务器端的求和函数 proxy.sum(1,2) ,服务器端返回3给客户端。 当你再次调用proxy.sum(3,4)的时候,如果是无状态的话,其是不会记住你上次操作的结果的,也就是其会返回7;但是如果是有状态的操作,其会记录上一次调用的结果,并把这次结果累加再返回给客户端,也就是3+7=10.
3)可缓存的
4)分层的
5) 统一的接口
以下是关于REST的Wiki解释:
http://en.wikipedia.org/wiki/Representational_state_transfer
3. 如何实现一个RESTful服务
实现一个RESTful服务的方案有很多。以Microsoft的.NET解决方案为例, WCF3.5就开始了对REST服务的支持。你可以用WCF轻松创建一个 WCF REST API服务供其它客户端consume.
有关如何通过WCF 实现一个REST服务,可以参考下面的文档:
https://msdn.microsoft.com/en-us/magazine/dd315413.aspx
4 如何过滤和选择数据
如何让服务器返回自己真正想要的数据呢? 其实ODATA提供了丰富的数据过滤和选择操作:
1)$filter : 过滤数据
2) $select : 选择需要的字段
3) $expand: 扩展返回的属性
4) $orderby:排序
5) $top:返回前N条数据
5. SharePoint 2013对REST服务的支持
SharePoint 2013 实现了REST服务的支持,这样我们又多了一种途径可以访问SharePoint了。实际上SharePoint的REST服务是一个通过叫做client.svc的WCF REST服务来实现的。其URL为:https://sharepointserver/_vti_bin/client.svc
1) SharePoint 2013的REST服务部署的路径如下:
https://servername/sitename/_api/....
实际上上面这个地址只是其原始服务的一个别名(alias), 毕竟打出完成的client.svc有点丑陋哦。
比如:
https://winstononline.sharepoint.com/_api/...
https://winstononline.sharepoint.com/sites/spdev/...
2) SharePoint 2013支持的命名空间:
要对SharePoint 2013发出一个REST请求,必须指明你所要访问的namespace。支持的namespace包括:
a) _api/web
b) _api/site
c) _api/search
d) _api/publishing
例如: 我想返回一个叫做Doctest的list里面ID为3的item,并且只返回ID字段:
http://winstononline.sharepoint.com/_api/web/lists/Doctest/items?$filter=ID eq 3 & $select=ID
又如:
https://winstononline.sharepoint.com/_api/web/lists/Doctest/items?$select=ID & $orderby=ID
<?xml version="1.0" encoding="utf-8" ?> - <feed xml:base="https://winstononline.sharepoint.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"> <id>b3c0e660-6937-4da9-b1cf-c1bd6613be1a</id> <title /> <updated>2015-02-04T07:22:31Z</updated> - <entry m:etag=""3""> <id>bccd25b6-c6cf-4748-a75b-aca5b6826fe8</id> <category term="SP.Data.DoctestItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <link rel="edit" href="Web/Lists(guid'906b7bcc-bd4f-4d8c-9e46-6e01b5579d5f')/Items(1)" /> <title /> <updated>2015-02-04T07:22:31Z</updated> - <author> <name /> </author> - <content type="application/xml"> - <m:properties> <d:Id m:type="Edm.Int32">1</d:Id> <d:ID m:type="Edm.Int32">1</d:ID> </m:properties> </content> </entry> - <entry m:etag=""4""> <id>7604db11-97fa-4433-b652-51eb70b013d9</id> <category term="SP.Data.DoctestItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <link rel="edit" href="Web/Lists(guid'906b7bcc-bd4f-4d8c-9e46-6e01b5579d5f')/Items(2)" /> <title /> <updated>2015-02-04T07:22:31Z</updated> - <author> <name /> </author> - <content type="application/xml"> - <m:properties> <d:Id m:type="Edm.Int32">2</d:Id> <d:ID m:type="Edm.Int32">2</d:ID> </m:properties> </content> </entry> - <entry m:etag=""2""> <id>ec2a5b46-ae8f-4334-8a85-9a777660021c</id> <category term="SP.Data.DoctestItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <link rel="edit" href="Web/Lists(guid'906b7bcc-bd4f-4d8c-9e46-6e01b5579d5f')/Items(3)" /> <title /> <updated>2015-02-04T07:22:31Z</updated> - <author> <name /> </author> - <content type="application/xml"> - <m:properties> <d:Id m:type="Edm.Int32">3</d:Id> <d:ID m:type="Edm.Int32">3</d:ID> </m:properties> </content> </entry> </feed>
6. SharePoint客户端技术对REST服务的支持
如果采用上面发送REST请求的方法来和SharePoint进行交互,可以很容易的执行各种操作。但是唯一的确定是没法进行批量化的操作。为了让客户端采用javascript来调用SharePoint REST的时候更加友好和强大,SharePoint提供了2个javascript类库,分别为sp.js和sp.runtime.js, 其位于_layouts/15/###.js下。与开发者相关的,更多的是sp.js里面的定义。这样所有的REST操作都被封装在sp.js里定义的API 里面,显得更加友好。
下面是通过Napa创建的一个default sharePoint App, 里面就用到了SP.js。
<%-- The following 4 lines are ASP.NET directives needed when using SharePoint components --%>
<%@ Page Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" MasterPageFile="~masterurl/default.master" Language="C#" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%-- The markup and script in the following Content element will be placed in the <head> of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script> <!-- Add your CSS styles to the following file -->
<link rel="Stylesheet" type="text/css" href="../Content/App.css" /> <!-- Add your JavaScript to the following file -->
<script type="text/javascript" src="../Scripts/App.js"></script>
</asp:Content> <%-- The markup in the following Content element will be placed in the TitleArea of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
Page Title
</asp:Content> <%-- The markup and script in the following Content element will be placed in the <body> of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server"> <div>
<p id="message">
<!-- The following content will be replaced with the user name when you run the app - see App.js -->
initializing...
</p>
</div> </asp:Content> <!--app.js--> var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser(); (function () { // This code runs when the DOM is ready and creates a context object which is
// needed to use the SharePoint object model
$(document).ready(function () {
getUserName();
}); // This function prepares, loads, and then executes a SharePoint query to get
// the current users information
function getUserName() {
context.load(user);
context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
} // This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function onGetUserNameSuccess() {
$('#message').text('Hello ' + user.get_title() +"This is my first Napa app");
} // This function is executed if the above call fails
function onGetUserNameFail(sender, args) {
alert('Failed to get user name. Error:' + args.get_message());
} })(); function getParameterByName(name)
{ }
6. 调用SharePoint REST服务示例
未完待续...
SharePoint 2013的REST编程基础的更多相关文章
- SharePoint 2013 REST 以及 OData 基础
这篇文章会介绍: 简单的介绍REST,OData OData实现细节 OData在SharePoint 2013中的实现 为什么REST很重要 过去几年基于REST的webservice在IT企业越来 ...
- 在 SharePoint 2013 中针对地理位置字段创建地图视图
在 SharePoint 2013 中针对地理位置字段创建地图视图 了解如何通过在 SharePoint 2013 列表中使用地图视图来显示位置信息.您可以通过 SharePoint 用户界面 (UI ...
- [MSDN] 使用 SharePoint 2013 中的 JavaScript 库代码完成基本操作
MSDN:http://msdn.microsoft.com/zh-cn/library/jj163201.aspx 了解如何编写代码以在 SharePoint 2013 中使用 JavaScript ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第1章节--SharePoint 2013 介绍 SharePoint 2013 平台
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第1章节--SharePoint 2013 介绍 SharePoint 2013 平台 SharePoin ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第1章节--SharePoint 2013 介绍 处理开发者需求
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第1章节--SharePoint 2013 介绍 处理开发者需求 SharePoint本质上是一个平台.你 ...
- SharePoint 2013 create workflow by SharePoint Designer 2013
这篇文章主要基于上一篇http://www.cnblogs.com/qindy/p/6242714.html的基础上,create a sample workflow by SharePoint De ...
- 实现一个基于 SharePoint 2013 的 Timecard 应用(上)
在 SharePoint 2013 上面实现一个 Timecard 应用的想法来自一个真实的需求,而实现的方案在我脑海里面盘旋已经很久了,终于这几天准备安排点儿时间将它实现出来. “ We start ...
- SharePoint 2013 入门教程
以下文章是自己在学习SharePoint的过程中,不断积累和总结的博文,现在总结一个目录,分享给大家.这个博客也是自己从SharePoint入门,到一个SharePoint开发的成长记录,里面记录的都 ...
- SharePoint 2013 托管导航 无法被开启的解决办法
在阅读了园子中霖雨的一片博文<SharePoint 2013 托管导航及相关配置>之后,非常想尝试一下SharePoint 2013 中的这个新功能,但是我的网站集包括样式是从2010升级 ...
随机推荐
- 深夜配置一把struts2
在intellij idea里面配置出来了struts2的一个Helloworld,因为换了工具,在网上查了很多关于IDEA配置它的方式,好多是用Maven解决依赖关系的.于是按照网上的来,发现很多东 ...
- XEvent--基础
--SQL Server 扩展事件具有高度可伸缩且高度可配置的体系结构,--使用户能够按需收集解决性能问题或确定性能问题所需的信息.--1. 性能损耗小--2. 可配置性高--3. 可捕获底层事件 - ...
- Kafka与.net core(一)安装
1.安装JDK 目前官网不能直接下载,在网上找到1.8.0版本安装包下载到本地. 1.1.下载jdk并解压 [root@iz2zei2y693gtrgwlibzlwz java]# ls jdk1.. ...
- mac下wordpress环境搭建
mac下本来就有apache和php,只需要配置以下+安装mysql 1.Apache 预装目录在 /etc/apache2: 默认的网站目录在 /Library/WebServer/Documen ...
- ajax1—php(27)
一 简介 web程序工作原理图: 传统的web程序工作原理图: Ajax工作原理图: 1. 关于Ajax l Asynchronous 异步 l JavaScript l And l XML ...
- 《快学Scala》第二章 控制结构和函数
- 服务器变量 $_SERVER 详解
摘抄:http://ha.cker.in/942.seo 1.$_SESSION[‘PHP_SELF’] — 获取当前正在执行脚本的文件名 2.$_SERVER[‘SERVER_PROTOCOL’] ...
- PHP性能优化四(业务逻辑中使用场景)
php脚本性能,很多时候依赖于你的php版本.你的web server环境和你的代码的复杂度. Hoare曾经说过“过早优化是一切不幸的根源”.当你想要让你的网站更快运转的时候,你才应该去做优化的事情 ...
- SpringBoot idea maven打包war及运行war包
pom.xml修改打包类型pom改为war <artifactId>Test02</artifactId> <packaging>war</packaging ...
- Modbus通用数据读取工具设计及使用
一.公共功能码定义 二.能读取的数据类型 1.bit类型,比如01功能码,读到的就是位的状态,是ON 还是OFF,也就是对应着0或1. 2.byte类型,比如03功能码. 3.short类型,比如03 ...