Some cool functionalities of UserGroup.asmx webservice

SharePoint provides more than 21 out-of-the-box web services to do various functionalities. I have written some posts on Lists.asmx web service very long back. Here we can take a look at UserGroup.asmx web service.

The Users and Groups Web service provides methods for working with users and groups in Windows SharePoint Services.

To access this Web service set a Web reference to https://<site>/_vti_bin/UserGroup.asmx.

These are the web methods of UserGroup webservice (very less J) !

UserGroup

The following operations are supported. For a formal definition, please review the Service Description.

· AddGroup

· AddGroupToRole

· AddRole

· AddRoleDef

· AddUserCollectionToGroup

· AddUserCollectionToRole

· AddUserToGroup

· AddUserToRole

· GetAllUserCollectionFromWeb

· GetGroupCollection

· GetGroupCollectionFromRole

· GetGroupCollectionFromSite

· GetGroupCollectionFromUser

· GetGroupCollectionFromWeb

· GetGroupInfo

· GetRoleCollection

· GetRoleCollectionFromGroup

· GetRoleCollectionFromUser

· GetRoleCollectionFromWeb

· GetRoleInfo

· GetRolesAndPermissionsForCurrentUser

· GetRolesAndPermissionsForSite

· GetUserCollection

· GetUserCollectionFromGroup

· GetUserCollectionFromRole

· GetUserCollectionFromSite

· GetUserCollectionFromWeb

· GetUserInfo

· GetUserLoginFromEmail

· RemoveGroup

· RemoveGroupFromRole

· RemoveRole

· RemoveUserCollectionFromGroup

· RemoveUserCollectionFromRole

· RemoveUserCollectionFromSite

· RemoveUserFromGroup

· RemoveUserFromRole

· RemoveUserFromSite

· RemoveUserFromWeb

· UpdateGroupInfo

· UpdateRoleDefInfo

· UpdateRoleInfo

· UpdateUserInfo

Here

I am not going to explain about all the web methods. My requirement was, I want to know list of all users in a site collection, and their Groups and the roles. If any user added directly to the site without through a group then his roles in that site. Also, I want to get these details in the web level in a detailed report.

Report will output the result something like this.

*************************************************************************

DOMAIN1\sowmyans was added under group. Please see the group(s) and its roles below

*************************************************************************

Group :Source Owners

Role : Full Control

Description : Has full control.

Type : Administrator

BasePermissions : FullMask

Before thinking about writing code, I just checked the available SharePoint web services. Then I found UserGroup.asmx web service and that guy was pretty cool to satisfy my all needs. After that, I directly created a proxy class of this web service and implemented this requirement in a .NET console based application.

This code is self explanatory and I hope it won’t confuse you anymore J

(I forgot to dispose XMLNodeReader objects L – don’t forget to do this while using this code)

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Text;
    4: using Microsoft.SharePoint;
    5: using UserGroupCheck.localhost;
    6: using System.Xml;
    7: using System.IO;
    8:  
    9: namespace UserGroupCheck
   10: {
   11:     class Program
   12:     {
   13:         private static localhost.UserGroup objUserGroup = null;
   14:         private static TextWriter oTxtWriter = null;
   15:         static void Main(string[] args)
   16:         {
   17:             try
   18:             {
   19:                 oTxtWriter = new StreamWriter("UserGroupsAndRolesReport.txt");
   20:  
   21:                 Console.WriteLine("*************************************************************************************************");
   22:                 Console.WriteLine("Enter your choice for  generating a report for getting  user's groups and related roles information :");
   23:                 Console.WriteLine("*************************************************************************************************");
   24:                 Console.WriteLine("1. Site Collection Level");
   25:                 Console.WriteLine("2. Specific website level (Eg: only from a subsite which has unique permission)");
   26:                 Console.WriteLine("3. For a specific user (In this case you enter the login name of that user)");
   27:                 string strChoice = Console.ReadLine();
   28:  
   29:                 Console.WriteLine("Enter your site URL (eg: https://servername/sitename)");
   30:                 string strURL = Console.ReadLine();
   31:  
   32:                 objUserGroup = new UserGroup();
   33:                 objUserGroup.Credentials = System.Net.CredentialCache.DefaultCredentials;
   34:                 objUserGroup.Url = strURL + "/_vti_bin/UserGroup.asmx";
   35:  
   36:                 switch (strChoice)
   37:                 {
   38:                     case "1": DecideAboutUsers(true);
   39:                         break;
   40:                     case "2": DecideAboutUsers(false);
   41:                         break;
   42:                     case "3": Console.WriteLine("Please enter login name of the user");
   43:                         RetrieveGroupsAndRoles(Console.ReadLine());
   44:                         break;
   45:                 }
   46:  
   47:                 oTxtWriter.Close();
   48:                 Console.WriteLine(@"operation has been completed successfully...please check the report - \UserGroupCheck\bin\Debug\UserGroupsAndRolesReport.txt");
   49:                 
   50:             }
   51:             catch (Exception ex)
   52:             {
   53:                 Console.WriteLine(ex.Message);
   54:             }
   55:             Console.ReadLine();
   56:         }
   57:  
   58:         private static void DecideAboutUsers(bool IsSiteCollection)
   59:         {
   60:             XmlNode xUsers = null;
   61:             if(IsSiteCollection)
   62:                 xUsers = objUserGroup.GetUserCollectionFromSite();
   63:             else
   64:                 xUsers = objUserGroup.GetAllUserCollectionFromWeb();
   65:  
   66:             XmlNodeList xUsersList = xUsers.ChildNodes;
   67:             foreach (XmlNode xUser in xUsersList)
   68:             {
   69:                 XmlNodeReader oUserReader = new XmlNodeReader(xUser);
   70:                 while (oUserReader.Read())
   71:                 {
   72:                     if (oUserReader["LoginName"] != null)
   73:                     {
   74:                         string strLoginName = oUserReader["LoginName"].ToString();
   75:                         if (strLoginName != "NT AUTHORITY\\local service")
   76:                         {
   77:                             if (strLoginName != "SHAREPOINT\\system")
   78:                                 RetrieveGroupsAndRoles(strLoginName);
   79:                         }
   80:                     }
   81:                 }
   82:                 oUserReader.Close();
   83:             }
   84:         }
   85:  
   86:         private static void RetrieveGroupsAndRoles(string strLoginName)
   87:         {
   88:             XmlNode xGroups = objUserGroup.GetGroupCollectionFromUser(strLoginName);
   89:             XmlNodeList xGroup = xGroups.ChildNodes;
   90:  
   91:             // Finding the groups for this user and then will find the group roles
   92:             foreach (XmlNode oNode in xGroup)
   93:             {
   94:                 if (oNode.HasChildNodes)
   95:                 {
   96:                     oTxtWriter.WriteLine("*************************************************************************************************");
   97:                     oTxtWriter.WriteLine(strLoginName + " was added under group. Please see the group(s) and its roles below");
   98:                     oTxtWriter.WriteLine("*************************************************************************************************");
   99:                     XmlNodeReader oGreoupReader = new XmlNodeReader(oNode);
  100:                     while (oGreoupReader.Read())
  101:                     {
  102:                         if (oGreoupReader["Name"] != null)
  103:                         {
  104:                             string strGroup = oGreoupReader["Name"].ToString();
  105:                             oTxtWriter.WriteLine("Group :" + strGroup);
  106:                             oTxtWriter.WriteLine();
  107:  
  108:                             // find the group roles
  109:                             XmlNode xGroupRoles = objUserGroup.GetRoleCollectionFromGroup(strGroup);                            
  110:                             XmlNodeList xGroupRoleList = xGroupRoles.ChildNodes;
  111:                             foreach (XmlNode oGroupRole in xGroupRoleList)
  112:                             {
  113:                                 XmlNodeReader oGroupRoleReader = new XmlNodeReader(oGroupRole);
  114:                                 while (oGroupRoleReader.Read())
  115:                                 {
  116:                                     if (oGroupRoleReader["Name"] != null)
  117:                                     {
  118:                                         oTxtWriter.WriteLine("Role : " + oGroupRoleReader["Name"].ToString());
  119:                                         oTxtWriter.WriteLine("Description : " + oGroupRoleReader["Description"].ToString());
  120:                                         oTxtWriter.WriteLine("Type : " + oGroupRoleReader["Type"].ToString());
  121:                                         oTxtWriter.WriteLine("BasePermissions : " + oGroupRoleReader["BasePermissions"].ToString());
  122:                                         oTxtWriter.WriteLine();
  123:                                     }
  124:                                 }
  125:                             }
  126:                         }
  127:                     }
  128:                 }
  129:  
  130:                 // else part means this user doesn't have a group but he will be having some roles, so we can list out that roles
  131:                 else
  132:                 {
  133:                     oTxtWriter.WriteLine("*************************************************************************************************");
  134:                     oTxtWriter.WriteLine(strLoginName + " was not added under group. His roles and corrusponding details are listed below");
  135:                     oTxtWriter.WriteLine("*************************************************************************************************");
  136:                     XmlNode xUserRoles = objUserGroup.GetRoleCollectionFromUser(strLoginName);
  137:                     XmlNodeList xUserRoleList = xUserRoles.ChildNodes;
  138:                     foreach (XmlNode xUserRole in xUserRoleList)
  139:                     {
  140:                         XmlNodeReader oUserRoleReader = new XmlNodeReader(xUserRole);
  141:                         while (oUserRoleReader.Read())
  142:                         {
  143:                             if (oUserRoleReader["Name"] != null)
  144:                             {
  145:                                 oTxtWriter.WriteLine("Role : " + oUserRoleReader["Name"].ToString());
  146:                                 oTxtWriter.WriteLine("Description : " + oUserRoleReader["Description"].ToString());
  147:                                 oTxtWriter.WriteLine("Type : " + oUserRoleReader["Type"].ToString());
  148:                                 oTxtWriter.WriteLine("BasePermissions : " + oUserRoleReader["BasePermissions"].ToString());
  149:                                 oTxtWriter.WriteLine();
  150:                             }
  151:                         }
  152:                     }
  153:                 }
  154:  
  155:             }
  156:  
  157:         }
  158:     }
  159: }

You will get complete list of all 21 web services from the following MSDN article.

https://msdn.microsoft.com/en-us/library/ms479390.aspx

 

To want to know more about UserGroup class you can refer the below article.https://msdn.microsoft.com/en-us/library/ms772575.aspx

More reference for other web methods. https://msdn.microsoft.com/en-us/library/ms412944.aspx