从提供商托管的应用程序访问Sharepoint文档库项目

时间:2018-01-30 11:32:22

标签: c# asp.net-mvc sharepoint sharepoint-apps

所以我试图从C#中的Sharepoint文档库中访问文件。我的应用是提供商托管的Sharepoint应用。我似乎能够访问图书馆,但不能访问图书馆的项目。

以下是我在控制器中获取上下文的方法:

var spContext = SharePointContextProvider.Current.GetSharePointContext(System.Web.HttpContext.Current);
using (var clientContext = spContext?.CreateUserClientContextForSPHost())
{
    if (clientContext != null)
    {
        template.SetMergefields(clientContext);
    }
}

以及我如何尝试访问这些文件:

Web web = clientContext.Web;

List templateList = web.Lists.GetByTitle(libraryName);
clientContext.Load(templateList);
clientContext.ExecuteQuery();

var templateFiles = templateList.RootFolder.Files;
clientContext.Load(templateFiles);
clientContext.ExecuteQuery();

var templateListItems = templateList.GetItems(CamlQuery.CreateAllItemsQuery());
clientContext.Load(templateListItems);
clientContext.ExecuteQuery();

此时,templateList的属性ItemCount = 8与库中的文件数相匹配。然而,templateFilestemplateListItems都有Count = 0,所以我似乎无法访问这8个项目。

我还尝试通过它在Sharepoint上查找的ID来访问单个项目:

var itemWithId1 = templateList.GetItemById(1);
clientContext.Load(itemWithId1);
clientContext.ExecuteQuery();

但这会导致错误:

Microsoft.SharePoint.Client.ServerException:'项目不存在。它可能已被其他用户删除。'

我尝试的另一种方法是使用GetFileByServerRelativeUrl来获取特定文件:

File file = clientContext.Web.GetFileByServerRelativeUrl(serverRelativeUrl);
clientContext.Load(file);
clientContext.ExecuteQuery();

这给了我以下错误:

Microsoft.SharePoint.Client.ServerUnauthorizedAccessException:'访问被拒绝。您无权执行此操作或访问此资源。'

是的,我已经检查过 - 我确实拥有此库的完全权限,而且它是默认设置,因此项级权限不会因库的权限而异。

有谁知道我做错了什么或怎么做得好?实际目标是通过文件名访问库中的特定文件,文件列表也可以。

5 个答案:

答案 0 :(得分:0)

您是否尝试以管理员身份启动Visual Studio? 如果这样可以解决您的问题,那么您可能需要一个清单文件并在需要的管理员内部进行更改。 当您尝试访问不属于Windows用户的文件时,通常会发生这种情况,这意味着根文件夹(C :)。

答案 1 :(得分:0)

之前我曾在这类事情上工作,但我没有使用C#。如果您想尝试其他方式,可以使用名为PnP PowerShell的PowerShell方法轻松访问SharePoint应用程序并返回内容(Microsoft正式认可)。您可以尝试:

Get-PnPListItem -List <ListPipeBind>
            [-Fields <String[]>]
            [-PageSize <Int>]
            [-ScriptBlock <ScriptBlock>]
            [-Web <WebPipeBind>]
            [-Connection <SPOnlineConnection>]

返回列表中的所有项目(请记住文档库只是一个特殊列表,如任务列表或其他),cmdlet的版本只返回1个项目。

例如:

PS:> Get-PnPListItem -List Tasks -PageSize 1000

返回任务列表的1000个第一个元素。

这是主要的github:https://github.com/SharePoint/PnP-PowerShell/tree/master/Documentation

这是cmdlet文档:https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/Get-PnPListItem.md

答案 2 :(得分:0)

我可以尝试给出另一个答案,您是否直接从服务器位置运行代码?我有时需要这样做,以使我的一些程序工作。

答案 3 :(得分:0)

要访问列表项的文件,您需要访问列表项本身,并在上下文中加载其File属性:

var docs = clientContext.Web.Lists.GetByTitle(Library);
var listItem = docs.GetItemById(listItemId);
clientContext.Load(docs);
clientContext.Load(listItem, i => i.File);
clientContext.ExecuteQuery();

var fileRef = listItem.File.ServerRelativeUrl;

您确定自己拥有ID = 1的文件吗?使用您的代码:

  

var templateListItems = templateList.GetItems(CamlQuery.CreateAllItemsQuery());

     

clientContext.Load(templateListItems);

     

clientContext.ExecuteQuery();

获取所有项目,并检查他们的ID。

修改

根据您的评论 - 您是否尝试使用其他查询(编写自己的查询)来根据规则检索某些项目。因为属性templateList.ItemCount为您提供了一些价值,但仍然并不意味着您已正确加载了商品。这意味着您已加载列表。

查询有一些东西。从我在所有示例中看到的,他们正在创建这样的查询:

var query = CamlQuery.CreateAllItemsQuery();
var templateListItems = templateList.GetItems(query);

我知道这似乎不是一个交易破坏者,但你知道 - SharePoint ......值得一试。

答案 4 :(得分:0)

我终于开始工作了!事实证明,SharePoint上的权限错误。

在SharePoint上,在“测试中的应用”标签中,您可以点击应用旁边的“...”,转到“管理” PERMISSIONS“,您将被转发到一个页面,询问您”您信任XXX吗?“。我非常专注于这个问题和“信任它”“取消”按钮,我没有看到左边的下拉列表“让它完全控制列表:“ ”应用程序包“预选。当我将其更改为我尝试访问的列表时,我的代码中的一种方法突然起作用:

List templateList = web.Lists.GetByTitle(libraryName);
clientContext.Load(templateList);
clientContext.ExecuteQuery();

ListItemCollection templateListItems = templateList.GetItems(CamlQuery.CreateAllItemsQuery());
clientContext.Load(templateListItems);
clientContext.ExecuteQuery();

现在templateListItems实际上包含了我库中的对象!

当然可以“优化”代码并删除第一个ExecuteQuery(),或将CamlQuery.CreateAllItemsQuery()定义为单独的变量并将其传递给GetItems(),或使用var而不是显式类型 - 所有这些工作,都取决于您的偏好。

感谢所有的帮助,我希望我从这里得到它:)