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:
https://blogs.msdn.com/infopath/archive/2007/02/28/using-the-contact-selector-control.aspx
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", "https://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-20T20:01:12");
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 <>
nodes[i].MoveNext();
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("Admin@domain.com", "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);
}
Comments
Anonymous
April 17, 2008
Hi , I am not able to get all the names selected seperated by ';' in the contact selector control. I am only getting the first name .present but the other name are not getting Thanks, DevAnonymous
April 17, 2008
Can you show the code which you have used ?Anonymous
April 17, 2008
Hi , I used the same code as given here in the blog.
- Get the Display Names and Login Names for all users in contact Selector Control It seems like this code is incomplete .for (int i = 0; i <> ?? Could you please send me the exact code to be used to get all the selected names in the contact selector control.
Anonymous
April 17, 2008
Sorry for tat, this is the code: XPathNavigator xNavMain = this.XmlFormView2.XmlForm.MainDataSource.CreateNavigator(); XmlNamespaceManager xNameSpace = new XmlNamespaceManager(new NameTable()); xNameSpace.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-20T20:01:12"); 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 <> nodes[i].MoveNext(); if (nodes[2].Current.ToString() == "User") { names = names + nodes[0].Current.ToString()+";"; accid = accid + nodes[1].Current.ToString()+";"; } } Or refer here: http://madhurahuja.blogspot.com/2007/12/performing-operations-on-infopath-2007.htmlAnonymous
April 20, 2008
In the above code, for loop is incomplete. for (int j = 0; j <> ? for (int i = 0; i <> ? Could you please send the exact code?Anonymous
April 20, 2008
Sorry about that, this is due to the blog migration issues. Till I fix this, you can use the following url: http://madhurahuja.blogspot.com/2007/12/performing-operations-on-infopath-2007.htmlAnonymous
April 20, 2008
Hi, Sorry to trouble you. I checked the other link [http://madhurahuja.blogspot.com/2007/12/performing-operations-on-infopath-2007.html]. In that one also, the code(for loop) is incomplete. Thanks, DevAnonymous
April 20, 2008
Sorry again. There seems to be mess up. Anyways, will correct that. The loop should be for (int i = 0; i <emailids.Length;++i) { ....Anonymous
April 23, 2008
Hi, I have a requirement like, in a QuickMail button click, I have to pass the email id's of the selected person's in the contact selector control to the 'To' part of an outlook message.So, first I am trying to get the names separated by ';'. This is the code that I am using. XPathNavigator xNavMain = this.MainDataSource.CreateNavigator(); XmlNamespaceManager xNameSpace = new XmlNamespaceManager(new NameTable()); xNameSpace.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-20T20:01:12"); XPathNodeIterator[] nodes = new XPathNodeIterator[4]; nodes[0] = xNavMain.Select("/my:myFields/my:gpContactSelectorNotifyOthers/my:Person/my:DisplayName", xNameSpace); nodes[1] = xNavMain.Select("/my:myFields/my:gpContactSelectorNotifyOthers/my:Person/my:AccountId", xNameSpace); nodes[2] = xNavMain.Select("/my:myFields/my:gpContactSelectorNotifyOthers/my:Person/my:AccountType", xNameSpace); string names=string.Empty; string accid=string.Empty; for (int i = 0; i <= nodes.Length; ++i) { nodes[i].MoveNext(); if (nodes[2].Current.ToString() == "User") { names = names + nodes[0].Current.ToString() + ";"; accid = accid + nodes[1].Current.ToString() + ";"; } } But it is not getting inside the if loop. I debugged the code to check the values. I could find that in nodes[0].Current.ToString(),nodes[1].Current.ToString and nodes[2].Current.ToString I am not getting the dispalyname, id and 'User'.In my Infopath form there are a lot of controls.The value of the nodes is coming as 'nt[Some textboxvalue]....nt[some dropdown value].....nt[Displayname]nt[id].........'. Am I doing anything wrong here. Please correct me if I am wrong. Thanks for your time. Thanks, Dev.Anonymous
July 29, 2008
alot of ad servers use the username@domain.local as the email address. Using code is the LONGGGGG way to do it. you should be able to just resolve an email address as a function using this: concat(AccountId,"@domain.local") replacing the domain with your company's domain, ex: concat(AccountId,"@corp.local")Anonymous
November 24, 2008
The for loop is incomplete in section 1 also. Could you please update or let me know what should be at string names=string.Empty; string accid=string.Empty; for (int j = 0; j <>????????????????? { for (int i = 0; i <>?????????????????????? nodes[i].MoveNext(); if (nodes[2].Current.ToString() == "User") { names = names + nodes[0].Current.ToString()+";"; accid = accid + nodes[1].Current.ToString()+";"; } }Anonymous
December 18, 2008
Get and send emails or expoising control data are too simple tasks to use code. Save your skills for somthing more chalenging. http://alecpojidaev.wordpress.com/2008/12/16/infopath-codeless-programming-walkthrough/Anonymous
February 12, 2009
Where the stsadm command addtemplate stores the site template Regards IntekhabAnonymous
June 04, 2009
Hi Elfoamerican, i did followed http://alecpojidaev.wordpress.com/2008/12/16/infopath-codeless-programming-walkthrough/ but of no use. When i enter the user in contact slector it doesnot returnthe email. Any idea???? Thanks.