快捷搜索:

SharePoint 2013 app的新特性Provider Hosted app使用实战

本文先容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 =@"

'Title' />'Text'>a'Created' Ascending='FALSE' />

'FileLeafRef' />'Title' />'Modified' />'Author' />'FileRef' />'FileSizeDisplay' />'Created' />'Editor' />

";

SP.ListItemCollection listItems = spList.GetItems(camlQuery);clientContext.Load(listItems);

clientContext.ExecuteQuery();foreach (SP.ListItem item in listItems)

{Response.Write(item["FileRef"] + "

");

}}

}}

}}

运行该app(直接点击VS2012上的Start按钮),当呈现下列页面时点击Trust It:

但法度榜样可能会报回绝造访(access denied)的差错,缘故原由是在AppManifest.xml没有申明该App必要造访那些SharePoint资本。但app考试测验读取文档库 Documents.

办理措施为:

双击AppManifest.xml

单击Permission 选项卡

Scope 选择List, Permission 选择Read

留意:这里Scope和permission都有很多选项可以选,开拓者可以根据app的资本造访需求来选择

从新运行该app,就可以打印出文档库里面的文件相对路径了

您可能还会对下面的文章感兴趣: