Hide Ribbon Element based on Group Permission

Hide Edit button when we do which is present on left hand side,then what happens when we go to documnt lib and do drag drop it doesnt work and unable to acccess files and list settings. to avoid this we check if edit id is null then do not execute thhe code​                
function abcd()
{
//hiding the ribbon at the start itself so end users don't have a lag
 document.getElementById("ctl00_ctl53_PageStateActionButton").style.display= "none";
// The 1st parameter is a string of the name of the group.
// -- It can be a partial part of the name.
// The 2nd parameter is a delegate function and run on completion.
// -- Make sure to provide the function with one var.
// -- This will either be true or false depending if the user is in the group!

IsCurrentUserMemberOfGroup("Communities Owners", function (isCurrentUserInGroup) {
    if(isCurrentUserInGroup) //if he is a part of group, make ribbon visible
    {
  
 document.getElementById("ctl00_ctl53_PageStateActionButton").style.display= "inline-block";        
    }
});
}



function IsCurrentUserMemberOfGroup(strGroupName, functionComplete) {

        //Setup Vars
        currentContext  = null;
        currentWeb  = null;
        allGroups   = null;
        leaderGroup     = null;
        currentUser     = null;
        groupUsers  = null;

        //Get an instance of the Client Content.
        currentContext = new SP.ClientContext.get_current();

        //Grab the client web object.
        currentWeb = currentContext.get_web();

        //Get the current user object
        currentUser = currentContext.get_web().get_currentUser();
        currentContext.load(currentUser);

        //Setup the groupColletion.
        allGroups = currentWeb.get_siteGroups();
        currentContext.load(allGroups);

        //Now populate the objects above.
        currentContext.executeQueryAsync(
            Function.createDelegate(this, GetAllGroupsExecuteOnSuccess),
            Function.createDelegate(this, ExecuteOnFailure)
        );

        // GroupCollection - Load - SUCCESS
        function GetAllGroupsExecuteOnSuccess(sender, args) {

            // CHECK THE GROUPS
            // Time to Enumerate through the group collection that was returned.
            var groupEnumerator = allGroups.getEnumerator();

            // Loop for the collection.
            while (groupEnumerator.moveNext()) {

                //Grab the Group Item.
                var group = groupEnumerator.get_current();
                if (group.get_title().indexOf(strGroupName) > -1) {

                    // Now that we have the group let's grab the list of users.
                    groupUsers = group.get_users();
                    currentContext.load(groupUsers);
                    currentContext.executeQueryAsync(
                        Function.createDelegate(this, SingleGroupExecuteOnSuccess),
                        Function.createDelegate(this, ExecuteOnFailure)
                    );
                }
            }
        }

        // Single Group - Load - SUCCESS
        function SingleGroupExecuteOnSuccess(sender, args) {

            // Time to setup the Enumerator
            var groupUserEnumerator = groupUsers.getEnumerator();

            // This is the flag to set to true if the user is in the group.
            var boolUserInGroup = false;

            // and start looping.
            while (groupUserEnumerator.moveNext()) {

                //Grab the User Item.
                var groupUser = groupUserEnumerator.get_current();

                // and finally. If a Group User ID Matches the current user ID then they are in the group!
                if (groupUser.get_id() == currentUser.get_id()) {
                    boolUserInGroup = true;
                }
            }

            //Run the delegate function with the bool;
            functionComplete(boolUserInGroup);
        }

        // GroupCollection or Single Group - Load - FAILURE
        function ExecuteOnFailure(sender, args) {
            //Run the delegate function and return false because there was no match.
            functionComplete(false);
        }
    }
   
   
    //Script below to display welcome tag:
    ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var currentUser;
function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}

function onQuerySucceeded() {
    document.getElementById('userTitle').innerHTML = currentUser.get_title();
}

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

function pqr()
{
var a= document.getElementById("ctl00_ctl53_PageStateActionButton")

if(a===null)
{


}
else
{
_spBodyOnLoadFunctionNames.push("abcd");

}

}
//_spBodyOnLoadFunctionNames.push("abcd");
_spBodyOnLoadFunctionNames.push("pqr");

Comments