SharePoint 2010 – Check if user in a specific group using Client Object Model

This post is just a modification of the solution provided by Vadim Gremyachev for SharePoint 2013 here. I’ve modified the code to work with SharePoint 2010 (since getByName() is not available in SP 2010)

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
 try{
    var context = new SP.ClientContext.get_current();
    var currentWeb = context.get_web();
 
    var currentUser = context.get_web().get_currentUser();
    context.load(currentUser);
 
    var allGroups = currentWeb.get_siteGroups();
    context.load(allGroups);
 
    context.executeQueryAsync(
       function(sender, args){
          var count = allGroups.get_count();
          for(i = 0; i < count; i++){
             var grp = allGroups.getItemAtIndex(i);
             //provide your group name
             if(grp.get_loginName() == groupName){
                var groupUsers = grp.get_users();
                //load users of the group
                context.load(groupUsers);
                context.executeQueryAsync(
                   function(sender2, args2){
                      var userInGroup = false;
                      var groupUserEnumerator = groupUsers.getEnumerator();
                      while (groupUserEnumerator.moveNext()) {
                         var groupUser = groupUserEnumerator.get_current();
                         //check if current user is in the group members
                         if (groupUser.get_id() == currentUser.get_id()) {
                            userInGroup = true;
                            break;
                         }
                      }
                      OnComplete(userInGroup);
                   },
                   function onFailureGrp(sender2, args2){
                      OnComplete(false);
                   }
               );
            }
         }
       },
       function onFailure(sender, args){
          OnComplete(false);
       }
    );
 }
 catch(e){
    OnComplete(false);
 }
}

And this is how you the function should be invoked –

function IsCurrentUserAManager() 
{
		try{
			IsCurrentUserMemberOfGroup("Managers", function (isCurrentUserInGroup) {
				if(!isCurrentUserInGroup)
				{
				    //user in group		    
				}
			});
		}
		catch(e){			
		}		
}

People picker not resolving entities in SharePoint 2010

In one of the applications I was working lately, one usual day I was informed that the people picker suddenly stopped working across the site. It sounded very weird at first. After reviewing all the changes that were done recently in the site I found that the custom master page the application was using was updated to support IE9 compatibility which was breaking all the people picker controls in all the pages. So changing back it to IE8 fixed the issue.

Not compatible –

<meta http-equiv=”X-UA-Compatible” content=”IE=9″/>

Change it to –

<meta http-equiv=”X-UA-Compatible” content=”IE=8″/>

Opening a SharePoint list item without the navigations in a page using ‘IsDlg’

When you are working with custom master pages with a customized left navigation (done with SP designer) you would have noticed that no matter how many times you set it as “Custom Master Page” and “Default Master Page” in designer the list forms in the site will open with its own left navigation. This is because the list forms will make use of the application master page which will be residing in the Layouts folder and not the default or custom master pages.

In one of my recent projects I had a mail notification feature for a list which will send a mail when a new item is added. The mail will be sent with a link to the item. So I build the URL as http://app/sites/site2/Lists/Invoices/Dispform.aspx?ID=5 which points to the item with the ID as 5 in the list Invoices.

But opening the link as such will show the list item with the old left navigation which we don’t want. So when I checked out the URL opened by SharePoint when “View item” is clicked in the ribbon I found that it opens with a query parameter IsDlg in the URL which mentions whether the item is opened in a dialog. But unfortunately when I tried the same parameter as IsDlg=1 in the link the item opened fine with the left navigation hidden but the scrollbars disabled. Arrgh. Now the SP madness begins. But before I plucked all my hair out I luckily found this brilliant answer by XueQi which mentions that we can either use IsDlg=0 or IsDlg=2 for the scrollbars to work fine.

And the expedition continues.

Filtering list items in SharePoint based on current user and current user groups

Of late, I was working on a list view where I’ve to filter the items based on the current user and the current user groups. Filtering the items by the current user is a straight forward approach where you can select the user field and set the value to [Me]. But there is no provision in the list view filter page where you can filter the items based on the group specified in the field.

But with the help of designer you can manually edit the CAML of the XSLT list viewer webpart in the view page and use the Membership element to filter based on the group. All you have to do is to use CurrentUserGroups as the membership type to show that particular item for the users in that specified group.

<Or>
  <Membership Type=\"CurrentUserGroups\">
    <FieldRef Name=\"AssignedTo\"/>
  </Membership>
  <Eq>
    <FieldRef Name=\"AssignedTo\"></FieldRef>
    <Value Type=\"Integer\">
      <UserID/>
    </Value>
  </Eq>
</Or>

Let’s consider that an item has the filed value as “Kate, John; Approvers”. The item will be shown to the user John kate as well as the members of the group ‘Approvers’. Remember that you have to set the people or group field to allow multiple values as well as selecting objects from SharePoint groups to hold such a value.