【问题标题】:How to upload a file to a SharePoint library folder using the Microsoft Graph API C# SDK?如何使用 Microsoft Graph API C# SDK 将文件上传到 SharePoint 库文件夹?
【发布时间】:2021-10-14 17:57:21
【问题描述】:

我找不到任何关于如何使用 MS Graph SDK 将文件上传到 SharePoint 库子文件夹的示例。

这些帖子展示了如何使用 REST API 而不是 C# SDK 来实现这一点。

upload files in a folder in Sharepoint/OneDrive library using graph api

How to perform a resumable Upload to a SharePoint Site (Not Root) Subfolder using MS Graph API

【问题讨论】:

    标签: microsoft-graph-api sharepoint-online


    【解决方案1】:

    这就是我最后的做法:

    try
    {
        using var stream = new MemoryStream(contentBytes);
    
        // Retrieve the folder item.
        var items = await this._graphClient.Sites["siteId"].Lists["listId"].Items
            .Request()
            .GetAsync();
    
        // I happened to know the folder name in advance, so that is what I used to retrieve the folder item from Items collection.
        // I could not find how to query the Graph to get just the folder instead of all the items. Hopefully I will find it and update the code.
        var folderItem = items.FirstOrDefault(i => i.WebUrl.Contains($"{path}") && i.ContentType.Name == "Folder");
    
        // you must create a DriveItem, not a ListItem object. It is important to set the the File property.
        var listItem = new DriveItem
        {
            Name = fileName,
            File = new Microsoft.Graph.File(),
            AdditionalData = new Dictionary<string, object>()
                    {
                        { "@microsoft.graph.conflictBehavior", "replace" }
                    },
    
        };
    
        listItem = await this._graphClient.Sites["siteId"].Lists["listId"].Items[folderItem.Id].DriveItem.Children
            .Request()
            .AddAsync(listItem);
    
        // I needed the drive Id here. it is in the Drives properties of the site. It corresponds to the drive associated to the library.
        var uploadSession = await this._graphClient.Sites["siteId"].Drives["driveId"]
            .Items[listItem.Id]
            .CreateUploadSession()
            .Request()
            .PostAsync();
    
        var largeFileUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, stream);
    
        UploadResult<DriveItem> uploadResult = await largeFileUploadTask.UploadAsync();
    
        return uploadResult;
    }
    catch (ServiceException e)
    {
        Log.Error(e);
        throw;
    }
    

    重要的是要知道,在创建上传会话时,您首先需要检索与库关联的驱动器 ID。只要您加载扩展驱动器查询选项,就可以从站点对象中检索驱动器。

    var siteQueryOptions = new List<QueryOption>()
    {
        new QueryOption("expand", "drives")
    };
    
    var site = await this._graphClient.Sites.GetByPath(siteRelativeUrl, hostName)
        .Request(siteQueryOptions)
        .GetAsync();
    
    string driveId = site.Drives.FirstOrDefault(d => d.WebUrl.EndsWith(workingDocumentsLibName, StringComparison.InvariantCultureIgnoreCase))?.Id;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多