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:
http://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 schemashown 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 XMLschema. We will store the names and login names as comma separated values.
Assuming that our control name is gpContactSelector, the code below extracts thedisplay names and login names.
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()+";";
}
The code above is pretty self explanatory. It parses the generated XML Schemaand stores the Names and login ids in two variables, names and accid as semicolonseparated 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 addressesof the contacts. To get the email addresses, we will first extract the login namesfrom the XML schema and then use the Microsoft.SharePoint.Utilities.SPUtility.GetFullNameandEmailfromLoginclass to get the email addresses.
The code below accepts the login names as semicolon separated values and buildsa 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];
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;
finalstring = finalstring + emailids[i] + ";";
return finalstring;
Now, we can use using System.Net.Mail namespace to send mails. This namespaceoverrides the System.Web.Mail used in .NET 1.1. For those who are new to thisnamespace, below is the sample code given to send mail.
private void SendMail()
MailMessage mail = new MailMessage();
mail.From = new MailAddress("Admin@domain.com", "Administrator");
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);
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,
Dev
Can you show the code which you have used ?
I used the same code as given here in the blog.
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.
Sorry for tat, this is the code:
Or refer here:
http://madhurahuja.blogspot.com/2007/12/performing-operations-on-infopath-2007.html
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?
Sorry about that, this is due to the blog migration issues. Till I fix this, you can use the following url:
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.
Sorry again. There seems to be mess up. Anyways, will correct that. The loop should be
for (int i = 0; i <emailids.Length;++i)
....
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();
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);
for (int i = 0; i <= nodes.Length; ++i)
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 '\n\t[Some textboxvalue]....\n\t[some dropdown value].....\n\t[Displayname]\n\t[id].........'.
Am I doing anything wrong here.
Please correct me if I am wrong.
Thanks for your time.
Dev.
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")
The for loop is incomplete in section 1 also. Could you please update or let me know what should be at
for (int j = 0; j <>?????????????????
for (int i = 0; i <>??????????????????????
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/
Where the stsadm command addtemplate stores the site template
Regards
Intekhab
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.