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 http://<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: http://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.


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


 


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


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


Comments (10)

  1. Once I got a requirement to add values to the created by &amp; modified by columns through a web service.

  2. joy says:

    I have a problem while using this web service; the part of code I used is given below.

    UserGroup test = new UserGroup();

    test.Credentials = System.Net.CredentialCache.DefaultCredentials;

    test.PreAuthenticate = false;

    test.Url = "http://wss/_vti_bin/UserGroup.asmx&quot;;

    XmlNode xuser = null;

    xuser = test.GetAllUserCollectionFromWeb();

    In this line i got an error. The error is like this.

    “The request failed with HTTP status 401: Unauthorized.”

  3. sowmyancs says:

    which authentication that you are using in http://wss ?

    have you enabled anonymous access in that site ?

  4. joy says:

    No, I didn’t enabled anonymous access in that site.

  5. joy says:

    No, I didn’t enabled anonymous access in that site.

  6. justin says:

    Can you use this web service to add users to a site collection?  Such as in the API you would perform the following:

    site = new SPSite(URL);

    site.RootWeb.AllUsers.Add(user.LoginName, user.Email, user.Name, user.Notes);

    site.RootWeb.Update();

  7. John Shaw says:

    what is the 'set sample value' (ns4:emailXml) syntax for what you fill in the data receive connection in SharePoint Designer for GetUserLoginFromEmail. I have tried Domainusername, user@Domain.com. In order to complete the dataconnection.

    I consistently can not complete the connection without getting the following error after trying to fill i nthe sample value: "The SOAP response indicates that an error occurred on the server:

    Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.

    <detail><errorstring xmlns="schemas.microsoft.com/…/">Data at the root level is invalid. Line 1, position 1.</errorstring></detail>

    "

  8. Anju says:

    hi

    when am trying to get the roles from group,

    it is throwing soap server exception

    claysys081UserGroups.UserGroup getRolesFromGroups = new claysys081UserGroups.UserGroup();

                   getRolesFromGroups.Credentials = new NetworkCredential(txtSourceUser.Text, txtSourcePass.Text);

                   getRolesFromGroups.Url = string.Concat(txtSourceSite.Text + "/_vti_bin/UserGroup.asmx");

                   XmlNode xmlGroupRoleColletion = getRolesFromGroups.GetRoleCollectionFromGroup(strGroup);

    this is the code i used getrolecollectionfromgroup method returns such an exception

  9. MarcusAnderson.com.au says:

    Well its a nice little routine but there is one major problem with all this: you talk about the Web Service but use the Server Objects instead. No Web Service calls to be seen anywhere !!! This code only runs on the Sharepoint Server. A Web Service services a Client – typically on another machine. You need to be using Microsoft.SharePoint.Client to call a Sharepoint Web Service API call.