Pages

Wednesday 26 August 2015

Sharepoint Master Page Customization based on sharepoint groups

If you want to customize your master page, you may want to do the changes here.

Go to sharepoint site setting page -> Web Designer Galleries -> Master pages and Page Layouts
-> Download seattle.master file and make the changes. Now open the master file using visual studio, 
Here I have removed the "Site Actions" icon based on sharepoint groups.
Include the script file in header section.
 <script type="text/javascript" src="........./Style%20Library/jquery.min.js"></script>
        <script type="text/javascript">
            var clientContext;
            $(document).ready(function () {
                // Make sure the SharePoint script file 'sp.js' is loaded before your
                // code runs.
                SP.SOD.executeFunc('sp.js', 'SP.ClientContext', HideSiteActions);
            });

            function CheckSiteAdmin() {

                var context = new SP.ClientContext.get_current();
                this.website = context.get_web();
                this.currentUser = website.get_currentUser();
                context.load(currentUser);
                context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
            }
            function onQuerySucceeded(sender, args) {
                //Site Collection administrator               
                var value = currentUser.get_isSiteAdmin();
                if (value) {
                    $(".ms-siteactions-root > span > a.ms-core-menu-root").show();
                }
            }
            function onQueryFailed(sender, args) {
                alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
            }

            //Hide Site Action Icon
            function HideSiteActions() {
                //By default hide the site actions icon
                $(".ms-siteactions-root > span > a.ms-core-menu-root").hide();
                //Check current user is site admin
               CheckSiteAdmin();              
                
                //Check Current user is a member of the group Admin & Employee
                IsCurrentUserMemberOfGroup("Admin", function (isCurrentUserInGroup) {
                    if (isCurrentUserInGroup)                      
                        $(".ms-siteactions-root > span > a.ms-core-menu-root").show();
                });
                IsCurrentUserMemberOfGroup("Employee", function (isCurrentUserInGroup) {
                    if (isCurrentUserInGroup)                       
                        $(".ms-siteactions-root > span > a.ms-core-menu-root").show();
                });
                
            }
            //Current User is a member of the group
            function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
                clientContext = SP.ClientContext.get_current();
                var currentWeb = clientContext.get_web();
                var currentUser = clientContext.get_web().get_currentUser();
                clientContext.load(currentUser);
                var allGroups = currentWeb.get_siteGroups();
                clientContext.load(allGroups);
                var group = allGroups.getByName(groupName);
                clientContext.load(group);
                var groupUsers = group.get_users();
                clientContext.load(groupUsers);
                clientContext.executeQueryAsync(OnSuccess, OnFailure);

                function OnSuccess(sender, args) {

                    var userInGroup = false;
                    var groupUserEnumerator = groupUsers.getEnumerator();
                    while (groupUserEnumerator.moveNext()) {
                        var groupUser = groupUserEnumerator.get_current();
                        if (groupUser.get_id() == currentUser.get_id()) {
                            userInGroup = true;
                            break;
                        }
                    }
                    OnComplete(userInGroup);
                }
                function OnFailure(sender, args) {
                    OnComplete(false);
                }
            }


        </script>
Save the master file and rename the file . 
After that upload the renamed document file in the master page gallery.Finally publish the major version of the file.

Now the latest file has been uploaded in the gallery. If you want use the new master page for your sharepoint site collection -> Go to Site Settings -> Look and Feel - > Select Master page 
Here you can select your master file which has been uploaded by you , will be listed in the dropdown. 
Select your master file for both site master page & system master page section.

Finally your changes will be applied for the sharepoint site collection. If the user not in the group or not an site collection administrator, "Site Actions" icon will not be listed in your sharepoint site.

How to Display the Master Page link in Site Settings for SharePoint 2013

If you create a SubSite in SharePoint 2013, you may want it to take on the same look and feel from the parent site. This is easily done with enough permissions. However, by default, the link to the Master Pages was not visible on our site.
If you know the link you can navigate to it directly, but if you don’t breathe SharePoint, it might be a little easier to have the link available.
Follow these steps to get the Master Page link to display:
  1. Navigate to Settings -> Site Settings of the Parent Site.
  2. Under Site Collection Administration click on the Site collection features link.
  3. Enable the SharePoint Server Publishing Infrastructure site collection by clicking the Activate button.
    SharePoint 2013 SharePoint Server Enterprise Site Collection features
  4. Navigate to  Settings -> Site Settings of your SubSite.
  5. Under Site Settings click on the Manage site features link.
  6. Enable the SharePoint Server Publishing Site Features click the Activate button.
    SharePoint 2013 SharePoint Server Publishing
  7. Navigate back to the Site Settings of your SubSite, under Look and Feel section notice a new link called Master page. Click this link.
    SharePoint 2013 Site Settings Master Page
  8. You can now update the settings of your SubSite to inherit the master page from the Parent Site.
    SharePoint 2013 Inherit Master from Parent

No comments:

Post a Comment