[sharepoint]文档库,文件夹授权
写在前面
在项目中用到了文档库授权的方法,这里将查询到的方式总结一下。
涉及到的方法
在逻辑中用到的方法。
/// <summary>
/// 获取sharepoint站点角色定义 rest api
/// </summary>
/// <param name="hostWebUrl"></param>
/// <param name="strAPI"></param>
/// <param name="userName"></param>
/// <param name="pwd"></param>
/// <param name="domain"></param>
/// <returns></returns>
private static ArrayList GetRoleDefinition(string hostWebUrl, string strAPI, string userName, string pwd, string domain)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamReader sr = null;
strAPI = hostWebUrl + strAPI;
ArrayList lstRoleDefinition = new ArrayList();
try
{
request = (HttpWebRequest)HttpWebRequest.Create(strAPI);
request.Credentials = new NetworkCredential(userName, pwd, domain);
request.Method = "GET";
request.Accept = "application/json;odata=verbose";
using (response = (HttpWebResponse)request.GetResponse())
{
using (sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
JObject jobj = JObject.Parse(sr.ReadToEnd());
var results = jobj["d"]["results"];
foreach (var item in results)
{
lstRoleDefinition.Add(new
{
Id = Convert.ToInt32(item["Id"]),
Description = item["Description"] != null ? item["Description"].ToString() : "",
Hidden = Convert.ToBoolean(item["Hidden"]),
Name = item["Name"] != null ? item["Name"].ToString() : "",
Order = Convert.ToInt32(item["Order"]),
RoleTypeKind = Convert.ToInt32(item["RoleTypeKind"]),
BasePermissions = new
{
High = item["BasePermissions"]["High"] != null ? item["BasePermissions"]["High"].ToString() : "",
Low = item["BasePermissions"]["Low"] != null ? item["BasePermissions"]["Low"].ToString() : ""
}
});
}
}
}
}
catch (WebException ex)
{
throw ex;
}
return lstRoleDefinition;
}
/// <summary>
/// 将用户添加到sharepoint站点。
/// </summary>
/// <param name="hostWebUrl"></param>
/// <param name="addUserName"></param>
/// <param name="userName"></param>
/// <param name="pwd"></param>
/// <param name="domain"></param>
/// <returns></returns>
private static object AddUserToSharePointSite(string hostWebUrl, string addUserName, string userName, string pwd, string domain)
{
if (hostWebUrl.Contains("https"))
{
//如果请求的站点是https的url,则使证书的认证返回true。
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, sslPolicyErrors) =>
{
return true;
});
}
try
{
ClientContext spContext = new ClientContext(hostWebUrl);
spContext.Credentials = new NetworkCredential(userName, pwd, domain);
Web web = spContext.Web;
string loginName = @"i:0#.w|" + domain + "" + addUserName;
User user = web.EnsureUser(loginName);
//需要load,不然拿不到user的属性
spContext.Load(user);
spContext.ExecuteQuery();
return new { Email = user.Email, Id = user.Id, LoginName = user.LoginName, Title = user.Title };
}
catch (WebException ex)
{
throw ex;
}
}
public static string GetContextinfo(string hostWebUrl, string userName, string pwd, string domain)
{
HttpWebRequest contextInfoRequest = null;
HttpWebResponse endpointResponse = null;
StreamReader sr = null;
string strJson = string.Empty;
try
{
//获取contextinfo
contextInfoRequest = (HttpWebRequest)HttpWebRequest.Create(hostWebUrl + "/_api/contextinfo");
contextInfoRequest.Method = "POST";
contextInfoRequest.Credentials = new NetworkCredential(userName, pwd, domain);
contextInfoRequest.Accept = "application/json;odata=verbose";
contextInfoRequest.ContentLength = ;
using (endpointResponse = (HttpWebResponse)contextInfoRequest.GetResponse())
{
using (sr = new StreamReader(endpointResponse.GetResponseStream(), Encoding.UTF8))
{
strJson = sr.ReadToEnd();
JObject jobj = JObject.Parse(strJson);
return jobj["d"]["GetContextWebInformation"]["FormDigestValue"].ToString();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 将用户添加到sharepoint站点。rest api方式
/// </summary>
/// <param name="hostWebUrl"></param>
/// <param name="addUserName"></param>
/// <param name="userName"></param>
/// <param name="pwd"></param>
/// <param name="domain"></param>
/// <returns></returns>
private static string AddUserToSharePointSite(string hostWebUrl, bool isRestAPI, string addUserName, string userName, string pwd, string domain)
{
string data = "{ '__metadata': { 'type': 'SP.User' }, 'LoginName':'i:0#.w|membership|" + addUserName + "'}";
string strAPI = "_api/Web/siteusers";
HttpWebRequest request = null;
StreamReader sr = null;
HttpWebResponse response = null; try
{
request = (HttpWebRequest)HttpWebRequest.Create(hostWebUrl + "/" + strAPI);
request.Method = "POST";
if (!string.IsNullOrEmpty(data))
{
byte[] buffer = Encoding.UTF8.GetBytes(data);
request.ContentLength = buffer.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(buffer, , buffer.Length);
}
}
else
{
request.ContentLength = ;
}
request.Credentials = new NetworkCredential(userName, pwd, domain);
request.Accept = "application/json;odata=verbose";
request.ContentType = "application/json;odata=verbose";
request.Headers.Add("X-RequestDigest", GetContextinfo(hostWebUrl, userName, pwd, domain));
using (response = (HttpWebResponse)request.GetResponse())
{
using (sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return sr.ReadToEnd();
}
}
}
catch (Exception ex)
{ throw ex;
}
}
/// <summary>
/// 为文件夹授权
/// </summary>
/// <param name="currentCoworkLibrary"></param>
/// <param name="strCheckUser"></param>
private static void AssignToUserReadPermissionToFolder(string hostWebUrl, string folderServerRelativeUrl, string strCheckUser, string userName, string pwd, string domain)
{
//https,取消https证书认证
if (hostWebUrl.Contains("https"))
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, sslPolicyErrors) =>
{
return true;
});
}
try
{
ServicePointManager.Expect100Continue = false;
ClientContext spContext = new ClientContext(hostWebUrl); spContext.Credentials = new NetworkCredential(userName, pwd, domain);
Web web = spContext.Web;
string loginName = @"i:0#.w|" + domain + "\\" + strCheckUser; Principal user = web.EnsureUser(loginName);
spContext.ExecuteQuery();
Folder folder = web.GetFolderByServerRelativeUrl(folderServerRelativeUrl);
var roleDefinition = spContext.Site.RootWeb.RoleDefinitions.GetByType(RoleType.Reader);
var roleBindings = new RoleDefinitionBindingCollection(spContext) { roleDefinition };
spContext.ExecuteQuery();
if (folder != null)
{
folder.ListItemAllFields.BreakRoleInheritance(true, false);
folder.ListItemAllFields.RoleAssignments.Add(user, roleBindings);
}
}
catch (Exception ex)
{
throw ex;
}
}
另外加个rest api,这种方式也可以进行授权。
_api/web/lists/getByTitle('" + LibraryName + "')/RoleAssignments/addroleassignment(principalid=" + userId + ",roledefid=" + roleDefinitionId + ")"
总结
在对文档库或者文件夹进行授权的过程,总是磕磕碰碰,不管怎么,最后还是实现了。总结在这里,方便以后查询
[sharepoint]文档库,文件夹授权的更多相关文章
- SharePoint文档库文件夹特殊字符转义
当我们在SharePoint网站文档库中新建文件夹时包含了~ " # % & * : < > ? / \ { | }字符时(一共15个), 或者以.开头或者结束,或者包含 ...
- 解决SharePoint文档库文件在搜索结果页面显示的标题和文档的标题不一致问题(search result)
问题表现: SharePoint 2013 爬网后,搜索一个文档,虽然搜到了,但是显示有点问题,如图: 原因分析: 造成该问题的原因是,该文档除了本身有一个名称外,在文档metadata的title属 ...
- sharepoint rest api 创建文档库 文件夹
function createFolder() { var requestHeaders = { "Accept": "application/json;odata=ve ...
- 在Outlook中查看预览SharePoint文档库的文档
本文概况 阅读时间: 约2分钟 适用版本:SharePoint Server 2010及以上 面向用户:普通用户,管理员 难度指数:★★☆☆☆ 在日常工作中,总有一些常用的文档需要经常打开查看,其实我 ...
- SharePoint 文档库实现文件夹拖放到文档库
打开文档库-> 选择文件夹-> 在Ribbon中选择“库(list)”-> 在右边可以看到打开方式-> 选择用资源管理器打开-> 在新打开的资源管理器中可能实现对文夹的拖 ...
- [sharepoint]rest api文档库文件上传,下载,拷贝,剪切,删除文件,创建文件夹,修改文件夹属性,删除文件夹,获取文档列表
写在前面 最近对文档库的知识点进行了整理,也就有了这篇文章,当时查找这些接口,并用在实践中,确实废了一些功夫,也为了让更多的人走更少的弯路. 系列文章 sharepoint环境安装过程中几点需要注意的 ...
- 修改Sharepoint 文档库列表点击Excel文件默认跳转到Excel Service服务 xlviewer.aspx页面
在Sharepoint 文档库中,当点击库中的一个Excel文件时,Sharepoint默认为转跳到Excel Services上,无论是Sharepoint 的是否开启了Excel Service, ...
- 360安全卫士造成Sharepoint文档库”使用资源管理器打开“异常
备注:企业用户还是少用360为妙 有客户反馈:部门里的XP SP2环境客户机全部异常,使用资源管理器打开Sharepoint文档库,看到的界面样式很老土,跟本地文件夹不一样 ...
- Atitit 基于图片图像 与文档混合文件夹的分类
Atitit 基于图片图像 与文档混合文件夹的分类 太小的文档(txt doc csv exl ppt pptx)单独分类 Mov10KminiDoc 但是可能会有一些书法图片迁移,因为他们很微小,需 ...
随机推荐
- Python3基本语法
#编码 ''' 默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: cp-1252 ...
- Android记事本开发01
今天: 学习一下Android的基本知识,了解一下记事本开发大概需要哪些知识. 昨天: 无 遇到的问题:
- realloc在aarch64_be-gcc的奇怪表现
最近遇到一个使用aarch64_be-gcc编译的ssh服务器出现不能通过ssh1协议使用密钥+passphrase不能正常登陆的问题. (⊙o⊙)…不要奇怪为啥还在用SSH1,我也在奇怪.. 一顿捣 ...
- LINQ to Entities 不识别方法“System.Guid Parse(System.String)”,因此该方法无法转换为存储表达式。
LINQ to Entities 不识别方法"System.Guid Parse(System.String)",因此该方法无法转换为存储表达式. linq 中不能转换类型
- P3141 [USACO16FEB]围栏Fenced In_Platinum
题目描述 Farmer John has realized that many of his cows are strangely agoraphobic (being fearful of larg ...
- BZOJ5301 [Cqoi2018]异或序列 【莫队】
题目链接 BZOJ5301 题解 莫队水题 BZOJ400AC纪念 #include<algorithm> #include<iostream> #include<cst ...
- 在有道词典程序文件夹发现一个后缀名为sql的数据库(SQLite)
缘起 在清理电脑磁盘的时候,看一看各安装文件夹有占用了多大容量,发现有道词典居然达140MB了,于是进去看看. 发现个有趣的文件:XXX.sql. 首先我们看一看它的安装文件夹的结构: Dict └─ ...
- 7月10日day2总结
今天学习过程和小结 上午: java项目建立方法,maven Tomcat的部署和MySQL的安装与权限. 分配时中datanote和namenote DataNode 作用 1Data Node以数 ...
- hdu 1465 不容易系列之一(错排模板)
不容易系列之一 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- codechef AUG17 T1 Chef and Rainbow Array
Chef and Rainbow Array Problem Code: RAINBOWA Chef likes all arrays equally. But he likes some array ...