如何以编程方式将文件上传到sharepoint中的文档库?
我目前正在使用C#创建一个Windows应用程序,它将文档添加到文档库列表中。
答案 0 :(得分:63)
您可以使用对象模型或SharePoint Webservices将文档上载到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();
}
}
答案 1 :(得分:12)
如果您在此行中收到此错误“值不在预期范围内”:
SPFolder myLibrary = oWeb.Folders[documentLibraryName];
使用它来修复错误:
SPFolder myLibrary = oWeb.GetList(URL OR NAME).RootFolder;
使用URl来获取列表或其他列表,因为它们是唯一的,名称不是最佳方式;)
答案 2 :(得分:8)
string filePath = @"C:\styles\MyStyles.css";
string siteURL = "http://MyDomain.net/";
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");
}
}
}
答案 3 :(得分:5)
作为webservices的替代方法,您可以使用FrontPage RPC API中的put document调用。这具有额外的好处,使您能够在与文件数据相同的请求中提供元数据(列)。明显的缺点是协议更加模糊(与文档很好的Web服务相比)。
有关解释Frontpage RPC使用的参考应用程序,请参阅CodePlex上的SharePad项目。
答案 4 :(得分:5)
使用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);
}
答案 5 :(得分:0)
tutar.sort(function(a,b) {
if (parseInt(a.Tutar.toLowerCase()) < parseInt(b.Tutar.toLowerCase())) return -1;
if (parseInt(a.Tutar.toLowerCase()) > parseInt(b.Tutar.toLowerCase())) return 1;
});
答案 6 :(得分:0)
我使用本文允许c#访问共享点站点。
http://www.thesharepointguide.com/access-office-365-using-a-console-application/
基本上,您创建ClientId和ClientSecret键以使用c#访问该站点。
希望这可以为您提供帮助!