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

本文共 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

你可能感兴趣的文章
3009.脚本作业—l201.7.0编写一个脚本用于检测IP地址(递进版7)
查看>>
Linux命令随记
查看>>
readExceptionWithFileNotFoundExceptionFromParcel
查看>>
系统服务
查看>>
转贴:v4l2视频流控制
查看>>
序列化对象--C#
查看>>
linux安装mysql数据库
查看>>
ioS开发知识(二十)
查看>>
Linux 文件与目录管理+用户管理命令
查看>>
C#中父类和子类之间相互转换
查看>>
keepalived 自生存活监测
查看>>
python paramiko模块讲解
查看>>
端口聚合配置
查看>>
各大门户网站全局CSS样式定义
查看>>
《Linux菜鸟入门2》mail服务
查看>>
Python字节码是什么?
查看>>
监控易Linux Agent 安装说明
查看>>
RedHat/CentOS 7如何更改网卡名为eth0 eth1......
查看>>
Zabbix2.4安装部署、编码及翻译不准确解决方案
查看>>
PowerQuery实战:数据转置的综合应用
查看>>