.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页面打开之 ...
随机推荐
- c语言-链表VS数组
数组和链表的区别 数组是将元素在内存中连续存放,由于每个元素占用内存相同,可以通过下标迅速访问数组中任何元素.但是如果要在数组中增加一个元素,需要移动大量元素,在内存中空出一个元素的空间,然后将要 ...
- try--catch--finally中return返回值执行的顺序(区别)
1.try块中没有抛出异常,try.catch和finally块中都有return语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static int ...
- Android菜鸟成长记11 -- sqlite数据库的设计和升降级
Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLite 只需要带一个动 ...
- JDK和Tomcat环境变量,以及用MyEclipse新建Web Project测试Tomcat Server
[请尊重原创版权,如需引用,请注明来源及地址] 在此之前一直用的Eclipse挺顺手的,今天突然想换MyEclipse试试,不知安装MyEclipse的时候我选错了什么选项,反正JDK和Tomcat的 ...
- Git相关知识
一些有用的链接: https://www.git-scm.com/ http://nvie.com/posts/a-successful-git-branching-model/ Git开发模式: 建 ...
- Appium移动自动化测试之Java篇
1.环境准备:创建模拟器请参考:http://www.cnblogs.com/mrjade/p/5803131.html 2.新建一个java project,[File]-->[New]--& ...
- videoconverter转换
以前录制的avi用vfw可以解码的,但是现在变成win7系统了,无法解码了.只好用视频转换软件把avi转成无压缩的. 选择losses uncompressed avi,点进去选UYVY就行了.
- C/C++入门基础---指针(2)
5,数组指针的不同含义 int a[5][10]; printf(%d, %d, %d\n", a, a+1, &a+1); //1310392,1310432,1310592 a ...
- Everything Be True
function every(collection, pre) { // Is everyone being true? //return pre; for(var i in collection){ ...
- Unbunt vi 编辑器键盘按键不正确的一次经历与解决方案
在 VMWare 上安装了 Ubuntu Server 14.04.1(这个默认没有安装 vim ) 我在配置网卡的时候,编辑得很心碎.键盘输入和平常vi的响应完全不一样.又不能用 SecureCRT ...