C# 如何将文件上载到sharepoint中的文档库?

C# 如何将文件上载到sharepoint中的文档库?,c#,sharepoint,upload,C#,Sharepoint,Upload,如何以编程方式将文件上载到sharepoint中的文档库 我目前正在使用C制作一个Windows应用程序,它将文档添加到文档库列表。您可以使用对象模型或将文档上载到SharePoint库 使用对象模型上载: String fileToUpload = @"C:\YourFile.txt"; String sharePointSite = "http://yoursite.com/sites/Research/"; String documentLibraryName = "Shared Docu

如何以编程方式将文件上载到sharepoint中的文档库


我目前正在使用C制作一个Windows应用程序,它将文档添加到文档库列表。

您可以使用对象模型或将文档上载到SharePoint库

使用对象模型上载:

String fileToUpload = @"C:\YourFile.txt";
String sharePointSite = "http://yoursite.com/sites/Research/";
String documentLibraryName = "Shared Documents";

using (SPSite oSite = new SPSite(sharePointSite))
{
    using (SPWeb oWeb = oSite.OpenWeb())
    {
        if (!System.IO.File.Exists(fileToUpload))
            throw new FileNotFoundException("File not found.", fileToUpload);                    

        SPFolder myLibrary = oWeb.Folders[documentLibraryName];

        // Prepare to upload
        Boolean replaceExistingFiles = true;
        String fileName = System.IO.Path.GetFileName(fileToUpload);
        FileStream fileStream = File.OpenRead(fileToUpload);

        // Upload document
        SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

        // Commit 
        myLibrary.Update();
    }
}

作为Web服务的替代方案,您可以使用FrontPage RPC API的调用。这样做的另一个好处是,您可以在与文件数据相同的请求中提供元数据(列)。明显的缺点是该协议有点模糊(与文档非常丰富的Web服务相比)

有关解释Frontpage RPC使用的参考应用程序,请参阅CodePlex上的项目

如果您收到此错误“值不在此行的预期范围内”

string filePath = @"C:\styles\MyStyles.css"; 
string siteURL = "http://example.org/"; 
string libraryName = "Style Library"; 

using (SPSite oSite = new SPSite(siteURL)) 
{ 
    using (SPWeb oWeb = oSite.OpenWeb()) 
    { 
        if (!System.IO.File.Exists(filePath)) 
            throw new FileNotFoundException("File not found.", filePath);                     

        SPFolder libFolder = oWeb.Folders[libraryName]; 

        // Prepare to upload 
        string fileName = System.IO.Path.GetFileName(filePath); 
        FileStream fileStream = File.OpenRead(filePath); 

        //Check the existing File out if the Library Requires CheckOut
        if (libFolder.RequiresCheckout)
        {
            try {
                SPFile fileOld = libFolder.Files[fileName];
                fileOld.CheckOut();
            } catch {}
        }

        // Upload document 
        SPFile spfile = libFolder.Files.Add(fileName, fileStream, true); 

        // Commit  
        myLibrary.Update(); 

        //Check the File in and Publish a Major Version
        if (libFolder.RequiresCheckout)
        {
                spFile.CheckIn("Upload Comment", SPCheckinType.MajorCheckIn);
                spFile.Publish("Publish Comment");
        }
    } 
} 
SPFolder myLibrary = oWeb.Folders[documentLibraryName];
改为使用此选项修复错误:

SPFolder myLibrary = oWeb.GetList(URL OR NAME).RootFolder;

始终使用URl获取列表或其他列表,因为它们是唯一的,名称不是最佳方式;)

有了SharePoint 2013新库,我成功地做到了以下几点:

private void UploadToSharePoint(string p, out string newUrl)  //p is path to file to load
{
    string siteUrl = "https://myCompany.sharepoint.com/site/";
    //Insert Credentials
    ClientContext context = new ClientContext(siteUrl);

    SecureString passWord = new SecureString();
    foreach (var c in "mypassword") passWord.AppendChar(c);
    context.Credentials = new SharePointOnlineCredentials("myUserName", passWord);
    Web site = context.Web;

    //Get the required RootFolder
    string barRootFolderRelativeUrl = "Shared Documents/foo/bar";
    Folder barFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl);

    //Create new subFolder to load files into
    string newFolderName = baseName + DateTime.Now.ToString("yyyyMMddHHmm");
    barFolder.Folders.Add(newFolderName);
    barFolder.Update();

    //Add file to new Folder
    Folder currentRunFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl + "/" + newFolderName);
    FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(@p), Url = Path.GetFileName(@p), Overwrite = true };
    currentRunFolder.Files.Add(newFile);
    currentRunFolder.Update();

    context.ExecuteQuery();

    //Return the URL of the new uploaded file
    newUrl = siteUrl + barRootFolderRelativeUrl + "/" + newFolderName + "/" + Path.GetFileName(@p);
}

我使用本文允许c#访问sharepoint网站

基本上,您可以创建一个ClientId和ClientSecret密钥,以便使用c访问站点#


希望这能帮助你

SPSite是Microsoft.SharePoint命名空间的一部分,因此您需要添加对Microsoft.SharePoint.dll的引用。假设您是在服务器上开发的,dll可以在以下位置找到:C:\Program Files\Common Files\Microsoft Shared\web Server extensions\12\ISAPI\Microsoft.SharePoint.dllWait a sec。。。此代码只能在连接到服务器场的盒子中工作,对吗?在任何其他框中,它需要使用您不需要调用myLibrary.Update();在Files.Add(…)之后添加文件这对我来说效果很好,只是在以后删除文件时,我必须在文件流上使用“using”(为了使删除生效)。我将其更改为:使用(FileStream FileStream=File.OpenRead(fileToUpload)){//Upload document SPFile SPFile=doclib.Files.Add(fileName,FileStream,replaceExistingFiles);}这不是一个好的解决方案。它需要以DBO和SP场管理员的身份运行,这对于上载文件的任务来说是一个荒谬的选择,对于签入if语句来说是+1。考虑不更新MyLabor。它可能导致并发冲突。您好,我得到下面的错误。System.NotSupportedException:给定路径';不支持s格式。在System.IO.FileStream.Init(字符串路径、文件模式、文件访问权限、Int32权限、布尔用户权限、文件共享、Int32缓冲大小、文件选项选项、安全属性secAttrs、字符串msgPath、布尔bFromProxy、布尔useLongPath、布尔checkHost)你能帮忙吗?哪一排给你这个例外?请给你的答案解释一下目前为止最好的答案。
try
{
    //Variablen für die Verarbeitung
    string source_file = @"C:\temp\offer.pdf";
    string web_url = "https://stackoverflow.sharepoint.com";
    string library_name = "Documents";
    string admin_name = "admin@stackoverflow.com";
    string admin_password = "Password";

    //Verbindung mit den Login-Daten herstellen
    var sercured_password = new SecureString();
    foreach (var c in admin_password) sercured_password.AppendChar(c);
    SharePointOnlineCredentials credent = new 
    SharePointOnlineCredentials(admin_name, sercured_password);

    //Context mit Credentials erstellen
    ClientContext context = new ClientContext(web_url);
    context.Credentials = credent;

    //Bibliothek festlegen
    var library = context.Web.Lists.GetByTitle(library_name);

    //Ausgewählte Datei laden
    FileStream fs = System.IO.File.OpenRead(source_file);

    //Dateinamen aus Pfad ermitteln
    string source_filename = Path.GetFileName(source_file);

    //Datei ins SharePoint-Verzeichnis hochladen
    FileCreationInformation fci = new FileCreationInformation();
    fci.Overwrite = true;
    fci.ContentStream = fs;
    fci.Url = source_filename;
    var file_upload = library.RootFolder.Files.Add(fci);

    //Ausführen
    context.Load(file_upload);
    context.ExecuteQuery();
    
    //Datenübertragen schließen
    fs.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Fehler");
    throw;
}