代码之家  ›  专栏  ›  技术社区  ›  D. Kelly

使用Microsoft Graph使用C将文件上载到Sharepoint#

  •  0
  • D. Kelly  · 技术社区  · 1 年前

    我有一个客户,他设置了一个证书,允许我访问他的Sharepoint网站。他向我提供了PDX文件、ClientID和TenantID。我相信我能够使用下面提供的代码进行身份验证,但我没有成功使用GraphServiceClient将文件上传到网站。

    他已经用Powershell脚本对自己的端进行了一些测试,并且能够通过Powershell上传文件。我对Powershell不太流利,更喜欢用C#编写我的解决方案。

    我到处找代码的例子来做这项工作。没有一个例子有效。我不需要异步解决方案。此过程将定期(可能每天)运行,从一个系统读取文件并将其复制到另一个系统。这在C#中可能吗?

    这是我的密码。由于最后一行出现问题,它不会编译-var resultDriveItem=wait。。。

    谢谢

    public static async void TestClientCert()
        {
            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var clientId = "blah";
            var tenantId = "blah";
            var clientCertificate = new X509Certificate2(@"c:\work\Test.pfx", "blah");
    
            var options = new ClientCertificateCredentialOptions
            {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
            };
    
            var clientCertCredential = new ClientCertificateCredential(
              tenantId, clientId, clientCertificate, options);
    
            var gc = new GraphServiceClient(clientCertCredential, scopes);
    
            var fileName = @"c:\work\fileToUpload\upload.doc";
    
            using (Stream fileStream = new FileStream(
                fileName,
                FileMode.Open,
                FileAccess.Read))
                {
                var resultDriveItem = await gc.Sites["SiteID-Blah"]
                        .Drives[0].Root.ItemWithPath(fileName).Content.Request().PutAsync<DriveItem>(fileStream);
                }
        }
    
    
    
    
    
    0 回复  |  直到 1 年前
        1
  •  0
  •   Elidjay Ross    1 年前

    我想说的是:

    1: Connect-PnPOnline -Url "contoso.sharepoint.com" -ClientId 6c5c98c7-e05a-4a0f-bcfa-0cfc65aa1f28 -CertificatePath 'c:\mycertificate.pfx'
    2: Add-PnPFile -Path c:\temp\file.pdf -Folder DocumentLibraryName
    

    并且您的文件是使用PnP PowerShell模块(经过microsoft认证)上载的。

    如果你真的想用C#,我会考虑试试 PnP Core SDK :

    // Get a reference to a folder
    IFolder siteAssetsFolder = await context.Web.Folders.Where(f => f.Name == "SiteAssets").FirstOrDefaultAsync();
            
    // Upload a file by adding it to the folder's files collection
    IFile addedFile = await siteAssetsFolder.Files.AddAsync("test.docx", System.IO.File.OpenRead($".{Path.DirectorySeparatorChar}TestFilesFolder{Path.DirectorySeparatorChar}test.docx"));
    
        2
  •  0
  •   RaytheonXie-MSFT    1 年前

    您可以参考以下代码

    public static void UploadFile(ClientContext context,string uploadFolderUrl, string uploadFilePath)  
    {  
        var fileCreationInfo = new FileCreationInformation  
        {  
                Content = System.IO.File.ReadAllBytes(uploadFilePath),  
                Overwrite = true,  
                Url = Path.GetFileName(uploadFilePath)  
        };  
        var targetFolder = context.Web.GetFolderByServerRelativeUrl(uploadFolderUrl);  
        var uploadFile = targetFolder.Files.Add(fileCreationInfo);  
        context.Load(uploadFile);  
        context.ExecuteQuery();  
    }  
      
    using (var ctx = new ClientContext(webUri))  
    {  
         ctx.Credentials = credentials;  
      
         UploadFile(ctx,"LibName/FolderName/Sub Folder Name/Sub Sub Folder Name/Sub Sub Sub Folder Name",filePath);     
    }
    
        3
  •  0
  •   Jeremy Caney Abloin    1 年前

    而不是

    .ItemWithPath(fileName) -- Path
    

    使用

    .ItemWithPath("TestFile.txt")