39
A
回答
58
您可以使用对象模型或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();
}
}
5
作为替代web服务,您可以使用从FrontPage RPC API的调用put document。这还具有使您能够在与文件数据相同的请求中提供元数据(列)的额外好处。显而易见的缺点是该协议有点晦涩(与非常好的Web服务相比)。
有关解释使用Frontpage RPC的参考应用程序,请参阅CodePlex上的SharePad项目。
7
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");
}
}
}
11
,如果你得到这个错误 “值不在预期范围内” 在这一行:
SPFolder myLibrary = oWeb.Folders[documentLibraryName];
改用这种修正这个错误:
SPFolder myLibrary = oWeb.GetList(URL OR NAME).RootFolder;
因为它们是唯一的,所以始终使用URl来获取列表或其他名称,因为名称不是最好的方式;)
3
使用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);
}
相关问题
- 1. 如何将文件上传到SharePoint文档库
- 2. 使用HTTP将文件上传到Sharepoint(WSS 3.0)文档库PUT
- 3. 上传文件到SharePoint文档库的元数据
- 4. 将同名文档上传到SharePoint 2010
- 5. 使用CronTab将文档上传到SharePoint
- 6. 使用WebClient将文档上载到SharePoint文档库中的特定文件夹
- 7. 使用csv文件将文档上传到Sharepoint站点
- 8. 将文件复制到SharePoint中的文档库中
- 9. 如何将Sharepoint文档库中的文档返回给用户?
- 10. 哪个Windows帐户用于将文件上传到SharePoint文档库?
- 11. 一次可以将多少个文件上传到SharePoint文档库?
- 12. 使用低级Web服务将文档上传到Sharepoint库
- 13. 使用IRM将文档上传到SharePoint库
- 14. 将SharePoint文档库集成到ASP.NET中
- 15. 将上传更改为SharePoint 2010中的其他文档库
- 16. 如何使用Perl将文档上传到SharePoint?
- 17. 如何使用Java将文档上传到SharePoint?
- 18. 修改SharePoint文档库中的文件
- 19. 更新Sharepoint文档库中的文件
- 20. Sharepoint文档库中的Eml文件
- 21. Sharepoint 2010上传文档
- 22. 如何将文档发布到SharePoint 2013中的另一个文档库o365
- 23. 将文档上传到Google文档的文档中
- 24. 上传到文档库
- 25. 需要将文档上传到MOSS文档库的系统
- 26. 使用Powershell将文件上传到SharePoint
- 27. 上传文件到SharePoint Server
- 28. Sharepoint文档库
- 29. Sharepoint从文档库中删除文件
- 30. 将文件上传/同步到文档库
Chadworthington,SPSite的是命名空间Microsoft.SharePoint程序的一部分,所以你需要添加引用Microsoft.SharePoint.dll的。 假设你正在开发服务器上,可以在这里找到DLL:C:\ Program Files \ Common Files \ Microsoft Shared \ Web服务器扩展\ 12 \ ISAPI \ Microsoft.SharePoint.dll – 2009-06-04 09:23:46
等待一段时间...此代码只会在加入农场的箱子里工作,对吗?在任何其他框中,它需要使用http://msdn.microsoft.com/en-us/library/ee857094.aspx – Ariel 2011-05-05 02:35:34
@Ariel你是对的。客户端API是从外部访问SharePoint服务器的方式。另请参阅http://msdn.microsoft.com/en-us/library/ee537564.aspx – 2011-08-05 16:19:05