【发布时间】:2018-01-30 11:32:22
【问题描述】:
所以我正在尝试从 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 与库中的文件数匹配。然而,templateFiles 和 templateListItems 都有 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: '访问被拒绝。您无权执行此操作或访问此资源。'
是的,我已经检查过了 - 我确实拥有此库的完全权限,并且它处于默认设置,因此项目级权限不会与库的权限有所不同。
有没有人知道我做错了什么或如何正确做?实际目标是通过文件名访问库中的特定文件,文件列表也可以。
【问题讨论】:
-
您能否发布您的
AppManifest.xml中显示AppPermissionRequests部分的部分?从你所说的应用程序权限是错误的,即使你这样做,它也无权访问列表项。 -
@Equalsk 我的
AppManifest.xml不包含任何AppPermissionRequestssection。我只有Properties和Title和StartPage,以及AppPrincipal和RemoteWebApplication。 -
应用程序是要在当前用户的上下文中运行,还是只需要应用程序权限运行?
-
@Equalsk 既然你提到了它......使用仅限应用程序的权限运行可能会更好。我现在将其更改为
CreateAppOnlyClientContextForSPHost()并设置<AppPermissionRequests AllowAppOnlyPolicy="true" />但代码的行为完全相同:( -
您要求的范围是什么?应该有一条看起来像
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web/list" Right="Write"/>的行(您的行可能会因需要而异)
标签: c# asp.net-mvc sharepoint sharepoint-apps