Infopath and Yukon: the details
In a recent post, I touched upon the reasons why you might want to go with SQL Server XML columns as the storage for your infopath forms. in this article, we'll talk about actually making it work.
Infopath's strength is working with XML data sources: Web services. In the Implementation below, we'll write a simple web service that will help us retrieve and submit the data to Yukon.
Scenario
Build a resume database for the HR department. We want to storeHighly structuredResumes in a database. highly structured here is the opposite of freeform: a resume in our scenario isn't a blob of text; we have the graduation date, employee dates, actual titles and other things parsed into data structures.
Data Structure
We will store candidate records as XML blobs in a Yukon XML column. each resume will be stored as a separate record; each record will also have a primary key-A jobcandidateid-to simplify our development. we'll define only one table, called jobcandidate:
Populate the table with a few resume XML files that all satisfy the same schema. Store the schema somewhere where the Web server can access it.
Core Implementation in English: We'll write a web service as a middle tier between SQL Server 2005 and the infopath form. This web service will have two methods:
1) getcandidaterecord: Given A jobcandidateid (an integer), return a dataset (for our purposes, an XSD-typed xml blob) that contains the candidate record. give me the job candidate ID, I'll give you his resume.
2) updatecandidaterecord: Take a jobcandidateid (an integer) and a resume (an XML document ), and update the resume of the candidate with that participant ID to the resume passed in as a second parameter. nothing fancy, really.
Core Implementation in C #
I promised you two methods, here they are. First, getcandidaterecord.
[Webmethod]
Public DatasetGetcandidaterecord (IntJobcandidateid)
{
DatasetResult =Null;
Using (Sqlconnection Conn = New Sqlconnection (Connstring ))
{
Conn. open ();
Sqlcommand Command = conn. createcommand ();
Command. commandtext = @"
Select resume
From" + Tablename +@"
Where jobcandidateid = @ X" ;
Command. Parameters. Add ( "@ X" , Sqldbtype . INT );
Command. Parameters [0]. value = jobcandidateid;
Sqldatareader Reader = command. executereader ();
If (Reader. Read ())
{
Dataset DS = New Dataset ();
DS. readxmlschema ( @ "C: \ Inetpub \ wwwroot \ infopath_yukon \ resume. XSD" );
Xmldatadocument XD = New Xmldatadocument (DS );
XD. Load ( New Stringreader (( String ) Reader. getvalue (0 )));
Result = XD. dataset;
}
Conn. Close ();
Return Result;
}
}
Things are fairly straightforward here:
-Open a sqlconnection using ASP. NET credentials (make sure the ASPNET user has read/write rights to the database ).
-Build a simple SELECT statement to return a resume. Recall that the resume is just an XML document stored as-is in the database.
-Cast the resume dataset into a typed dataset by applying a schema stored somewhere on the Web server. Oh, I forgot to tell you-you need a schema :-). Why? Infopath form needs to know what to expect CT from the Web service, and while infopath can infer the shape of the data from the instance, this method is very much error prone. for example, how can infopath know of a repeating structure if only one instance was present in a sample XML document? How about choice or optional structures? Because of all of these reasons, you need to provide a typed dataset through your web service.
-Return the typed dataset for the resume record.
Next, let's look at updatecandidaterecord.
[Webmethod]
Public VoidUpdatecandidaterecord (XmldocumentXML,IntJobcandidateid)
{
Using(SqlconnectionConn =New Sqlconnection(Connstring ))
{
Conn. open ();
SqlcommandCommand = conn. createcommand ();
Command. commandtext =@"
Update" + Tablename + @"
Set resume = @ x
Where jobcandidateid = @ Y" ;
Command. Parameters. Add ( "@ X" , Sqldbtype . XML );
Command. Parameters [0]. value = xml. innerxml. tostring ();
Command. Parameters. Add ( "@ Y" ,Sqldbtype . INT );
Command. Parameters [1]. value = jobcandidateid;
Command. executenonquery ();
Conn. Close ();
}
}
-Open a sqlconnection
-Build a simple update statement to save the resume for a given candidate. Note that you must use sqlcommand parameters: Just concatenating the XML blob won't do.
-Execute the update statement. Note that we are replacing the entire resume with the new one; no partial updates are done. This means that simultaneous editing of resume records won't be possible.
Basic form template
Now that the Web service is set up, we can easily build a form template based on it. the template may or may not be browser-enabled; the method described here works with both. just launch infopath designer, and pick "start from Web Service" as your data source. specify getcandidaterecord as the "receive" piece of the web service, and updatecandidaterecord as the submit part.
Infopath will ask you for sample jobcandidateid values for the receive Web Service; since our database already has a few resumes, We can type in the jobcandidateid for one of them. you may be wondering-wait, I thought infopath won't do the schema inference by example! It won't-The dataset returned by your web service will contain a schema (that's why we called dataset. readxmlschema () in getcandidaterecord), and infopath will use that schema to build your data source tree.
After you 've gone through the initial data connection setup, you'll notice that your main data source tree is correctly populated with the data types from your schema. complex Structures shocould show up just fine-repeating, optional, choice structures, non-string datatypes, XSD validation... and the submit button shocould be configured to save the modified resumes back to SQL Server.
FAQ
1.Why do we have to use a custom web service, and notBuilt-in Yukon Web Services?
There are unfortunate technical limitations that currently require you to write a custom web service to work with SQL Server 2005 in a manner described above. the Web service is, as you saw, very easy; we know that this is something that can be made better, and will consider addressing this in future versions of infopath and SQL Server.
2.Why not XSD-typed XML columns?
When infopath submits datasets to the Web service, it adds dataset tracking information; while you can add optional attributes to your infopath-generated schema and upload it to Yukon, this wocould complicate maintenance quite a bit.
3.What other resources are available on the topic?
Be sure to check out this article by s.y. M. Wong-a-ton.
Alex Weinstein
Program Manager
Filed under: writing code, data connections Turn: http://blogs.msdn.com/infopath/archive/2007/02/05/infopath-and-yukon-the-details.aspx