Ady*_*dyt 44 c# upload sharepoint
如何以编程方式将文件上载到sharepoint中的文档库?
我目前正在使用C#创建一个Windows应用程序,它将文档添加到文档库列表中.
Hen*_*chi 64
您可以使用对象模型或SharePoint Web服务将文档上载到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();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 12
如果您在此行中收到此错误" 值不在预期范围内 ":
SPFolder myLibrary = oWeb.Folders[documentLibraryName];
Run Code Online (Sandbox Code Playgroud)
使用它来修复错误:
SPFolder myLibrary = oWeb.GetList(URL OR NAME).RootFolder;
Run Code Online (Sandbox Code Playgroud)
永远使用URl来获取列表或其他因为它们是唯一的,名称不是最好的方式;)
小智 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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用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);
}
Run Code Online (Sandbox Code Playgroud)
小智 6
try\n{\n //Variablen f\xc3\xbcr die Verarbeitung\n string source_file = @"C:\\temp\\offer.pdf";\n string web_url = "https://stackoverflow.sharepoint.com";\n string library_name = "Documents";\n string admin_name = "admin@stackoverflow.com";\n string admin_password = "Password";\n\n //Verbindung mit den Login-Daten herstellen\n var sercured_password = new SecureString();\n foreach (var c in admin_password) sercured_password.AppendChar(c);\n SharePointOnlineCredentials credent = new \n SharePointOnlineCredentials(admin_name, sercured_password);\n\n //Context mit Credentials erstellen\n ClientContext context = new ClientContext(web_url);\n context.Credentials = credent;\n\n //Bibliothek festlegen\n var library = context.Web.Lists.GetByTitle(library_name);\n\n //Ausgew\xc3\xa4hlte Datei laden\n FileStream fs = System.IO.File.OpenRead(source_file);\n\n //Dateinamen aus Pfad ermitteln\n string source_filename = Path.GetFileName(source_file);\n\n //Datei ins SharePoint-Verzeichnis hochladen\n FileCreationInformation fci = new FileCreationInformation();\n fci.Overwrite = true;\n fci.ContentStream = fs;\n fci.Url = source_filename;\n var file_upload = library.RootFolder.Files.Add(fci);\n\n //Ausf\xc3\xbchren\n context.Load(file_upload);\n context.ExecuteQuery();\n \n //Daten\xc3\xbcbertragen schlie\xc3\x9fen\n fs.Close();\n}\ncatch (Exception ex)\n{\n MessageBox.Show(ex.Message, "Fehler");\n throw;\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
193868 次 |
| 最近记录: |