.net sharepoint文档库操作
/// <summary>
/// 检查文档库
/// </summary>
/// <param name="siteUrl"></param>
/// <param name="listName"></param>
/// <returns></returns>
public static bool IsSPDocExists(string mainSiteUrl,string siteUrl, string documentName)
{ using (SPSite site = new SPSite(mainSiteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists.TryGetList(documentName);
if (list != null)
{
return true;
}
return false; }
}
else
{
foreach (var item in site.AllWebs.ToList())
{
if (item.Url == siteUrl)
{ //当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
SPList list = web.Lists.TryGetList(documentName);
if (list != null)
{
return true;
}
return false; }
}
}
}
return false;
} }
检查文档库
/// <summary>
/// 创建文档库
/// </summary>
/// <param name="siteUrl"></param>
/// <param name="documentName"></param>
/// <returns></returns>
public static string CreateDocumentLibrary(string mainSiteUrl, string siteUrl, string documentName)
{
string result = "false"; SPSecurity.RunWithElevatedPrivileges(delegate
{
try
{ if (SPSite.Exists(new Uri(mainSiteUrl)))
{
using (SPSite site = new SPSite(mainSiteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{ web.AllowUnsafeUpdates = true;
SPList spdoc = web.Lists.TryGetList(documentName);
if (spdoc == null)
{
web.Lists.Add(documentName, "上传附件文档库", SPListTemplateType.DocumentLibrary); }
web.Update();
web.AllowUnsafeUpdates = false;
result = "true";
}
}
else
{
var allWebs = site.AllWebs.ToList();
foreach (var item in allWebs)
{
if (item.Url == siteUrl)
{
//当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
web.AllowUnsafeUpdates = true;
SPList spdoc = web.Lists.TryGetList(documentName);
if (spdoc == null)
{
web.Lists.Add(documentName, "上传附件文档库", SPListTemplateType.DocumentLibrary); }
web.Update();
web.AllowUnsafeUpdates = false;
result = "true";
} }
}
}
}
}
result = "true";
}
catch (Exception ex)
{
result = "CreateDocumentLibrary|" + ex.Message;
} }); return result; }
创建文档库
//子文件夹断继承,并remove掉所有组
public static void SetFolderPermission(string mainSiteUrl,string siteUrl,string documentName, string folderName)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(siteUrl)))
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{ web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
SPFolder folder = null;
for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == folderName)
{
folder = sPList.RootFolder.SubFolders[i];
}
}
folder.Item.BreakRoleInheritance(true);
SPGroupCollection groupCollection = web.Groups;
foreach (SPGroup var in groupCollection)
{
if (!var.Name.Contains("项目文档查看"))
{
//remove掉除(项目文档查看)组的所有其他组
folder.Item.RoleAssignments.Remove(var); }
}
//移除掉所有人员权限
SPUserCollection userCollection = web.Users;
foreach (SPUser user in userCollection)
{
folder.Item.RoleAssignments.Remove(user);
}
sPList.Update();
web.AllowUnsafeUpdates = false;
}
}
}
}); } public static void SetFolderPermissionClearly(string mainSiteUrl, string siteUrl, string documentName, string folderName)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(siteUrl)))
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{ web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
SPFolder folder = null;
for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == folderName)
{
folder = sPList.RootFolder.SubFolders[i];
}
}
folder.Item.BreakRoleInheritance(true);
SPGroupCollection groupCollection = web.Groups;
foreach (SPGroup var in groupCollection)
{
folder.Item.RoleAssignments.Remove(var);
}
//移除掉所有人员权限
SPUserCollection userCollection = web.Users;
foreach (SPUser user in userCollection)
{
folder.Item.RoleAssignments.Remove(user);
}
sPList.Update();
web.AllowUnsafeUpdates = false;
}
}
}
}); } public static void DeleteSubFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName, string strFolderNameSub)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(siteUrl)))
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true; for (int i = ; i < sPList.RootFolder.SubFolders[strFolderName].SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[strFolderName].SubFolders[i].Name == strFolderNameSub)
{
sPList.RootFolder.SubFolders[strFolderName].SubFolders[i].Delete();
}
}
sPList.Update();
web.AllowUnsafeUpdates = false;
}
}
}
});
} public static void CreateFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName, string currentUser)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(mainSiteUrl)))
{
using (SPSite site = new SPSite(mainSiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true;
for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
{
return;
}
}
SPListItem sPListItem = sPList.Items.Add(sPList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);
sPListItem["上传者"] = currentUser;
sPListItem.Update();
sPList.Update();
web.AllowUnsafeUpdates = false;
}
}
}
});
} public static void CreateSubFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName, string strFolderNameSub)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(siteUrl)))
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true; for (int i = ; i < sPList.RootFolder.SubFolders[strFolderName].SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[strFolderName].SubFolders[i].Name == strFolderNameSub)
{
return;
}
}
SPListItem sPListItemSub = sPList.Items.Add(sPList.RootFolder.SubFolders[strFolderName].ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderNameSub);
sPList.Update();
sPListItemSub.Update();
web.AllowUnsafeUpdates = false;
}
}
}
});
}
其他
/// <summary>
/// 删除文件夹
/// </summary>
/// <param name="documentName">文档库名</param>
/// <param name="strFolderName">文件夹名</param>
public static void DeleteFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(siteUrl)))
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true; for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
{
sPList.RootFolder.SubFolders[i].Delete();
}
}
sPList.Update();
web.AllowUnsafeUpdates = false;
}
}
}
});
}
删除文件夹
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="documentName">文档库名</param>
/// <param name="strFolderName">文件夹名</param>
public static void CreateFolder(string mainSiteUrl,string siteUrl,string documentName, string strFolderName)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(mainSiteUrl)))
{
using (SPSite site = new SPSite(mainSiteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true;
for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
{
return;
}
}
SPListItem sPListItem = sPList.Items.Add(sPList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);
sPList.Update();
sPListItem.Update();
web.AllowUnsafeUpdates = false;
}
}
else
{
var allWebs = site.AllWebs.ToList();
foreach (var item in allWebs)
{
if (item.Url == siteUrl)
{
//当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true;
for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
{
return;
}
}
SPListItem sPListItem = sPList.Items.Add(sPList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);
sPList.Update();
sPListItem.Update();
web.AllowUnsafeUpdates = false;
} }
}
} }
}
});
}
创建文件夹
/// <summary>
/// 单个附件上传
/// </summary>
/// <param name="documentName"></param>
/// <param name="folderName"></param>
/// <param name="folderSubName"></param>
/// <param name="file"></param>
/// <param name="filename"></param>
public static string SaveToFormLibrary(string mainSiteUrl, string siteUrl, string documentName, string folderName, byte[] file, string filename)
{
string ff = "";
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(mainSiteUrl)))
{
// Add the file to a document library
using (SPSite site = new SPSite(mainSiteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
//web.Folders[documentName].SubFolders[folderName].Files.Add(filename, file, true);
SPFile files = web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Add(filename, file, true);
ff = files.ServerRelativeUrl;
web.AllowUnsafeUpdates = false;
web.Close();
}
}
else
{
var allWebs = site.AllWebs.ToList();
foreach (var item in allWebs)
{
if (item.Url == siteUrl)
{
//当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
web.AllowUnsafeUpdates = true;
//web.Folders[documentName].SubFolders[folderName].Files.Add(filename, file, true);
SPFile files = web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Add(filename, file, true);
ff = files.ServerRelativeUrl;
web.AllowUnsafeUpdates = false;
web.Close();
} }
}
} site.Close();
}
}
});
return ff;
}
单个附件上传
/// <summary>
/// 单个附件删除
/// </summary>
/// <param name="documentName"></param>
/// <param name="folderName"></param>
/// <param name="folderSubName"></param>
/// <param name="file"></param>
/// <param name="filename"></param>
public static void DeleteFile(string mainSiteUrl, string siteUrl, string documentName, string folderName, string urlOfFile)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(mainSiteUrl)))
{
// Add the file to a document library
using (SPSite site = new SPSite(mainSiteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true; try
{
web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Delete(urlOfFile);
}
catch
{ }
web.AllowUnsafeUpdates = false;
web.Close(); }
}
else
{
var allWebs = site.AllWebs.ToList();
foreach (var item in allWebs)
{
if (item.Url == siteUrl)
{
//当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
web.AllowUnsafeUpdates = true; try
{
web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Delete(urlOfFile);
}
catch
{ }
web.AllowUnsafeUpdates = false;
web.Close();
} }
}
} site.Close();
}
}
});
}
单个附件删除
public static string UploadFile(string mainSiteUrl, string siteUrl, string documentName, string folderName, byte[] file, string filename)
{
string result = "false";
string ff = "";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
using (SPSite site = new SPSite(siteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true; SPList list = web.Lists[documentName];
string templateDispName = folderName;
SPFolder folder = null;
try
{
folder = list.RootFolder.SubFolders[templateDispName];
}
catch (Exception)
{
folder = list.RootFolder.SubFolders.Add(templateDispName);
folder.Update();
} SPFile newFile = folder.Files.Add(filename, file, true);
newFile.Update();
ff = newFile.ServerRelativeUrl;
site.AllowUnsafeUpdates = false;
web.AllowUnsafeUpdates = false;
web.Close(); result = "true";
}
}
else
{
var allWebs = site.AllWebs.ToList();
foreach (var item in allWebs)
{
if (item.Url == siteUrl)
{
//当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true; SPList list = web.Lists[documentName];
string templateDispName = folderName;
SPFolder folder = null;
try
{
folder = list.RootFolder.SubFolders[templateDispName];
}
catch (Exception)
{
folder = list.RootFolder.SubFolders.Add(templateDispName);
folder.Update();
} SPFile newFile = folder.Files.Add(filename, file, true);
newFile.Update();
ff = newFile.ServerRelativeUrl;
site.AllowUnsafeUpdates = false;
web.AllowUnsafeUpdates = false;
web.Close(); result = "true";
} }
}
}
}
}
catch (Exception ex)
{
result = "error|(UploadFile)" + ex.Message;
}
});
return ff + "~" + result;
}
mainSiteUrl="http://192.168.110.51:8000/";
SiteUrl="http://192.168.110.51:8000/PPCS/";
sharepoint中删除文档库
库-》库设置-》删除此文档库
.net sharepoint文档库操作的更多相关文章
- 在Outlook中查看预览SharePoint文档库的文档
本文概况 阅读时间: 约2分钟 适用版本:SharePoint Server 2010及以上 面向用户:普通用户,管理员 难度指数:★★☆☆☆ 在日常工作中,总有一些常用的文档需要经常打开查看,其实我 ...
- SharePoint 文档库实现文件夹拖放到文档库
打开文档库-> 选择文件夹-> 在Ribbon中选择“库(list)”-> 在右边可以看到打开方式-> 选择用资源管理器打开-> 在新打开的资源管理器中可能实现对文夹的拖 ...
- 360安全卫士造成Sharepoint文档库”使用资源管理器打开“异常
备注:企业用户还是少用360为妙 有客户反馈:部门里的XP SP2环境客户机全部异常,使用资源管理器打开Sharepoint文档库,看到的界面样式很老土,跟本地文件夹不一样 ...
- 修改Sharepoint 文档库列表点击Excel文件默认跳转到Excel Service服务 xlviewer.aspx页面
在Sharepoint 文档库中,当点击库中的一个Excel文件时,Sharepoint默认为转跳到Excel Services上,无论是Sharepoint 的是否开启了Excel Service, ...
- SharePoint文档库,如何在新窗口打开中的文件
默认情况下,点击文档库中的文件是在当前浏览器中打开的(如果你设置的是在客户端软件打开,则不符合本文情况).那么如果让他在新窗口中打开呢? 这里需要借助jQuery,关于如何将jQuery集成到Shar ...
- 如何为SharePoint文档库、文件夹、文件单独设置权限
在这里使用截图的方式简单描述两个问题:设置SharePoint Server文档库权限和文档库中的文件夹权限 一.设置SharePoint Server文档库权限 Figure 1 - 打开文档库后, ...
- SharePoint文档库文件夹特殊字符转义
当我们在SharePoint网站文档库中新建文件夹时包含了~ " # % & * : < > ? / \ { | }字符时(一共15个), 或者以.开头或者结束,或者包含 ...
- 解决SharePoint文档库文件在搜索结果页面显示的标题和文档的标题不一致问题(search result)
问题表现: SharePoint 2013 爬网后,搜索一个文档,虽然搜到了,但是显示有点问题,如图: 原因分析: 造成该问题的原因是,该文档除了本身有一个名称外,在文档metadata的title属 ...
- 解决SharePoint 文档库itemadded eventhandler导致的上传完成后,编辑页面保持报错的问题,错误信息为“该文档已经被编辑过 the file has been modified by...”
在文档库中添加itemadded 后,在上传文件后,会自动打开文档属性的编辑页面,在保存的时候就会报错,说这个文档已经被编辑过了.这是应为默认itemadded实践是异步执行的,会在edit页面打开之 ...
随机推荐
- VB.NET TextBox 只允许输入1-100之间的整数 简洁篇
Dim Str As String = "" Private Sub txtRecond_KeyUp(sender As System.Object, e As System.Wi ...
- MFC 不让程序显示在任务栏上
如果是对话框程序直接在对话框的 初始化时,修改样式 ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW); 但是对于多文档或是单文档,则没有对应的对话框样式需 ...
- datetimepicker一个不错的日历android特效
datetimepicker一个不错的日历效,选中和选择日历效果都很不错, 实用的时候直接可以把datetimepicker-library这个引入到项目,调用的地方在实现 TimePickerDia ...
- Chrome浏览器Network面板http请求时间分析
Chrome浏览器开发者工具Network窗口下,可以查看下载各组件所需的具体时间 根据上表进行简要分析-- Stalled(阻塞) 浏览器对同一个主机域名的并发连接数有限制,因此如果当前的连接数已经 ...
- MATLAB与C#混合编程 之 double与MWArray 、MWNumericArray 转化
double acc_ang_d;//待计算组 MWNumericArray eng_input_array = new MWNumericArray(acc_ang_d); MWArray eng_ ...
- map创建JSONObject对象
public static void mapToJSONObject(){ Map<String, Object> map = new HashMap<String,Object&g ...
- Device nodes and device stacks
[Device nodes and device stacks] 链接:https://msdn.microsoft.com/en-us/library/windows/hardware/ff5547 ...
- 身份证校验,前台js校验,后台java校验
js校验: var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内 ...
- C# 科学计数法转换成数字
/// <summary> /// 判断输入的数是否是科学计数法.如果是的话,就会将其换算成整数并且返回,否则就返回false. /// </summary> /// < ...
- Nginx-->基础-->理论-->nginx进程模型
一.nginx的进程模型基础 如上图,是nginx的基本进程模型. 1.nginx的master进程与worker进程关系 nginx的master进程负责worker进程的管理,包括创建worker ...