标签:sharepoint js 20102013 nest ad group user in group
本文讲述 JS 端如何判断当前用户是否在某个SharePoint group (包含用户在AD组,然后将AD组加入到SP的组的情况).
笔者曾经总结过JS端判断用户权限的三种方式: http://blog.csdn.net/abrahamcheng/article/details/17447479
但是实际需求中判断某个用户是否在哪个组是非常常见的,正如前面文章中所说,JS端判断当前用户是否在哪个组,只适用用于将用户直接加到SP group这种情况,针对用户在AD组,然后将AD组加入到SP的组的情况却无能为力。但现实需求中都是将用户加到AD组中,然后将AD组加入到SP group 中。怎么解决这个问题呢? 本文将给出一个解决方案。
首先从后端,使用C#是可以判断当前用户是否在某个SP group 中(包含用户在AD组,然后将AD组加入到SP的组的情况):
public static bool IsCurrentUserInGroup(string groupName) { SPGroup group = null; bool result = false; SPWeb web = SPContext.Current.Web; if (!string.IsNullOrWhiteSpace(groupName)) { try { group = web.Groups[groupName]; } catch (Exception ex) { Helper.WriteException(ex, "IsCurrentUserInGroup"); try { group = web.SiteGroups[groupName]; } catch (Exception innerEx) { Helper.WriteException(innerEx, "IsCurrentUserInGroup"); } } } if (null == group) { result = false; } else { result = web.IsCurrentUserMemberOfGroup(group.ID) || group.ContainsCurrentUser; } return result; }那么就可以添加一个ashx来调用后台的C#代码:
public partial class IsCurrentUserInGroup : IHttpHandler { public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); context.Response.ContentType = "application/json"; var jsonResult = new JosnResult(); string group = context.Request.QueryString.Get("group"); try { if (string.IsNullOrWhiteSpace(group)) { jsonResult.Data.Add(false); } else { jsonResult.Data.Add(Helper.IsCurrentUserInGroup(group)); } } catch (Exception ex) { jsonResult.ErrorMessage = string.Format("Error message:{0}, group name:{1}", ex.Message,group); Helper.WriteException(ex, "IsCurrentUserInGroup.ProcessRequest"); } context.Response.Write(jsonSerializer.Serialize(jsonResult)); } }
public class JosnResult { public string ErrorMessage { get; set; } public List<object> Data { get; set; } public string Position {get; set;} public JosnResult() { this.Data = new List<object>(); } }
ExecuteOrDelayUntilScriptLoaded(CheckIfCurrentUserInGroup, 'sp.js'); function CheckIfCurrentUserInGroup() { var clientContext = new SP.ClientContext.get_current(); var url = clientContext.get_url() + "/_layouts/15/.../IsCurrentUserInGroup.ashx?group=" + encodeURI("SP Group Name"); var data = null; $.ajax({ type: 'GET', data: JSON.stringify(data), dataType: 'json', contentType: 'application/json', url: url }) .done(function (result) { console.log(result.Data); console.log(result.ErrorMessage); if (result.Data.length > 0) { if (result.Data[0]) { // Do some thing if the current user in the specified group } else { // Do some thing if the current user is not in the specified group } } }) .fail(function (errorMessage) { console.error(errorMessage); }) .always(function () { }); }
JS 端判断当前用户是否在某个SharePoint group (包含用户在AD组,然后将AD组加入到SP的组的情况)
标签:sharepoint js 20102013 nest ad group user in group
原文地址:http://blog.csdn.net/abrahamcheng/article/details/38012269