The JS end determines whether the current user is in a SharePoint group (where the user is in the ad group and then joins the ad group to the SP)

Source: Internet
Author: User

This article describes how the JS side determines whether the current user is in a SharePoint group (which contains the users in the ad group and then joins the ad group to the SP's groups).

I have summed up the JS terminal to determine the user rights of three ways: http://blog.csdn.net/abrahamcheng/article/details/17447479

But the actual need to determine whether a user is in which group is very common, as mentioned in the previous article, the JS side to determine whether the current user is in which group, only for the user directly to the SP group, for the user in the ad group, and then add the ad group to the SP group situation is powerless. But the real need is to add the user to the ad group, and then add the ad group to the SP group. How to solve this problem? This article will give a solution.

First from the backend, using C # is the case that you can determine whether the current user is in an SP group (the group that contains the user in the ad group and then joins the ad group to the 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; }
Then you can add a ashx to invoke the C # code in the background:

  Public partial class Iscurrentuseringroup:ihttphandler {public bool IsReusable {get {RE Turn 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)); }    }

The josnresult is defined as follows (for passing results to the foreground):

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> ();       }    }

The JS-side calling code is:

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 () {}); }


The JS end determines whether the current user is in a SharePoint group (where the user is in the ad group and then joins the ad group to the SP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.