博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sharepoint list 文档上传和删除
阅读量:5303 次
发布时间:2019-06-14

本文共 3173 字,大约阅读时间需要 10 分钟。

最近项目需要对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

转载于:https://www.cnblogs.com/majiang/p/3838678.html

你可能感兴趣的文章
Oracle事务
查看>>
String类中的equals方法总结(转载)
查看>>
属性动画
查看>>
标识符
查看>>
给大家分享一张CSS选择器优选级图谱 !
查看>>
Win7中不能调试windows service
查看>>
通过httplib2 探索的学习的最佳方式
查看>>
快来熟练使用 Mac 编程
查看>>
Node.js 入门:Express + Mongoose 基础使用
查看>>
一步步教你轻松学奇异值分解SVD降维算法
查看>>
使用pager进行分页
查看>>
UVA - 1592 Database
查看>>
Fine Uploader文件上传组件
查看>>
javascript中的传递参数
查看>>
objective-c overview(二)
查看>>
python查询mangodb
查看>>
consonant combination
查看>>
驱动的本质
查看>>
Swift的高级分享 - Swift中的逻辑控制器
查看>>
Swagger简单介绍
查看>>