InfoPath 2010 with custom code

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi all,
I am trying to make SharePoint 2010 out-of-the-box approval work-flow (with InfoPath embedded ). The basic requirements are as follows:
1) I need to call a Stored Procedure as custom code and backed by InfoPath 2010 form. This
SP will retrieve name of the manager who will approve a " Travel Request " as they are not in AD.
<br/>
2) I have installed VSTA and try to manipulate custom code for InfoPath 2010 for approval work-flow.
Firstly, I am using SharePoint OOTB Approval work flow. <br/>
Then, I need to work on "Approve/Reject" buttons for tasks. <br/>
<br/>
I have used SharePoint Designer 2010 to create work-flows and tried to consume both the InfoPath 2010 forms that SPD provides.
<br/>

Now, I am facing an issue when I am trying to manipulate the custom code.
Q1) On Form_Loading event, I am using people-picker control to per-fill the approver.Also, I have written a function that reads manager from the
Stored Procedure. I assign the manager name as the DisplayName. However, SetValue(GetApprover("/my:myFields/my:approver/pc:Person/pc:DisplayName")) simply returns "/my:myFields/my:approver/pc:Person/pc:DisplayName". Am I
missing anything here?
Q2) GetApprover method returns DisplayName of the manager and its working fine in my custom work flow. But, when shifted to VSTA, its throwing this error:<br/>
"The ConnectionString property has not been initialized."
Can somebody advice me on this?
Thank you. <br/>
<br/>
Cheers, <br/>
--aaroh


<br/>

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using Microsoft.Office.InfoPath;<br/>
<span style="color:Blue; using System;<br/>
<span style="color:Blue; using System.Xml;<br/>
<span style="color:Blue; using System.Xml.XPath;<br/>
<span style="color:Blue; using System.Data;<br/>
<span style="color:Blue; using System.Data.SqlClient;<br/>
<span style="color:Blue; using System.Configuration;<br/>
<br/>
<br/>
<span style="color:Blue; namespace CustomInitiationFrom<br/>
{<br/>
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class FormCode<br/>
{<br/>
<span style="color:Green; // Member variables are not supported in browser-enabled forms.<br/>
<span style="color:Green; // Instead, write and read these values from the FormState<br/>
<span style="color:Green; // dictionary using code such as the following:<br/>
<span style="color:Green; //<br/>
<span style="color:Green; // private object _memberVariable<br/>
<span style="color:Green; // {<br/>
<span style="color:Green; // get<br/>
<span style="color:Green; // {<br/>
<span style="color:Green; // return FormState["_memberVariable"];<br/>
<span style="color:Green; // }<br/>
<span style="color:Green; // set<br/>
<span style="color:Green; // {<br/>
<span style="color:Green; // FormState["_memberVariable"] = value;<br/>
<span style="color:Green; // }<br/>
<span style="color:Green; // }<br/>
<br/>
<span style="color:Green; // NOTE: The following procedure is required by Microsoft InfoPath.<br/>
<span style="color:Green; // It can be modified using Microsoft InfoPath.<br/>
<span style="color:Blue; public <span style="color:Blue; void InternalStartup()<br/>
{<br/>
((ButtonEvent)EventManager.ControlEvents[<span style="color:#A31515; "btnStart"]).Clicked += <span style="color:Blue; new ClickedEventHandler(btnStart_Clicked);<br/>
EventManager.FormEvents.Loading += <span style="color:Blue; new LoadingEventHandler(FormEvents_Loading);<br/>
}<br/>
<br/>
<span style="color:Blue; public <span style="color:Blue; void btnStart_Clicked(<span style="color:Blue; object sender, ClickedEventArgs e)<br/>
{<br/>
<span style="color:Green; // Write your code here.<br/>
<br/>
}<br/>
<br/>
<span style="color:Blue; public <span style="color:Blue; void FormEvents_Loading(<span style="color:Blue; object sender, LoadingEventArgs e)<br/>
{<br/>
<span style="color:Green; // Write your code here.<br/>
XPathNavigator root = MainDataSource.CreateNavigator();<br/>
root.SelectSingleNode(<span style="color:#A31515; "/my:myFields/my:approver/pc:Person/pc:DisplayName", NamespaceManager).SetValue(GetApprover(<span style="color:#A31515; "/my:myFields/my:approver/pc:Person/pc:DisplayName"));
<br/>
<br/>
}<br/>
<br/>
<span style="color:Blue; private <span style="color:Blue; string GetApprover(<span style="color:Blue; string user)<br/>
{<br/>
<span style="color:Blue; string connectionString = System.Configuration.ConfigurationManager.AppSettings[<span style="color:#A31515; "connectionString"];<br/>
<br/>
SqlConnection connection = <span style="color:Blue; null;<br/>
<span style="color:Blue; try<br/>
{<br/>
connection = <span style="color:Blue; new SqlConnection(connectionString);<br/>
connection.Open();<br/>
SqlCommand com = <span style="color:Blue; new SqlCommand();<br/>
com.Connection = connection;<br/>
com.CommandType = CommandType.StoredProcedure;<br/>
com.CommandText = <span style="color:#A31515; "spGetManager";<br/>
com.Parameters.Add(<span style="color:Blue; new SqlParameter(<span style="color:#A31515; "@employee_login", user));<br/>
<br/>
<span style="color:Blue; object obj = com.ExecuteScalar();<br/>
<span style="color:Blue; if (obj == <span style="color:Blue; null)<br/>
<span style="color:Blue; return <span style="color:#A31515; "";<br/>
<span style="color:Blue; else<br/>
<span style="color:Blue; return obj.ToString();<br/>
}<br/>
<span style="color:Blue; catch (Exception ex)<br/>
{<br/>
<span style="color:Blue; throw (ex);<br/>
}<br/>
<span style="color:Blue; finally<br/>
{<br/>
<span style="color:Blue; if (connection != <span style="color:Blue; null && connection.State == ConnectionState.Open)<br/>
connection.Close();<br/>
connection = <span style="color:Blue; null;<br/>
}<br/>
}<br/>
}<br/>
}<br/>

[/code]
<br/>



View the full article
 
Back
Top