SharePoint 2013 App Development读书笔记5

使用客户端对象模型(Client-Side Object Model,即CSOM)和REST API来开发app,针对不同的host环境,有不同的限制,可以参考下图:


这张图告诉我们,如果开发SharePoint-hosted的app,只能使用javascript调用CSOM或者REST API,

如果开发Provider或者auto-hosted的app,可以使用C#来调用CSOM或者REST(当然这些C#代码是运行在remote web中的),也可以使用js来调用REST,但是不能使用JS调用CSOM。

使用客户端对象模型访问SharePoint


这张架构图的左边部分是使用客户端对象模型访问SharePoint的App,右边是SharePoint服务端,可以看到,在客户端有两种方式,一种是供JS调用的CSOM,一种是托管的C#代码调用的CSOM,它们通过web service向SharePoint发送请求(XML格式),接收SharePoint发回的JSON。在服务端处理请求的是Client.svc,它针对客户端的请求,调用服务端代码,返回结果。

使用REST API访问SharePoint


SharePoint 2010的时候,可以通过listdata.svc来访问SharePoint,在SharePoint 2013中,listdata.svc仍旧可以使用,但是推荐使用client.svc。我们可以通过_api来访问client.svc,例如:http://localhost/_api/site将返回站点集的属性(注意如果使用IE浏览器的时候,需要关闭feed view):

  <?xml version="1.0" encoding="utf-8" ?> 
  <entry xml:base="http://sp2013test/_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>http://sp2013test/_api/site</id> 
      <category term="SP.Site" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
      <link rel="edit" href="site" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/EventReceivers" type="application/atom+xml;type=feed" title="EventReceivers" href="site/EventReceivers" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Features" type="application/atom+xml;type=feed" title="Features" href="site/Features" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Owner" type="application/atom+xml;type=entry" title="Owner" href="site/Owner" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/RecycleBin" type="application/atom+xml;type=feed" title="RecycleBin" href="site/RecycleBin" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/RootWeb" type="application/atom+xml;type=entry" title="RootWeb" href="site/RootWeb" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/UserCustomActions" type="application/atom+xml;type=feed" title="UserCustomActions" href="site/UserCustomActions" /> 
      <title /> 
      <updated>2014-04-09T01:19:48Z</updated> 
      <author>
          <name /> 
      </author>
      <content type="application/xml">
          <m:properties>
              <d:AllowDesigner m:type="Edm.Boolean">true</d:AllowDesigner> 
              <d:AllowMasterPageEditing m:type="Edm.Boolean">false</d:AllowMasterPageEditing> 
              <d:AllowRevertFromTemplate m:type="Edm.Boolean">false</d:AllowRevertFromTemplate> 
              <d:AllowSelfServiceUpgrade m:type="Edm.Boolean">true</d:AllowSelfServiceUpgrade> 
              <d:AllowSelfServiceUpgradeEvaluation m:type="Edm.Boolean">true</d:AllowSelfServiceUpgradeEvaluation> 
              <d:CompatibilityLevel m:type="Edm.Int32">15</d:CompatibilityLevel> 
              <d:Id m:type="Edm.Guid">8e5e5d0c-f242-49e8-bf17-055ec80cf3d0</d:Id> 
              <d:LockIssue m:null="true" /> 
              <d:MaxItemsPerThrottledOperation m:type="Edm.Int32">5000</d:MaxItemsPerThrottledOperation> 
              <d:PrimaryUri>http://sp2013test/</d:PrimaryUri> 
              <d:ReadOnly m:type="Edm.Boolean">false</d:ReadOnly> 
              <d:ServerRelativeUrl>/</d:ServerRelativeUrl> 
              <d:ShareByLinkEnabled m:type="Edm.Boolean">false</d:ShareByLinkEnabled> 
              <d:ShowUrlStructure m:type="Edm.Boolean">false</d:ShowUrlStructure> 
              <d:UIVersionConfigurationEnabled m:type="Edm.Boolean">false</d:UIVersionConfigurationEnabled> 
              <d:UpgradeReminderDate m:type="Edm.DateTime">1899-12-30T00:00:00</d:UpgradeReminderDate> 
              <d:Upgrading m:type="Edm.Boolean">false</d:Upgrading> 
              <d:Url>http://sp2013test</d:Url> 
          </m:properties>
      </content>
  </entry>

一些常用的URI:

命名空间:

URI命名空间
http://localhost/_api/SharePoint Foundation
http://localhost/_api/searchEnterprise search
http://localhost/_api/sp.userProfiles.peopleManagerUser profiles

常用的对象及属性:

URI对象和属性
http://localhost/_api/siteSite对象
http://localhost/_api/webWeb对象
http://localhost/_api/site/urlSite的url属性
http://localhost/_api/web/listslist集合
http://localhost/_api/web/lists('543456r5-r45w-4rds-34dd-d3f44563429v')list集合索引其
http://localhost/_api/web/lists/getbytitle('Contacts')list集合方法
http://localhost/_api/web/lists/getbytitle('Contacts')/itemsitem集合

使用$select和$order来选择特定的对象和排序

URI功能
http://localhost/_api/web/lists/getbytitle('Modules')/items选择Modules列表中的所有字段
http://localhost/_api/web/lists/getbytitle('Modules')/items?$select=Title选择Modules列表中的Title字段
http://localhost/_api/web/lists/getbytitle('Modules')/items?$select=Title,Instructor/FullName&$expand=Instructor/FullName选择Title和Instructor这俩个字段,其中Instructor字段是一个lookup字段,使用$expand包含Instructor所lookup的字段“FullName”
http://localhost/_api/web/lists/getbytitle('Modules')/items?$select=Title&$order=Modified选择Title字段并按照modified排序

使用$filter来过滤:

URI功能
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$filter=FirstName eq 'Sam'返回Contacts列表中,FirstName是Sam的item
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$filter=startwith(FirstName,'S')返回Contacts列表中,FirstName以S开头的item
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$filter=month(Modified) eq 8返回Contacts列表中,在8月份有修改的item

使用$top和$skip实现分页获取数据:

URI功能
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$top=10返回Contacts列表的前十条item
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$top=10&$skip=10返回Contacts列表的第十一到第二十条item
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$sort=Title&$top=10&$skip=10返回第十一到第二十条item,并按照Title排序

代码示例参见:git@code.csdn.net:shrenk/sharepoint-applab.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值