Click here to Skip to main content
15,867,308 members
Articles / Productivity Apps and Services / Sharepoint

Dynamic Distribution Group Viewer (Sharepoint 2010 Webpart)

Rate me:
Please Sign up or sign in to vote.
4.11/5 (3 votes)
8 Dec 2010CPOL1 min read 26.8K   140   1   5
Allows users to see who belongs to an exchange dynamic distribution group

Introduction

Lots of organizations / companies use dynamic distribution groups in Exchange. While these groups allow for low administrative overhead, they don't allow users to know who the email is going to until the email has been sent. This Sharepoint webpart allows users to see who is in a group before the email is sent.

Using the Code

Before you begin, you must have the Exchange Management Console installed on your server. If you are planning to deploy this in Sharepoint, you will also need to edit your web.config to read:

XML
<trust level="Full" originUrl="" />

In Visual Studio 2010, create a new Empty SharePoint Project. Add a new Visual Web Part Item. Add the System.Management.Automation reference to your project. Include the following 3 items in your using:

C#
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;

We need to open the Exchange management PowerShell snappin:

C#
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = rsConfig.AddPSSnapIn
("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open();

Next, we need to send the cmdlet into powershell. In this case, I am asking to Get-DynamicDistributionGroup with the groupName being a variable I had the user select from a drop down.

C#
Pipeline pipeLine = myRunSpace.CreatePipeline();
Command myCommand = new Command("Get-DynamicDistributionGroup");
CommandParameter identityParam = new CommandParameter("Identity", groupName);
myCommand.Parameters.Add(identityParam);
pipeLine.Commands.Add(myCommand);
Collection<PSObject> commandResults = pipeLine.Invoke();
PSObject distGroup = commandResults[0];
Runspace recipientRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
recipientRunSpace.Open();

Once we have the Dynamic Distribution Group's Name, we need its recipients.

C#
pipeLine = recipientRunSpace.CreatePipeline();
myCommand = new Command("Get-Recipient");
if (distGroup.Members["RecipientFilter"] != null && 
distGroup.Members["RecipientFilter"].Value.ToString().Length > 0)
   {
      CommandParameter recipientFilter = new CommandParameter
     ("RecipientPreviewFilter", distGroup.Members["RecipientFilter"].Value);
      myCommand.Parameters.Add(recipientFilter);
   }
CommandParameter OU = new CommandParameter("OrganizationalUnit", distGroup.Members
["RecipientContainer"]"Value.ToString());
myCommand.Parameters.Add(OU);
pipeLine.Commands.Add(myCommand);
commandResults = pipeLine.Invoke();

Next, I build a list of all the user's names from that group. The list of names is then bound to a bulleted List.

C#
List<string> nameList = new List<string>();
foreach (PSObject cmdlet in commandResults)
      {
         if (cmdlet.Properties["Name"] != null && 
         cmdlet.Properties["Name"].Value.ToString().Length > 0)
           {
             string recipientName = cmdlet.Properties["Name"].Value.ToString();
              nameList.Add(recipientName);
           }
      }
                
BulletedList1.DataSource = nameList;
BulletedList1.DataBind();

The last thing I did was change the DisplayMode of the bulleted list to LinkButton. This makes the user's names hyperlinks which go to the Sharepoint MySite page.

C#
protected void BulletedList1_Click(object sender, BulletedListEventArgs e)
{
    ListItem li = BulletedList1.Items[e.Index];
    Response.Redirect("http://sharepoint peoplesearch URL" 
    + BulletedList1.Items[e.Index].Text);
}

History

  • 8th December, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to authenticate to exchange server Pin
anilw27017-Aug-15 1:38
anilw27017-Aug-15 1:38 
GeneralMy vote of 5 Pin
Member 92757812-Apr-13 20:57
Member 92757812-Apr-13 20:57 
Generalthanks for sharing - have 5 Pin
Pranay Rana17-Jan-11 0:57
professionalPranay Rana17-Jan-11 0:57 
GeneralOk but... Pin
Not Active8-Dec-10 13:52
mentorNot Active8-Dec-10 13:52 
GeneralRe: Ok but... Pin
Matt Kloss8-Dec-10 15:19
Matt Kloss8-Dec-10 15:19 
Thanks for calling me out on this - it was a lazy solution to an error I was getting. I'll find a better way to do this an update the article.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.