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 informationfunction 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 namefunction onGetUserNameSuccess() {$('#message').text('Hello ' + user.get_title() +"This is my first Napa app");}// This function is executed if the above call failsfunction onGetUserNameFail(sender, args) {alert('Failed to get user name. Error:' + args.get_message());}})();function getParameterByName(name)
{}

6. 调用SharePoint REST服务示例

未完待续...

SharePoint 2013的REST编程基础相关推荐

  1. SharePoint 2013 基础版 离线安装步骤

    SharePoint 2013 基础版 离线安装步骤 1.    介绍 SharePoint 2013 提供了一个企业的业务解决方案,它利用了企业应用程序集成功能,以及灵活的部署选项和管理工具,将来自 ...

  2. BEGINNING SHAREPOINT#174; 2013 DEVELOPMENT 第1章节--SharePoint 2013 介绍 SharePoint 2013 平台...

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第1章节--SharePoint 2013 介绍 SharePoint 2013 平台 SharePoint保持了一个高级 ...

  3. SharePoint 2013 中的新增功能(与开发有关)

    了解 SharePoint 2013 中的新增特性和功能,包括新的云应用程序模型.开发工具.平台增强功能.移动应用程序以及更多其他功能. 适用范围: 云应用程序模型 SharePoint 2013 引 ...

  4. SharePoint 2013中规划企业搜索体系结构

    SharePoint 2013中规划企业搜索体系结构 摘要:了解如何规划小型.中型或大型企业搜索体系结构. 设置企业搜索体系结构之前,需要仔细规划很多事项.我们将逐步帮助您规划小型.中型或大型企业搜索 ...

  5. python编程基础与应用-有哪些适合零编程基础的人学习Python的书?

    筛选了2年内优秀的python书籍,个别经典的书籍扩展到5年内. python现在的主流版本是3.7(有明显性能提升,强烈推荐) 3.6, 不基于这两个或者更新版本的书,慎重选择.很多库已经不提供py ...

  6. python编程基础与应用-Python程序设计:从编程基础到专业应用

    基本信息 书名:Python程序设计:从编程基础到专业应用 :42.00元 作者:章宁,李海峰 编 出版社:机械工业出版社 出版日期:2019_03_01 ISBN:9787111620136 字数: ...

  7. Linux Shell编程基础

    linux系统下给命令指定别名alias命令用法: 在linux系统中如果命令太长又不符合用户的习惯,那么我们可以为它指定一个别名.虽然可以为命令建立"链接"解决长文件名的问题,但 ...

  8. 《Python编程快速上手——让繁琐工作自动化》——第一部分 Python编程基础 第1章 Python基础 1.1 在交互式环境中输入表达式...

    本节书摘来自异步社区<Python编程快速上手--让繁琐工作自动化>一书中的第1章,第1.1节,作者[美] Al Sweigart,王海鹏 译,更多章节内容可以访问云栖社区"异步 ...

  9. SharePoint 2013 图文开发系列之WebPart

    这是我们介绍SharePoint开发入门的第一篇,在这一篇里,我们会介绍SharePoint开发的几个关键物理路径,一些开发技巧和最基础的WebPart开发. 开发工具 在SharePoint 201 ...

最新文章

  1. jar包又冲突了?如何快速确定与哪个jar包冲突?
  2. Neuron 2020 | 深度强化学习和神经科学
  3. oracle java api_oracle java api
  4. HTML和JavaScript代码分离、平稳退化(1)
  5. 【c++】映射表std::map
  6. excel打开空白_PDF转成Excel后,打开表格却是空白的?
  7. 实验图文详解——apache的编译安装及httpd服务开机自启
  8. el-popover超过固定高度后出现滚动条_「测绘精选」RTK测量不出现固定解的原因...
  9. 八数码——路径寻找问题
  10. php一对一模型关联,通过实例学习Laravel模型中的一对一关联关系
  11. php环形链表,PHP环形链表实现方法示例
  12. day23 内置函数,匿名函数,递归
  13. 嘉楠勘智 K210 RISC-V 64位双核处理器开发板(荔枝丹)
  14. 索引、视图和同义词、序列
  15. linux yast命令,Linux_SUSE Linux系统上双网卡绑定方法,1、使用yast工具配置第一块网 - phpStudy...
  16. 音频soundpool
  17. 如何压缩Word文档大小?这个方法太简单啦!
  18. Unity 3D中级项目:捕鱼达人
  19. ubantu18-ROS环境下配置IMU(STIM300),含驱动下载安装与硬件配置。
  20. Pygame显示文字

热门文章

  1. python中使用正则模板匹配结果
  2. 【原创】大公司为什么还在采用过时的技术
  3. python学习(八)定制类和枚举
  4. 集合框架(一) ----------Map集合遍历的方法
  5. query的list()和iterate()区别 面试题
  6. mysql中char与varchar的区别分析
  7. 【LeetCode】44. Wildcard Matching (2 solutions)
  8. 给管道注册事件,用于用户是否登录!
  9. SQL语言的事务机制_转摘
  10. [Leetcode][第题][JAVA][两个数组的交集 II1][双指针][HashMap]