.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页面打开之 ...
随机推荐
- 数据库热备份工具innobackupex的安装
Xtrabackup是由percona开发的一个开源软件,此软件可以说是innodb热备工具ibbackup的一个开源替代品. 这个软件是由2个部分组成的:xtrabackup和innobackupe ...
- 过滤表名 & 拼接字符串
/// <summary> /// 分析sql语句中的表名 /// </summary> /// <param name="sql">sql语句 ...
- sqlserver创建,调用 带返回值存取过程
<1>create: ALTER proc [dbo].[common_proc] @sql1 varchar(5000), @sql2 varchar(5000) OUTPUT as ...
- iOS开发XCODE5 SVN配置 使用办法 (转) 收藏一下
标签: xcode5svn xcodesvn使用 xcode自带的svn xcodesvn版本操作 xcode自带svn版本 这两天响应老板要求,把所有代码放到公司的SVN服务器上,按照我的想法肯 ...
- centos 安装 nginx
采用版本 nginx-1.9.8.tar.gz yum -y install pcre-devel yum -y install openssl openssl-devel tar –zxvf ngi ...
- mysql的约束类型
1.主键约束 2.非空约束 3.唯一性约束 4.外键约束 5.默认值约束
- ios 关于状态栏的一些小知识
一.改变状态栏颜色 状态栏分为两种颜色,默认的是黑色,这里想要改为白色: 分为两步: 第一步:在项目中找到plist文件,添加View controller-based status bar appe ...
- VS2008设置断点不命中
网上试了各种办法都不好使,最后想到要修复一下,其实只要重置一下开发环境就好了,具体方法如下: 开始 --> Microsoft Visual Studio 2008 --> Visual ...
- 如何在string.Format()方法中输出大括号
在string.Format参数中,大括号{}是有特殊意义的符号,但是如果我们希望最终的结果中包含大括号({}),那么我们需要怎么做呢?是”\{”吗?很遗憾,运行时,会给你一个Exception的!正 ...
- 更新CocoaPods
终端输入 : sudo gem install -n /usr/local/bin cocoapods –pre 更新了CocoaPods后,在原来的工程中执行了pod install命令后,报这样的 ...