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:

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 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", "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 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);

}

Sunday, December 16, 2007

Doing Data Validations in Infopath 2007 Programmatically

Infopath 2007 provides rich validation function for each of its controls. For ex, you can validate a text box or a field

For conditions such as equal to, is blank, is greater than and if that field fails the validation, we can display a message box.


One important thing to be noted is that, Infopath 2007 would not submit the form unless all the controls pass the validation.


Now, let’s talk about the complex validation, let’s say, I want to validate whether or not the textbox contains email address or not.

Surprisingly, this can be also done out of the box using pattern matching. If we look at the validation options in Infopath, one finds the options matches pattern, does not matches pattern. Here one can specify a complex regular expression to be matched.


Custom Validation


Now we come to our main topic, custom validation, means validation using some business logic which Infopath cannot provide out of the box.

To implement custom validation, each control in Infopath provides a validating event, which is usually called when the value of the control Changes. This is almost same as Changed event, with one difference that during the execution of validating event, the whole document tree is read only. That is, we cannot write to any field in Infopath while validating event is being executed.


The reason for this is that, the values of Infopath fields can change due to the change in values of other fields, which will result in Validating event to be called again and again, until the stack runs out of memory.


Now, coming to our main point, validating event, here we will see how to validate a simple field in Infopath, programmatically.

To write managed code in Infopath, you must have either VSTO or VSTA installed.


To write a validating event for a field, right click the field and choose Programming ->validating event


The code editor will open up the desired function for inserting the code for the validating event for particular field.

Below is an example of one of the typical implementation of validating event




public void OtherAttendees_Validate(object sender, XmlEventArgs e)


{


try


{


this.Errors.Delete("Invalid Value");


}

catch (Exception) { }


string str = e.Site.Value;

bool flag = CheckAttendees(str);



if (flag == false)

{


this.Errors.Add(e.Site, "Invalid Value", "The value of the "+ e.Site.LocalName + " field must be email addresses seperated by ;");


}


}



To understand the above code, we must first realize that this function will be called as soon as the value of the field will be changed.


We clean up the Errors collection prior to validating our field using this.Errors.Delete();

CheckAttendees function implements the business logic which determines that weather the field is valid or not. The input to the function is nothing but the value of the field.


Now, if the decide that field validation has failed, we add an error into the Error Collection using this.Errors.Add(), indicating the value and the custom error message we want to be displayed.


You can find out about the Errors collection here:

http://msdn2.microsoft.com/en-us/library/bb229714.aspx


Now the point is, if the Errors Collection contains an item, the Infopath will not allow to submit the form. And, if you try to submit it, it will highlight the particular field and display the custom error message.



Tip about Validation and Submit in Browser Forms


This sort of validation works fine in client form since everything is executed on client. However in browser based forms, the user would not come to know about the failure in validation until he clicks submit and this would cost an expensive server page postback.


To overcome this limitation, we can implement the same logic in Changed event handler and set a value of hidden field which would indicate the validity of the form at the time of submission. Now on submit button, we can use custom rules to check the value of the hidden field and prevent the submission at the client side only. This prevents considerable overhead of page postback.