sharepoint跨文档库复制文档

代码比较简单,直接贴代码吧

        /// <summary>
        /// 文档库文档复制,获取文件
        /// </summary>
        public void getCopyDocLibFile(string IDstr)
        {
            try
            {
                //string IDstr = this.Request["id"];//文档ID
                int ID = 0;
                bool IsId = int.TryParse(IDstr, out ID);
                //string filename = this.Request["fn"]; //文件夹名称
                if (IsId)//&& !String.IsNullOrEmpty(filename)
                {
                    string curDocLibUrl = "/Template/TemplateDocument/";//当前文档所在文档库
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        Guid siteID = Web.Site.RootWeb.Site.ID;
                        using (SPSite spSite = new SPSite(siteID))
                        {
                            using (SPWeb spWeb = spSite.AllWebs["Template"])
                            {
                                spWeb.AllowUnsafeUpdates = true;
                                SPList clist = spWeb.GetList(curDocLibUrl);
                                SPListItem item = clist.GetItemById(ID);
                                if (item != null)
                                {
                                    SPFile file = item.File;
                                    string fn = System.IO.Path.GetFileName(file.Name);
                                    byte[] contet = file.OpenBinary();
                                    CopyDocLibFile(fn, contet);//复制文件
                                }
                            }
                        }
                    });
                }
                else
                {
                    this.JResponse.RetCode = 2;
                    this.JResponse.Message = "获取id ,fn 失败!";
                }
            }
            catch (Exception ex)
            {
                this.DebugTrace("lsycHandler.ashx DocLibFileCopy failed: {0}", ex.Message);
                this.JResponse.RetCode = 2;
                this.JResponse.Message = ex.Message;
            }
        }


        /// <summary>
        /// 复制文件到文档库根目录
        /// </summary>
        /// <param name="fn"></param>
        /// <param name="filecontent"></param>
        public void CopyDocLibFile(string fn, byte[] filecontent)
        {
            try
            {
                string docLibName = SPContext.Current.Web.CurrentUser.LoginName;
                string sitetitle = "mydocs";//文档库所在子网站
                Boolean isfileexist = false;
                if (!String.IsNullOrEmpty(docLibName))
                {
                    docLibName = docLibName.IndexOf("\\") >= 0 ? docLibName.Substring(docLibName.IndexOf("\\") + 1) : docLibName;
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        Guid siteID = Web.Site.RootWeb.Site.ID;
                        using (SPSite spSite = new SPSite(siteID))
                        {
                            using (SPWeb spWeb = spSite.AllWebs[sitetitle])
                            {
                                spWeb.AllowUnsafeUpdates = true;
                                SPList list = spWeb.Lists.TryGetList(docLibName);
                                SPDocumentLibrary docLib = (SPDocumentLibrary)list;
                                SPFolder sfolder = docLib.RootFolder;//文档库根目录
                                if (sfolder.Exists)
                                {
                                    isfileexist = true;
                                    SPFile f = sfolder.Files.Add(fn, filecontent, true);
                                    SPListItem item = f.Item;
                                    item.SystemUpdate();
                                }
                            }
                        }
                    });
                }
                int code = isfileexist ? 0 : 2;
                string msg = isfileexist ? ("fn :" + fn) : "文件夹不存在!";
                this.JResponse.RetCode = code;
                this.JResponse.Message = msg;
            }
            catch (Exception ex)
            {
                this.DebugTrace("lsycHandler.ashx CopyDocLibFile failed: {0}", ex.Message);
                this.JResponse.RetCode = 2;
                this.JResponse.Message = ex.Message;
            }
        }
        /// <summary>
        /// 复制文件到指定文档库路径文件夹
        /// </summary>
        /// <param name="fn">文件名</param>
        /// <param name="filecontent">二进制文件</param>
        /// <param name="filename">文档库文件夹路径</param>
        public void CopyDocLibtoFile(string fn, byte[] filecontent, string filename)
        {
            try
            {
                string docLibName = SPContext.Current.Web.CurrentUser.LoginName;
                string sitetitle = "mydocs";//文档库所在子网站
                Boolean isfileexist = false;
                if (!String.IsNullOrEmpty(docLibName))
                {
                    docLibName = docLibName.IndexOf("\\") >= 0 ? docLibName.Substring(docLibName.IndexOf("\\") + 1) : docLibName;
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        Guid siteID = Web.Site.RootWeb.Site.ID;
                        using (SPSite spSite = new SPSite(siteID))
                        {
                            using (SPWeb spWeb = spSite.AllWebs[sitetitle])
                            {
                                spWeb.AllowUnsafeUpdates = true;
                                SPFolder sfolder = spWeb.GetFolder(filename);//这边说明下filename = spWeb.ServerRelativeUrl + "/" + docLibName(文档库名称),具体结构可查看构建文档树那篇
                                if (sfolder.Exists)
                                {
                                    isfileexist = true;
                                    SPFile f = sfolder.Files.Add(fn, filecontent, true);
                                    SPListItem item = f.Item;
                                    item.SystemUpdate();
                                }
                            }
                        }
                    });
                }
                int code = isfileexist ? 0 : 2;
                string msg = isfileexist ? ("fn :" + fn) : "文件路径【" + filename + "】不存在!";
                this.JResponse.RetCode = code;
                this.JResponse.Message = msg;
            }
            catch (Exception ex)
            {
                this.DebugTrace("lsycHandler.ashx CopyDocLibFile failed: {0}", ex.Message);
                this.JResponse.RetCode = 2;
                this.JResponse.Message = ex.Message;
            }
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值