博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SharePoint 更新word 等文档的内容,包括替换哦。功能强大
阅读量:6479 次
发布时间:2019-06-23

本文共 2498 字,大约阅读时间需要 8 分钟。

   public void UpdateDocument()

        {
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite siteColl = new SPSite(""))
                {
                    using (SPWeb web = siteColl.OpenWeb())
                    {
                        try
                        {
                            SPFile spfile = web.GetFile("");
                            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("");
                                    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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private
void
UpdateDocumentForERB_ExecuteCode(SPWeb web,
string
originalFileUrl,
string
targetFileUrl)
{
 
SPSecurity.RunWithElevatedPrivileges(
delegate
()
 
{
  
SPFile OriFile = web.GetFile(originalFileUrl);
  
SPFile TarFile = web.GetFile(targetFileUrl);
  
  
byte
[] byteArrayOriFile = OriFile.OpenBinary();
  
  
TarFile.SaveBinary(byteArrayOriFile);
  
 
});
  
}
 
public
void
UpdateDocument()
{
 
System.Text.ASCIIEncoding enc =
new
System.Text.ASCIIEncoding();
  
 
SPSecurity.RunWithElevatedPrivileges(
delegate
()
 
{
  
using
(SPSite siteColl =
new
SPSite(
""
))
  
{
   
using
(SPWeb web = siteColl.OpenWeb())
   
{
    
try
    
{   
     
SPFile spfile = web.GetFile(
""
);
     
if
(spfile.Exists)
     
{
      
byte
[] byteArrayFileContentsBefore = spfile.OpenBinary();
  
      
if
(byteArrayFileContentsBefore.Length > 0)
      
{
       
string
strFileContentsBefore = enc.GetString(byteArrayFileContentsBefore);
//convert byte array to string.
       
string
newStr = strFileContentsBefore +
"This is the new text added"
;
       
byte
[] byteArrayFileContentsAfter =
null
;
       
if
(!newStr.Equals(
""
))
       
{
        
byteArrayFileContentsAfter = enc.GetBytes(newStr);
        
spfile.SaveBinary(byteArrayFileContentsAfter);
//save to the file.
       
}
      
}
     
}
    
}
    
catch
(Exception e){}
   
  
}
 
});
}
 

转载于:https://www.cnblogs.com/ahghy/archive/2012/08/13/2635825.html

你可能感兴趣的文章
生活杂事--度过十一中秋
查看>>
Pyrex也许是一个好东西
查看>>
WINFORM WPF字体颜色相互转换
查看>>
能力不是仅靠原始积累(三)
查看>>
实战:使用终端服务网关访问终端服务
查看>>
彻底学会使用epoll(一)——ET模式实现分析
查看>>
【Android 基础】Android中全屏或者取消标题栏
查看>>
Xilinx 常用模块汇总(verilog)【03】
查看>>
脱离标准文档流(2)---定位
查看>>
IO流之字符流
查看>>
集合异常之List接口
查看>>
Softmax回归
查看>>
紫书 习题11-11 UVa 1644 (并查集)
查看>>
App工程结构搭建:几种常见Android代码架构分析
查看>>
使用openssl进行证书格式转换
查看>>
ZOJ 3777 Problem Arrangement
查看>>
虚拟机类加载机制
查看>>
Callable和Future
查看>>
installshield12如何改变默认安装目录
查看>>
少用数字来作为参数标识含义
查看>>