SharePoint 2013的REST编程基础

1. SharePoint 2013对REST编程的支持javascript

自从SharePoint2013开始, SharePoint开始了对REST 编程的支持,这样除了.NET , Silverlight, Powershell以外, 又多了一种能够和SharePoint Server进行CSOM编程的方式。那么,问题来了:什么是REST呢?什么是ODATA?为何这么多产品都开始支持这个了?css

 

2. 什么是REST & ODATA?java

若是这个世界上只有一家IT公司就行了,这样就不须要REST了 :) 可是很显然,这是不可能的。不一样IT公司有本身提出的技术标准及解决方案。以Microsoft为例,其实现分布式处理的技术和解决方案发展历程以下:COM -> DCOM -> COM+ ,跨入.NET时代,又出现了 Enterprise Component -> Web Service -> Web Service Enhancemant -> WCF ...jquery

能够看到,仅仅一家公司,其提出的技术和解决方案就如此之多。在加上IBM, Google等等大鳄,就造成了各自为政,群雄混战的局面。这样的解决就是:在各自的系统和技术内,你们各自搞本身的。很显然这是不符合时代发展的 :) 好比没有任何一家公司,只用某一家IT公司的解决方案。不一样公司的系统之间,也须要互相通信。web

 

那怎么解决这个问题呢?你们握手言和吧:制定一个基于HTTP协议的标准。HTTP协议是目前为止,最通用和成功的协议。若是某个公司说我就是不遵照HTTP协议,那他就无法转了。 所以,只要你们遵循这个标准,系统之间均可以互相通讯。若是利用Microsoft的技术开发了一个服务,只要这个服务遵循这个标准,采用IBM技术的客户也能够consume这个服务,那不是很好吗? 所以这就是REST服务出现的现实须要。ajax

 

言归正传,什么是REST 服务呢?shell

REST的全称是: Representational state transfer (有点绕口 :)), 她的基本思想是:能够经过基于HTTP协议发出的不一样 操做:Create, Read, Update, and Delete (CRUD), 来实现对数据的操做。其有三大基本特征:编程

1) 客户端- 服务端:api

 因此数据都存储在服务端,客户端不知道也无需知道服务端如何处理的,只须要发送一个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服务示例

未完待续...

相关文章
相关标签/搜索