如何将文件上传到 sharepoint 中的文档库? [英] How do you upload a file to a document library in sharepoint?

查看:96
本文介绍了如何将文件上传到 sharepoint 中的文档库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do you programmatically upload a file to a document library in sharepoint?

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

I am currently making a Windows application using C# that will add documents to a document library list.

推荐答案

您可以使用对象模型或 SharePoint Web 服务.

You can upload documents to SharePoint libraries using the Object Model or SharePoint Webservices.

使用对象模型上传:

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

这篇关于如何将文件上传到 sharepoint 中的文档库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆