Wednesday, May 5, 2010

Infopath "Request for the permission of type 'Object' failed" exception in VSTA

If you are creating a InfoPath form with managed code(c# or VB), the form should have full trust (security permission).
If the trust level is not full it gives an exception "Request for permission of type 'object' failed" for the object.
Below code in VSTA with infopath form having default settings gives an exception "Request for the permission of type 'System.Net.Mail.SmtpPermission' failed".
MailMessage MailMsg = new MailMessage();
MailAddress fromAddress = new MailAddress("", "FromName");
MailMsg.From = fromAddress;
MailAddress toAddress = new MailAddress(toEmail, toName);
MailMsg.IsBodyHtml = true;
MailMsg.Subject = subject;
MailMsg.Body = body;
SmtpClient smtpClient = new SmtpClient("");

What it really needs to overcome the above issue is to give full trust level (security permission).
The full trust level can be given for infopath form changing the form settings as follows.
1) Open infopath form  2)Go to Tools menu  3)Open Forms Options  4)Go to Security and Trust tab 5)Deselect automatically determine security level check box  6) Select Full Trust radio button  7)Hit OK button

The setting itself solves the problem.

No comments:

Post a Comment