本文介绍SharePoint 2013 Provider Hosted app。
首先来回顾一些啊SharePoint 2013 Provider Hosted app的特点(在前面三篇介绍app的文章中我们也有提到一些,但没针对Provider Hosted app作过总结):
- 可以运行在SharePoint farm之外的server上
- 可以使用server端代码(注意不是SharePoint server API)
- 使用Client Object Model 或Rest API访问SharePoint 的资源
- 使用OAuth 协议取得SharePoint授权
- 可以使用非微软的技术平台来Host(当然本例使用Asp.net来host)
该app主要用途是SharePoint 与其他系统集成,因为该类型的App记可以访问SharePoint资源也可以访问宿主系统本身的资源。
闲话不说,说怎么做:
1. 创建一个证书备用 http://msdn.microsoft.com/en-us/library/fp179901.aspx#Cert2
2. 配置Provider Host app 信任关系 http://msdn.microsoft.com/en-us/library/fp179901.aspx#Configure2, 注意记住 issuerId,在第三步需要用到
3. 启用SharePoint 使用OAuth 时可以通过 Http请求 http://msdn.microsoft.com/en-us/library/fp179901.aspx#Https2
4. 创建Provider Host app http://msdn.microsoft.com/en-us/library/fp179901.aspx#Createapp2
5. 在SharePoint provider hosted app 中使用Client Ojbect Model 访问SharePoint 资源(本例功能为在页面上打印出SharePoint Documents文档库中的文件相对路径),将Default.aspx.cs修改为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SP=Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Linq;
namespace SharePointApp9Web.Pages
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// The following code gets the client context and Title property by using TokenHelper.
// To access other properties, you may need to request permissions on the host web.
Uri hostWeb = new Uri(Request.QueryString["SPHostUrl"]);
// Here only take CamlQuery for example for the client object basic operations
// for more examples, please check http://msdn.microsoft.com/en-us/library/fp179912.aspx
// and http://code.msdn.microsoft.com/SharePoint-2013-Perform-eba8df54
// using (var clientContext = TokenHelper.GetS2SClientContextWithWindowsIdentity(hostWeb, Request.LogonUserIdentity))
// 该处 如果使用TokenHelper.GetS2SClientContextWithWindowsIdentity 来取得clientContext,下面的代码就查不出文档记录了,笔者也不
// 知道是为啥,按道理讲是不应该的,可能是微软的bug吧,
// 所以最后笔者使用 new SP.ClientContext(hostWeb.AbsoluteUri)来初始化客户端模型的上下文:
using (var clientContext = new SP.ClientContext(hostWeb.AbsoluteUri))
{
Microsoft.SharePoint.Client.List spList = clientContext.Web.Lists.GetByTitle("Documents");
clientContext.Load(spList);
clientContext.ExecuteQuery();
if (spList != null && spList.ItemCount > 0)
{
Microsoft.SharePoint.Client.CamlQuery camlQuery = new SP.CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<Where><Neq><FieldRef Name='Title' /><Value Type='Text'>a</Value></Neq></Where><OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>
</Query>
<ViewFields><FieldRef Name='FileLeafRef' /><FieldRef Name='Title' /><FieldRef Name='Modified' /><FieldRef Name='Author' /><FieldRef Name='FileRef' /><FieldRef Name='FileSizeDisplay' /><FieldRef Name='Created' /><FieldRef Name='Editor' /></ViewFields>
</View>";
SP.ListItemCollection listItems = spList.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (SP.ListItem item in listItems)
{
Response.Write(item["FileRef"] + "<br>");
}
}
}
}
}
}
运行该app(直接点击VS2012上的Start按钮),当出现下列页面时点击Trust It:
但程序可能会报拒绝访问(access denied)的错误,原因是在AppManifest.xml没有申明该App需要访问那些SharePoint资源。但app尝试读取文档库 Documents.
解决方法为:
- 双击AppManifest.xml
- 单击Permission 选项卡
- Scope 选择List, Permission 选择Read
注意:这里Scope和permission都有很多选项可以选,开发者可以根据app的资源访问需求来选择
重新运行该app,就可以打印出文档库里面的文件相对路径了