Share via


Hide Web Part Based on Permissions

Question

Thursday, December 8, 2011 5:32 PM

Hello,

Is there a way to hide a web part based on permissions?  I do not want a user knowing that they are not seing a web part.

For instance - on a page we have setup for one of our vendors there is a confidential information section that we would like to move to a seperate web part.  If users are not in the "Confidential" group then they should not see that this web part exists on the page.

Thanks,

Matt

All replies (6)

Thursday, December 8, 2011 6:05 PM ✅Answered | 1 vote

You have another option if you don't want to code. Enable publishing features and you can use audience targeting.


Thursday, December 8, 2011 6:18 PM ✅Answered

Go to Edit Webpart ,Set the Audience settings only for "confidential group" now this webpart in the page will only be visible to confidential group.not to other users :)


Friday, December 9, 2011 6:57 PM ✅Answered

Take a look at Audience Targeting


Thursday, December 8, 2011 5:48 PM

Please try this

protected override void Render(HtmlTextWriter output) {
//check if user has appropriate permissions
    if (permissions group) {
      base.Render(output);
    }
}

hope it will help

Satyam MCITP, MCPD


Sunday, April 29, 2018 10:25 AM

function hideConfidentialWebpart() {

    getCurrentUserWithDetails().done(function (data) {
        var groupNames = 'Confidential';
        //determine wether current user is a memeber of group(s) 
        var userGroups = data.d.Groups.results;
        var foundGroups = userGroups.filter(function (g) { return groupNames.indexOf(g.LoginName) > -1 });
        if (foundGroups.length == 0) {
            $('#MSOZoneCell_WebPartWP1').hide();    //Replace web part id with your web part id.
        }
    }).fail(function (error) {
        console.log('Error: ' + JSON.stringify(error));
    });
}

function getCurrentUserWithDetails() {
    var endpointUrl = _spPageContextInfo.webServerRelativeUrl + '/_api/web/currentuser/?$expand=groups';

    return $.ajax({
        url: endpointUrl,
        method: "GET",
        contentType: "application/json;odata=verbose",
        headers: {
            "Accept": "application/json;odata=verbose"
        }
    });
}

_spBodyOnLoadFunctionNames.push("hideConfidentialWebpart");

Sunday, April 29, 2018 10:43 AM

Show web part based on permissions

n SharePoint 2013. In other version update the id and text accordingly. You can hide a web part(s) for the users having no permission with this simple jQuery code:

$( "td:contains('There are no documents in this view.')" ).parentsUntil( ".ms-webpartzone-cell" ).hide();

For example, I have a list view web part on a page. Permissions applied on list. If a user does not have permission then web part show message:

There are no documents in this view.

Now I placed a Script Editor web part on the top of all web parts and inserted this script:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
       $(document).ready(function(){
            $( "td:contains('There are no documents in this view.')" ).parentsUntil( ".ms-webpartzone-cell" ).hide();
       });
</script>