The Black Knight Sings

Songs about SharePoint and other adventures by Per Jakobsen


Date: # Monday, July 20, 2009

Title: List all SharePoint groups a user belongs to


This is just a small Utility requested on one of the SharePoint Newsgroups

It uses the UserGroup web service to list all the SharePoint Groups a user belongs to in a specified Site Collection

It's a small console application which requires two parameters:
···The first parameter is the Url of the site collection including protocol (example: http://localhost)
···The second parameter is the full username including Domain (example: MOSSWORK\user)

using System;

using System.IO;

using System.Xml;

using ListUsersGroups.ug;

 

namespace ListUsersGroups

{

    class Program

    {

        static int Main(string[] args)

        {

            if (args.Length < 2)

                return help();

            try

            {

                UserGroup usergroup = new UserGroup();

                usergroup.UseDefaultCredentials = true;

                usergroup.Url = args[0]+"/_vti_bin/usergroup.asmx";

                XmlNode groupXml =usergroup.GetGroupCollectionFromUser(args[1]);

                XmlDocument doc = new XmlDocument();

                doc.Load(new XmlNodeReader(groupXml));

                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

                nsmgr.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/directory/");

                foreach (XmlNode group in doc.SelectNodes("//sp:Groups/sp:Group", nsmgr))

                    Console.WriteLine(group.Attributes["Name"].Value);

 

            }

            catch (Exception ex)

            {

                Console.WriteLine("{0}\r\n{1}", ex.Message, ex.StackTrace);

            }

            return 0;

        }

 

        private static int help()

        {

            Console.WriteLine(@"{0} ", Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]));

            Console.WriteLine(@"  Site-url: Url of site collection including protocol (example http://localhost)");

            Console.WriteLine(@"  username: Full username including domain (example MOSSWORK\user)");

            return -1;

        }

    }

}


This execuable can be found here and the source code here



Monday, July 20, 2009 2:01:05 PM (Romance Standard Time, UTC+01:00)  #    Comments [1]

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. And all information or programs are without warranty. Use at your own risk


© Copyright 2010 Send mail to the author(s) Per Jakobsen Feed your aggregator (RSS 2.0)