0

I am trying to filter some webparts based on user's group in my SharePoint Default.aspx page. To do this, I want to define if a user is member of a group first and then use a conditional statement to show the webpart or hide it.

<% condition = CheckCurrentUserMembership("Group Name"); 
if(condition) { %>

<div>
    <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="my_requests_View" Title="loc:full">

    </WebPartPages:WebPartZone>
</div>

<% } %>

I wanted to know what is the code that can work like CheckCurrentUserMembership function.

1

You can use REST api for this:

function getData(siteurl) {
var endpoint = siteurl+"/_api/web/currentuser/groups";
$.ajax({
    url: endpoint,
    method: "GET",
    headers: {
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "Accept": "application/json; odata=verbose",
        "Content-Type": "application/json; odata=verbose"
    },
    success: function (data) {
        var data= data.d.results;
        var len = data.length;        
        for (i=0; i<len; ++i) {
             console.log(data[i].Title); //outputs group title
        }
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
});
}

This will give you the title of all groups of the current user. You can modify that with what you want to retrieve.

| improve this answer | |
0

You can use Target Audience property of webpart. This is out of box and available in webpart properties in "Advance".

enter image description here

In Target Audience, you can add a SharePoint group and webpart will be visible for that group only.

For reference:Target content to specific audience

Still if you want know whether user belongs to specific group. You can use below code:

SPWeb site = SPContext.Current.Web;
SPGroup managerGroup = site.Groups["Your_Group_Name"];

bool isManager = site.IsCurrentUserMemberOfGroup(managerGroup.ID);

Reference

| improve this answer | |
  • I am writing an application and I want to avoid any after development configuration. Is there any way that I can make this configuration inside my .aspx file where I put my WebParts codes? – Arman Fatahi Jul 20 '15 at 6:17
0

If you want to check if the current user logged in is a member of some group, you can use the code below:

    public static bool CheckAdminUser()
    {
        var isMember = false;
        try
        {
            using (var oSite = new SPSite(PortalUrl))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    var spGroup = oWeb.Groups["AdminGroup"];
                    if (spGroup != null)
                    {
                        isMember = oWeb.IsCurrentUserMemberOfGroup(spGroup.ID);
                    }
                }
            }
            return isMember;
        }
        catch (Exception ex)
        {
            return isMember;
        }
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.