您的位置:首页 > 其它

SharePoint 更新word 等文档的内容,包括替换哦。功能强大

2012-08-13 10:03 375 查看
public void UpdateDocument()
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteColl = new SPSite("http://localhost:8080/"))
{
using (SPWeb web = siteColl.OpenWeb())
{
try
{
SPFile spfile = web.GetFile("http://localhost:8080/Lists/DemoLib/abc.txt");
if (spfile.Exists)
{
byte[] byteArrayFileContentsBefore = spfile.OpenBinary();
if (byteArrayFileContentsBefore.Length > 0)
{
string strFileContentsBefore = enc.GetString(byteArrayFileContentsBefore); //convert byte array to string.
string newStr = strFileContentsBefore.Replace("http://dev:999","http://google.com");
byte[] byteArrayFileContentsAfter = null;
if (!newStr.Equals(""))
{
byteArrayFileContentsAfter = enc.GetBytes(newStr);
spfile.SaveBinary(byteArrayFileContentsAfter); //save to the file.
}
}
}
}
catch (Exception e) { }
}
}
});
}

SPFile has a CopyFile method which can copy the file to a new location. But if there was an existing file on the new location, you can set the overwrite parameter to true to overwrite it. Here is a problem, supposingly there was a workflow already on the file in the new location... when you use CopyFile.. the workflow is lost.. basically it is not an update of the file.. it is infact a delete and re-adding of the file. The following code will overcome this problem

?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: