sharepoint list 文档上传和删除

最近项目需要对sharepoint 文件操作,于是自己写了一个简单的工具类分享出来:

 

namespace Microsoft.SharePoint
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    public class DocLibManager
    {
        public static void UploadFile(SPList list, string filePath, string dirName, string fileName)
        {
            SPFolder rootFolder = GetSPFolder(list, dirName);
            SPFileCollection files = rootFolder.Files;

            if (FileExists(list, dirName, fileName))
            {
                rootFolder.Files.Delete(SPUrlUtility.CombineUrl(rootFolder.Url ,fileName));
            }
            using (FileStream fs = new FileStream(filePath, FileMode.Open))
            {
                rootFolder.Files.Add(fileName, fs);
            }


        }

        public static bool FileExists(SPList list, string dirName, string fileName)
        {
            SPFolder rootFolder = GetSPFolder(list, dirName);
            string fileurl = SPUrlUtility.CombineUrl(list.ParentWebUrl, SPUrlUtility.CombineUrl(rootFolder.Url, fileName));
            SPFile file = list.ParentWeb.GetFile(fileurl);
            if (file != null) {
                return file.Exists;
            }
            return false;
        }

        public static void RemoveFile(SPList list, string dirName, string fileName)
        {
            SPFolder rootFolder = GetSPFolder(list, dirName);
            if (!string.IsNullOrEmpty(fileName))
            {
                string fileurl = SPUrlUtility.CombineUrl(list.ParentWebUrl, SPUrlUtility.CombineUrl(rootFolder.Url, fileName));
                SPFile file = list.ParentWeb.GetFile(fileurl);
                if (file != null && file.Exists)
                    file.Delete();
            }
            else
            {
                rootFolder.Delete();
            }
         
        }
        public static SPFolder GetSPFolder(SPList list, string dirName)
        {
            SPFolder rootFolder = list.RootFolder;
            if (string.IsNullOrEmpty(dirName) || dirName == rootFolder.Name)
            {

            }
            else
            {
                SPFolder subFolder = null;
                subFolder = rootFolder.SubFolders.Add(SPUrlUtility.CombineUrl(rootFolder.Url, dirName));
                subFolder = rootFolder.SubFolders[dirName];
                rootFolder = subFolder;
            }
            return rootFolder;
        }

    }
}

 

 调用代码:

 string listurl = "http://mossvr/docs/Documents/Forms/AllItems.aspx";
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(listurl))
                {
                    using (SPWeb web = site.OpenWeb(listurl))
                    {
                        SPList list = web.GetList(listurl);
                        DocLibManager.UploadFile(list, @"C:\DocLib.txt", "root", "test.txt");
                        DocLibManager.RemoveFile(list, "root", "test.txt");
                    }
                }
            });

 如果是sharepoint server 建议考虑性能 http://blog.csdn.net/ma_jiang/article/details/6553190

posted on 2014-07-11 23:07  dz45693  阅读(2280)  评论(0编辑  收藏  举报

导航