Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, 1 August 2016

Get logged in user's group details in SharePoint 2010

Hi,

I need to show/hide content based on logged in user's group, for this i need to get logged in user's group details.

There are two way to do this,

1. manually checking user belongs to a group or not, if user belongs to group then show otherwise no
2. get all loggedin user's groups then based on group name we will how content.

Option 1 is very lengthy and its not generic.
Option 2 is very generic and we dont need to worry if new groups added in future.

So, am going to use option 2.

Solution:

1. download the "jquery.SPServices-0.6.2.js" file from codeplex.
2. Use the below code.

<script type="text/javascript" src="../SiteAssets/jquery.min.js"></script>
<script>    !window.jQuery && document.write('<script src="http://code.jquery.com/jquery-1.4.2.min.js"><\/script>');</script>
<script language="javascript" tye="text/javascript" src="../SiteAssets/jquery.SPServices-0.6.2.js"></script>
<script language="javascript" tye="text/javascript">
$(document).ready(function () {
var loggedinUserGroup="";
        $().SPServices({
            operation: "GetGroupCollectionFromUser",
            userLoginName: $().SPServices.SPGetCurrentUser(),
            async: false,
            completefunc: function (xData, Status) {
                //Shows the XML returned from the server
$(xData.responseXML).find("Group").each(function()
{
 //show and hide based on group name ..
});
}

});
});
</script>


Saturday, 10 January 2015

How to hide actions menu from SharePoint ribbon to all user other than site owners group members( or group members)

In this blog you will see how to hide site actions button in the top ribbon if the logged in user not exists in a particular SharePoint group using ECMAScript.


<script type="text/javascript">  
ExecuteOrDelayUntilScriptLoaded(IsUserExists, "sp.js");  
  
var group;  
var users;  
var ctx;   
var groupCollection;  
var user;  
var currentuser;  
  
function IsUserExists() {  
   ctx = SP.ClientContext.get_current();  
   groupCollection = ctx.get_web().get_siteGroups();  
   currentuser=ctx.get_web().get_currentUser();
   // var oGroup = collGroup.getByName(projGroup);  
   group = groupCollection.getByName("UKREDDY SharePointApp Journey Owners");  
   ctx.load(group);  
   ctx.load(currentuser);  
   ctx.executeQueryAsync(Function.createDelegate(this, this.OnGetGroupSuccess), Function.createDelegate(this, OnFailure));  
}  
  
function OnGetGroupSuccess() {  
    users=group.get_users();  
    ctx.load(users);  
    ctx.executeQueryAsync(Function.createDelegate(this, this.OnGetuserSuccess), Function.createDelegate(this, OnFailure));   
}  
  
function OnGetuserSuccess() {     
    var userEnumerator = users.getEnumerator();    
    while (userEnumerator.moveNext()) {  
           var user = userEnumerator.get_current();  
           if (user.get_id() == currentuser.get_id() || currentuser.get_title()=='Administrator' || currentuser.get_title()=='System Account' ) {  
                   document.getElementById('siteactiontd').style.display='inline';    
                   break;  
                }  
                else  
                {                     
                document.getElementById('siteactiontd').style.display='none';
                }  
            }  
         }  
  
function OnFailure(sender, args) {  
   
}  
</script>