This sample code demonstrates how to programmatically list all SharePoint Portal Server 2003 or Windows SharePoint Services v2.0 site groups and the users they contain (exception handling omitted). For simplicity the example code below demonstrates how to write this information to a basic text file.

In C#

using System;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

StreamWriter Writer = File.CreateText(@"C:\Temp\GroupsAndUsers.txt");

try
{
   SPWeb PortalWeb = SPControl.GetContextWeb(Context);
   SPRoleCollection PortalGroups = PortalWeb.Roles;

   foreach(SPRole Group in PortalGroups)
   { 
      Writer.WriteLine(String.Format("Group: {0}", Group.Name));

      foreach(SPUser User in Group.Users)
      {
        Writer.WriteLine(String.Format("- {0}", User.Name);
      }
   }
}
finally
{
   Writer.Close();
}

In VB.NET

Imports System
Imports System.IO
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls

Dim Writer As StreamWriter = File.CreateText("C:\Temp\GroupsAndUsers.Text")

Try
   Dim PortalWeb As SPWeb = SPControl.GetContextWeb(Context)
   Dim PortalGroups As SPRoleCollection = PortalWeb.Roles

   For Each Group As SPRole In PortalGroups
      Writer.WriteLine(String.Format("Group: {0}", Group.Name))
 
      For Each User As SPUser In Group.Users
         Writer.WriteLine(String.Format("- {0}", User.Name
      Next
   Next
Finally
   Writer.Close()
End Try
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview

View Lamont Harrington's profile on LinkedIn

Microsoft Employee


Calendar

<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

On this page...

Tags


Archives