Sharepoint文档库操作

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.IO;

namespace StatutesPublication
{
    public class DocLib
    {
        private string siteUri;//网站集
        private string webUri;//网站
        private string docLibUri;//文档库
        SPList list = null;

        public DocLib(string siteUri, string webUri, string docLibUri)
        {
            this.siteUri = siteUri;
            this.webUri = webUri;
            this.docLibUri = docLibUri;
        }

        //打开文档库并返回其引用
        public SPList Open()
        {
            SPSite site = null;
            SPWeb web = null;
            SPList list = null;
            //打开网站集       
            try
            {
                site = new SPSite(siteUri);
            }
            catch (Exception ex)
            {
                throw new Exception("网站集错误:" + ex.Message);
            }
            //打开文档库所在网站
            try
            {
                web = site.OpenWeb(webUri);
            }
            catch (Exception ex)
            {
                throw new Exception("网站错误:" + ex.Message);
            }
            //打开文档库
            try
            {
                web.Lists.IncludeRootFolder = true;//SPList.RootFolder的访问默认没有开启
                list = web.Lists[docLibUri];
            }
            catch (Exception ex)
            {
                throw new Exception("文档库错误:" + ex.Message);
            }
            return list;
        }

        public string Upload(string filePath,string dirName,string fileName)
        {
            try
            {
                list = this.Open();
            }
            catch
            {
                return "文档库打开错误!";
            }
            SPFolder rootFolder = list.RootFolder;
            if (dirName == "root")//直接保存在根目录
            {
                if(FileExists("root",fileName))
                    rootFolder.Files.Delete(rootFolder.Url + "/" + fileName);
                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] content = new byte[fs.Length];
                fs.Read(content, 0, (int)fs.Length);
                rootFolder.Files.Add(fileName, content);
                fs.Close();
            }
            else
            {
                SPFolder subFolder = null;
                subFolder = rootFolder.SubFolders.Add(rootFolder.Url + "/" + dirName);
                subFolder = rootFolder.SubFolders[dirName];
                if(FileExists(dirName,fileName))
                    subFolder.Files.Delete(subFolder.Url + "/" + fileName);
                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] content = new byte[fs.Length];
                fs.Read(content, 0, (int)fs.Length);
                subFolder.Files.Add(fileName, content);
                fs.Close();
            }
            return "Success";
        }

        public bool FileExists(string dirName, string fileName)
        {
            try
            {
                list = this.Open();
            }
            catch
            {
                return false;
            }
            SPFolder rootFolder = list.RootFolder;
            if (dirName == "root")
            {
                foreach (SPFile file in rootFolder.Files)
                {
                    if (file.Name == fileName)
                        return true;
                }
            }
            else
            {
                SPFolder subFolder = null;
                subFolder = rootFolder.SubFolders.Add(rootFolder.Url + "/" + dirName);
                subFolder = rootFolder.SubFolders[dirName];
                foreach (SPFile file in subFolder.Files)
                {
                    if (file.Name == fileName)
                        return true;
                }
            }
            return false;
        }

        public void FileRemove(string dirName, string fileName)
        {
            try
            {
                list = this.Open();
            }
            catch
            {
                return;
            }
            SPFolder rootFolder = list.RootFolder;
            if (dirName == "root")
            {
                foreach (SPFile file in rootFolder.Files)
                {
                    if (file.Name == fileName)
                        file.Delete();
                }
            }
            else
            {
                SPFolder subFolder = null;
                subFolder = rootFolder.SubFolders.Add(rootFolder.Url + "/" + dirName);
                subFolder = rootFolder.SubFolders[dirName];
                foreach (SPFile file in subFolder.Files)
                {
                    if (file.Name == fileName)
                        file.Delete();
                }
            }
        }

        //读取用户
        public string GetUser()
        {
            SPSite site = null;
            SPWeb web = null;
            //打开网站集       
            try
            {
                site = new SPSite(siteUri);
            }
            catch (Exception ex)
            {
                //throw new Exception("网站集错误:" + ex.Message);
                return String.Empty;
            }
            //打开文档库所在网站
            try
            {
                web = site.OpenWeb(webUri);
            }
            catch (Exception ex)
            {
                //throw new Exception("网站错误:" + ex.Message);
                return String.Empty;
            }
            string spUser = web.CurrentUser.Name;
            return spUser;
        }
    }
}
  最近在使用Sharepoint开发网站,写了一个简单的操作文档库的类,供大家分享。已实现功能:
1.上传文件到文档库
2.删除文档库中的文件
3.读取当前登录用户
功能比较简单且还在完善中,希望大家多交流

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值