简体   繁体   English

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

[英]access Sharepoint document library items from provider-hosted app

So I'm trying to access files from a Sharepoint document library in C#. 因此,我试图从C#中的Sharepoint文档库访问文件。 My app is a provider-hosted Sharepoint app. 我的应用程序是提供商托管的Sharepoint应用程序。 I seem to be able to access the library, but not the library's items. 我似乎可以访问图书馆,但不能访问图书馆的物品。

Here is how I get my context in the controller: 这是我在控制器中获取上下文的方法:

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

And how I try to access the files: 以及如何尝试访问文件:

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();

At this point, the templateList has the property ItemCount = 8 which does match the number of the files in the library. 此时, templateList具有属性ItemCount = 8 ,该属性与库中的文件数匹配。 Yet, both templateFiles and templateListItems , have Count = 0 so I don't seem to be able to access these 8 items. 但是, templateFilestemplateListItems都具有Count = 0因此我似乎无法访问这8个项目。

I have also tried to access a single item by it's id which I looked up on Sharepoint: 我还尝试通过其在ID上查找的ID来访问单个项目:

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

Yet this leads to the error: 但这会导致错误:

Microsoft.SharePoint.Client.ServerException: 'Item does not exist. Microsoft.SharePoint.Client.ServerException: '项目不存在。 It may have been deleted by another user.' 它可能已被其他用户删除。”

Another approach I tried was using the GetFileByServerRelativeUrl to get a specific file: 我尝试过的另一种方法是使用GetFileByServerRelativeUrl获取特定文件:

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

This gives me the following error: 这给了我以下错误:

Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: 'Access denied. Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: '访问被拒绝。 You do not have permission to perform this action or access this resource.' 您没有执行此操作或访问此资源的权限。”

And yes I've checked - I do have full permissions for this library and it's on default settings so item-level permissions do not vary from library's permissions. 是的,我已经检查过-我确实拥有此库的全部权限,并且它是默认设置,因此项目级权限与库的权限没有不同。

Does anyone have an idea what I'm doing wrong or how to do it properly? 有人知道我在做什么错或如何正确做吗? The actual goal is to access a specific file from the library by the file's name, a file list would also work. 实际目标是通过文件名从库中访问特定文件,文件列表也将起作用。

Did you try to start Visual Studio as Admin? 您是否尝试以管理员身份启动Visual Studio? If that solves your problem, you might be needing a manifest file and change inside that admin required. 如果这样可以解决您的问题,则您可能需要清单文件并在所需的管理员内部进行更改。 It normally happens when you're trying to access files that are not part of the Windows User, which means the root folder(C:). 当您尝试访问不属于Windows用户的文件时,通常会发生这种情况,这意味着根文件夹(C :)。

I worked on this type of things before, but I didn't use C#. 我以前从事这类工作,但没有使用C#。 If you want to try another way, there is a PowerShell Method called PnP PowerShell that accesses easily the SharePoint app and return things (It is officially recognised by Microsoft). 如果您想尝试其他方法,则有一个名为PnP PowerShell的PowerShell方法,可以轻松访问SharePoint应用程序并返回内容(Microsoft正式认可)。 You could try with : 您可以尝试:

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

which returns all items from a list (remember that a document library is just a special list, like a task list or other), there are versions of the cmdlet that only return 1 item. 它返回列表中的所有项目(请记住文档库只是一个特殊列表,如任务列表或其他),有些cmdlet版本仅返回1个项目。

For example: 例如:

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

returns the 1000 first elements of the Tasks list. 返回“任务”列表的前1000个元素。

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

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

Another answer I can try to give, do you run your code directly from the server location? 我可以尝试给出的另一个答案是,您是否直接从服务器位置运行代码? I sometimes needed to do that to make some of my programs work. 有时我需要这样做才能使某些程序正常工作。

To access the file of a list item, you need to access the list item itself, and load its File property within the context: 要访问列表项的文件,您需要访问列表项本身,并在上下文中加载其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;

Are you absolutely sure, that you have a file with ID = 1 ? 您绝对确定您拥有ID = 1的文件吗? Use your code: 使用您的代码:

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

clientContext.Load(templateListItems); clientContext.Load(templateListItems);

clientContext.ExecuteQuery(); clientContext.ExecuteQuery();

get all the items, and check their ids. 获取所有物品,并检查其ID。

EDIT 编辑

Based on your comment - Did you try with a different Query (writing your own) that will retrieve some items based on a rule. 根据您的评论-您是否尝试过其他查询(编写您自己的查询),该查询将基于规则检索某些项目。 Because the property templateList.ItemCount giving you some value, still doesn't mean that you have loaded the items properly. 因为属性templateList.ItemCount为您提供了一些价值,但这并不意味着您已经正确加载了项目。 It means that you have loaded the list. 这意味着您已经加载了列表。

There is something with the Query. 查询有问题。 From what I see in all the examples they are creating a query like this: 从我在所有示例中看到的,他们正在创建这样的查询:

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

I know that it doesn't seem like a deal breaker, but you know - SharePoint... It's worth the try. 我知道这似乎并没有破坏交易,但您知道-SharePoint ...值得尝试。

I finally got it to work! 我终于得到它的工作! Turns out, that the permissions on SharePoint where wrong. 原来,那对SharePoint的权限哪里错了。

On SharePoint, in the "Apps in Testing" Tab, you can click the "..." next to your app, go to "MANAGE PERMISSIONS" and you will be forwarded to a page that asks you "Do you trust XXX?" 在SharePoint上的“测试中的应用程序”选项卡中,您可以单击应用程序旁边的“ ...” ,转到“管理权限”,然后您将转至询问您“您是否信任XXX?”的页面。 . I was so focused on the question and the "Trust It" and "Cancel" Buttons that I didn't see the dropdown on the left saying "Let it have full control of the list:" with "App Packages" pre-selected. 我非常关注问题以及“信任”“取消”按钮,以至于我没有在左侧的下拉菜单中看到“让它完全控制列表:”,并且预先选择了“应用程序包” When I changed it to the list I was trying to access, one of the approaches in my code suddenly worked: 当我将其更改为尝试访问的列表时,代码中的一种方法突然起作用:

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

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

Now templateListItems actually contains objects from my library! 现在templateListItems实际上包含我库中的对象!

One can of course "optimize" the code and delete the first ExecuteQuery() , or define the CamlQuery.CreateAllItemsQuery() as a separate variable and pass it to GetItems() , or use var s instead of explicit types - all of those work, it's up to your preferences. 当然,可以“优化”代码并删除第一个ExecuteQuery() ,或者将CamlQuery.CreateAllItemsQuery()定义为单独的变量,并将其传递给GetItems() ,或者使用var而不是显式类型-所有这些工作,这取决于您的偏好。

Appreciate all the help, I hope I've got it from here on :) 感谢所有的帮助,我希望从这里得到它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Sharepoint 提供程序托管的应用程序中检索 SharePoint 2013 列表数据 - Retrieving SharePoint 2013 List Data from Sharepoint provider hosted app 将文档从Windows Store App上载到SharePoint 2013文档库 - Uploading a document from Windows Store App to a SharePoint 2013 document library 获取SharePoint文档库中项目的URL - get url of items in sharepoint document library 从 Sharepoint 文档库下载 - Downloading from Sharepoint Document Library Sharepoint Provider托管的应用程序:无效的JWT令牌。 令牌已过期 - Sharepoint Provider Hosted App: Invalid JWT Token. The token is expired 将Sharepoint App 2013部署到Windows Azure(托管提供程序) - Deploying Sharepoint App 2013 to Windows Azure (Provider Hosted) 链接到服务器端部分的SharePoint Provider托管应用程序 - SharePoint Provider Hosted App linking to server side part Sharepoint Provider托管用户权限 - Sharepoint Provider Hosted User Permissions 我将在哪里将Page_Load的代码放在MVC SharePoint 2013提供程序托管的应用程序中? - Where would I put the code for Page_Load in an MVC SharePoint 2013 provider hosted app? 如何将文档从Sharepoint文档库返回给用户? - How do I return a document from a Sharepoint Document library to the user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM