Concatenate Path.Getfilename(filename)将文件上载到SharePoint

时间:2016-03-04 14:18:33

标签: c# sharepoint concatenation filestream

我正在尝试编写一个将文件传输到SharePoint的程序,我正在使用Filestream,因为这将从文件服务器获取服务器。我无法连接Path.Getfilename(filename)Microsoft.SharePoint.Client.File.SaverBinaryDirect(context,"",fs,true);内的文字字符串,我更像是一个管理员而不是程序员。任何帮助都会很棒。

class Program
{
    static void Main(string[] args)
    {
       ClientContext context = new    ClientContext("https://company.sharepoint.com");
       {
          SecureString password = new SecureString();
          foreach (char c in "Password")
             password.AppendChar(c);
          context.Credentials = new SharePointOnlineCredentials("UserEmail", password);
          context.ExecuteQuery();
          Console.WriteLine("File Transfer is Complete");
       }

       string[] fileEntries = Directory.GetFiles(@"\\directorypath");
       foreach (string fileName in fileEntries)
       {
           Console.WriteLine(fileName);
           Console.WriteLine(Path.GetFileName(fileName));
           using (FileStream fs = new FileStream(fileName, FileMode.Open))
           {
                Path.Combine(fileName,Path.GetFileName(fileName.Replace("/sites/Name ofSite/documentlibrary/", "")));
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "", fs, true);  
           }


         }
     }
}

1 个答案:

答案 0 :(得分:0)

File.SaveBinaryDirect Method期望serverRelativeUrl参数一个字符串,表示文件的服务器相对URL ,例如:

/site/web/documents/file.zip

要构建文件服务器相对URL ,您可以组合列表/库服务器相对URL和文件名:

using (var fs = new FileStream(fileName, FileMode.Open))
{
     var fileUrl =  "/site/web/documents/" + Path.GetFileName(fileName);
     Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, fileUrl, fs, true);
}

修改后的示例

    static void Main(string[] args)
    {
        //source settings (local file system)
        var sourcePath = @"C:\Users\jdoe\Documents";

        //target settings (SharePoint Online)
        var webUrl = "https://contoso.sharepoint.com/";
        var username = "jdoe@contoso.onmicrosoft.com";
        var password = "password";
        var targetListTitle = "Documents";

        var fileNames = Directory.GetFiles(sourcePath);  //retrieve source files

        using (var ctx = GetContext(webUrl, username, password))
        {

            //prepare target list
            var targetList = ctx.Web.Lists.GetByTitle(targetListTitle);
            ctx.Load(targetList, l => l.RootFolder.ServerRelativeUrl);
            ctx.ExecuteQuery();


            foreach (var fileName in fileNames)
            {
                using (var fs = new FileStream(fileName, FileMode.Open))
                {
                    var fileUrl = targetList.RootFolder.ServerRelativeUrl + "/" + Path.GetFileName(fileName);
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, fileUrl, fs, true);
                }
            }
        }
    }

,其中

    private static ClientContext GetContext(string url,string username, string password)
    {
        var ctx = new ClientContext(url);
        var securePassword = new SecureString();
        foreach (char c in password) securePassword.AppendChar(c);
        ctx.Credentials = new SharePointOnlineCredentials(username, securePassword);
        return ctx;
    }