更新sharepoint 2013文档库中的现有文件 [英] Update an existing file in a sharepoint 2013 document library

查看:126
本文介绍了更新sharepoint 2013文档库中的现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我想上传一个文件到文件库但是如果有文件

已存在,名称相同我想更新现有的一个

,我想要上传到Sharepoint 2013文档库。



有没有人可以告诉我怎么做:(

Hello ,

I'd like to upload a file dto document library but if there is a file
already exists with the same name I'd like to update the existing one
with the one i am trying to upload to a Sharepoint 2013 document library .

Is there anyone can tell me how to do it :(

推荐答案

无论如何我解决了自己的问题,

如果其他人寻找相同的东西这是我的解决方案。



private static void uploadDocument(string DocumentLibraryString,Uri SiteUrl,string DocumenttoUploadUrl)

{

使用(SPSite网站=新SPSite(SiteUrl.ToString()))

{

使用(SPWeb Web =网站.OpenWeb())

{

if(!System.IO.File.Exists(DocumenttoUploadUrl))

抛出新的FileNotFoundExcepti on(File not found。,DocumenttoUploadUrl);



SPFolder myLibrary = Web.GetList(DocumentLibraryString).RootFolder;



//准备上传

布尔值replaceExistingFiles = true;

字符串fileName = System.IO.Path.GetFileName(DocumenttoUploadUrl);

FileStream fileStream = File.OpenRead(DocumenttoUploadUrl);



//上传文件

SPFile spFile = myLibrary.Files.Add (fileName,fileStream,replaceExistingFiles);



myLibrary.Update();

if(myLibrary.RequiresCheckout)

{

spFile.CheckIn(自动创建文件签入,SPCheckinType.MajorCheckIn);

sp File.Publish(自动创建文件已发布);

}



}

}



}
Anyway I solved my own problem,
In case of someone else looks for same thing Here is my solution .

private static void uploadDocument(string DocumentLibraryString, Uri SiteUrl, string DocumenttoUploadUrl)
{
using (SPSite Site = new SPSite(SiteUrl.ToString()))
{
using (SPWeb Web = Site.OpenWeb())
{
if (!System.IO.File.Exists(DocumenttoUploadUrl))
throw new FileNotFoundException("File not found.", DocumenttoUploadUrl);

SPFolder myLibrary = Web.GetList(DocumentLibraryString).RootFolder;

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

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

myLibrary.Update();
if (myLibrary.RequiresCheckout)
{
spFile.CheckIn("Automatically created file checked in", SPCheckinType.MajorCheckIn);
spFile.Publish("Automatically created file published");
}

}
}

}


我发布了上面的答案

玩得开心;)
I've posted the answer above
Have fun ;)


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

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