zoukankan      html  css  js  c++  java
  • SharePoint 更新word 等文档的内容,包括替换哦。功能强大

       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

    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("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 + "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){}
       
      }
     });
    }
     
  • 相关阅读:
    jquery怎么实现跨域的访问呢?与别人提供的接口连接
    服务器返回数组,data[0]得到的总是不对?如何处理?
    ajax 如何实现页面跳转
    问答精华-IntelliJ IDEA快捷键大全
    setinterval在jQuery里面是怎么使用的。
    background 、backgroundcolor、background-color 我怎么有点分不清了??
    视频最后用使用了function(i,ot)一笔带过,但我看不懂i和ot这2个参数的具体值是怎么获取得到的,能不能说一下参数传递过程?
    3张大图片自动播放
    图片自动加载
    用jQuery之后,之前javascript的一些方法就不能用了吗
  • 原文地址:https://www.cnblogs.com/ahghy/p/2635825.html
Copyright © 2011-2022 走看看