Monday, December 24, 2007

Performing Operations on Infopath 2007 Contact Selector Control using Managed Code

Infopath 2007 provides an out of the box contact selector control to select the user and validate against the Active Directory.

In this blog, We will see, how to get more out of this control. Performing some advanced functions using managed code.

For basic usage of this control, see this blog entry on infopath blog:

To start with basics, this control has predefined schema, since it simultaneously stores the display name, account id and account type.

  user display name
  DOMAIN/user account
  user or group type
It is interesting to note that this control behaves like a repeating control, in a sense,
the user can select multiple users from the same control. Internally, the XML schema
shown above is repeated for multiple users.
2. Get the Display Names and Login Names for all users in contact Selector Control
To get the display names and login names, we just need to parse the generated XML
schema. We will store the names and login names as comma separated values.
Assuming that our control name is gpContactSelector, the code below extracts the
display names and login names.

XPathNavigator xNavMain = this.XmlFormView2.XmlForm.MainDataSource.CreateNavigator();

XmlNamespaceManager xNameSpace = new XmlNamespaceManager(new NameTable());

xNameSpace.AddNamespace("my", "");

XPathNodeIterator[] nodes = new XPathNodeIterator[4];

nodes[0] = xNavMain.Select("/my:myFields/my:gpContactSelector/my:Person/my:DisplayName", xNameSpace);

nodes[1] = xNavMain.Select("/my:myFields/my:gpContactSelector/my:Person/my:AccountId", xNameSpace);

nodes[2] = xNavMain.Select("/my:myFields/my:gpContactSelector/my:Person/my:AccountType", xNameSpace);

string names=string.Empty;

string accid=string.Empty;

for (int j = 0; j <>


for (int i = 0; i <>


if (nodes[2].Current.ToString() == "User")


names = names + nodes[0].Current.ToString()+";";

accid = accid + nodes[1].Current.ToString()+";";


The code above is pretty self explanatory. It parses the generated XML Schema
and stores the Names and login ids in two variables, names and accid as semicolon
separated values. Further operations can be then performed on these.
2. Sending Emails to All users selected in Contact Selector
To send emails, we obviously need email addresses of the contacts selected.
However, contact selector does not automatically grabs out the email addresses
of the contacts. To get the email addresses, we will first extract the login names
from the XML schema and then use the Microsoft.SharePoint.Utilities.SPUtility.GetFullNameandEmailfromLogin
class to get the email addresses.
The code below accepts the login names as semicolon separated values and builds
a string containing email addresses as semicolon separated values.

private string GetEmails(string final)


char[] a = { ';' };

string[] loginIds = final.Split(a, StringSplitOptions.RemoveEmptyEntries);

string[] emailids = new string[loginIds.Length];

for (int i = 0; i <>


Microsoft.SharePoint.Administration.SPGlobalAdmin ga = new Microsoft.SharePoint.Administration.SPGlobalAdmin();

string dispname, email;

Microsoft.SharePoint.Utilities.SPUtility.GetFullNameandEmailfromLogin(ga, loginIds[i], out dispname, out email);

emailids[i] = email;


string finalstring = string.Empty;

for (int i = 0; i <>

finalstring = finalstring + emailids[i] + ";";

return finalstring;


Now, we can use using System.Net.Mail namespace to send mails. This namespace
overrides the System.Web.Mail used in .NET 1.1. For those who are new to this
namespace, below is the sample code given to send mail.

private void SendMail()


MailMessage mail = new MailMessage();

mail.From = new MailAddress("", "Administrator");

char[] a = { ';' };

string[] emailIds = to.Split(a, StringSplitOptions.RemoveEmptyEntries);

for (int i = 0; i < style="">

mail.To.Add(new MailAddress(emailIds[i]));

mail.Subject = "New Meeting Request";

mail.Priority = MailPriority.Normal;

mail.IsBodyHtml = true;

mail.Body = GetBody();

new SmtpClient("smtpserver").Send(mail);



Anonymous said...

You write very well.

Alec Pojidaev said...

same without single code line:

Linda said...

I am not new to Infopath but I do not recognize this code. Where exactly does the "XPathNavigator xNavMain = ..." code reside?