YES, There is a way to do this. The things to note here are:
- Using UDCX connections, we have saved all the connection data, query data and credentials data on a single file in SharePoint list/library.
- Query should be correct and executing without any issues [Otherwise infopath cannot download the resulted schema]. For example, you want to get the user by user name then you may created SPROC with name "GetUserByUserName" and in your UDCX file you give the query tag as "<udc:Query>EXEC "dbo.GetUserByUserName" 'DEFAULT'</udc:Query>". We know there is no record in database with the name "DEFAULT". But, this is what we have to give as default query. [This is what we will change in the c# code dynamically.]
- Read this connection, query in the infopath c# code.
- Change the query information in which way you want in code.
- Execute the query.
- Reset the UDCX connection information back to original.
- It will automatically refresh the control data depends on the latest result set after we executed from the C# logic.
To execute the below code I am assuming there is a UDCX connection file available in a SharePoint library and your infopath form is allowing c# code.
//Get the connection details by connection name AdoQueryConnection adoConnection = (AdoQueryConnection)DataConnections["Get_User_Details"]; if (adoConnection != null) { string orgCommand = adoConnection.Command; //To read original command int index = orgCommand.IndexOf("DEFAULT"); //Find where the keyword "DEFAULT" in the command string string SPROC = string.Empty; if (index > -1) { try { SPROC = orgCommand.Substring(0, index); //Get only the SPROC name. adoConnection.Command = string.Format(SPROC + userName + "'"); //Append user name to the query. adoConnection.Execute(); //Execute the final query. This is what the command which contains actual parameter value instead of DEFAULT string. } catch { } finally { adoConnection.Command = orgCommand; //Should not forget to write this. We have to do this. } } }Things to note:
- The connection name "Get_User_Details" is the connection name from infopath form [Managed Data Connections option].
- As we are reading from existing UDCX connection file, we are not hard-coded any of the connection strings or queries.
- userName is the string variable which holds the user name which comes at run time. You have to write some logic to get these in your code.
- Read the query and replace the dummy parameter values with the original values.
- Execute the connection.
- In finally block, we are resetting the command back to original.
The checkbox in above figure "Update this value when the result of formula is recalculated" and applies to only if you use the formula.
We are done. The data now comes from database and passed the parameters to database dynamically, used UDCX connection file and did not hard-coded any of the connection, query information in code. Very clean right?
Hope you understood it well and liked it.
Confused: How do you read through the result set!? Every example I've seen calls the execute method and stops. I need to prefill the rows in a table while iterating through the result. How do I do that?
ReplyDelete