Anatomy of UDC

on Thursday, October 22, 2009

The anatomy of a UDC file

OK, we’ve talked about super-fantastic high end authentication scenarios. We’ve talked about cross-domain security and administrative control. We’ve talked about generating UDC files using InfoPath and consuming them again in the designer. Now let’s drill into the structure of the file itself.


UDC V2 is an XML format, and like any good XML format, there is a schema and a namespace associated with it. I’ll give you the full schema at the end of this post.

A handy tip: copy the schema into notepad and save it with an xsd extension, then follow the steps outlined in Aaron Stebner’s blog here: http://blogs.msdn.com/astebner/archive/2005/12/07/501466.aspx to add the xsd file to Visual Studio’s intellisense cache. Once that’s in place, Visual Studio will help you generate your UDC files!


Basic Structure of the File

Every UDC file you create will have the structure below, so copy it to notepad and use it as the basis for all your files. This is the infrastructure – the metadata that describes the connection and allows external components such as SharePoint and InfoPath to understand what’s inside.

<?MicrosoftWindowsSharePointServices ContentTypeID=”0x010100B4CBD48E029A4ad8B62CB0E41868F2B0”?>
<udc:DataSource MajorVersion="2" MinorVersion="0" xmlns:udc="http://schemas.microsoft.com/office/infopath/2006/udc">
  <udc:Name/>
  <udc:Description/>
  <udc:Type MajorVersion="2" MinorVersion="0" Type=""/>
  <udc:ConnectionInfo Purpose="" AltDataSource=””/>
</udc:DataSource>


The file begins with a processing instruction specifying a Content Type Id. This is necessary to associate the file with the UDC content type on a Microsoft Office SharePoint 2007 server. Having the content type identified allows the Name, Title, Description, Type, and Purpose fields to be promoted into columns in the data connection library so that the InfoPath designer can see the file.

By the way: there’s a known issue on Windows Vista where the title property doesn’t get promoted when using the Convert function in the InfoPath designer. To work around this issue, save the file to your local disk, and then re-upload the file to the library using the SharePoint library upload function.

The root node is called DataSource, and it specifies the UDC version of 2.0, as well as the udc V2 namespace. The Name and Description fields are promoted into SharePoint columns, and they show up in the InfoPath designer when browsing to the file in the data connection wizard. So, while these fields are not strictly required, you should use them to provide useful information about what the data connection is about, and maybe even contact information for the owner of the data source.

Type (specifically, the Type attribute on the Type element) and Purpose are both required, and very easy to fill once you know the possible values:

DataSource/Type/@Type:


Value



Use for


SharePointList


SharePoint list query connection


SharePointLibrary


SharePoint Library submit connection


Database


Database query connection


XmlQuery


Xml file query connection


XmlSubmit


HTTP Post submit connection


WebService


Web service submit or query connection


DataSource/ConnectionInfo/@Purpose:



Value


Use for


ReadOnly


All query connections


WriteOnly



All submit connections


ReadWrite


Web service only, when both query and submit methods are specified and they reference the same WSDL

The AltDataSource attribute specifies a second UDC file in the same library. When specified, InfoPath will use the original file, and Forms Services will use the file specified in the attribute. The attribute's value should be the filename of the alternate UDC file. Naturally, the two files should specify equivalent connections - specifically, the connections have to have the same Type and Purpose, and they have to return the same data, or data in the same format.


The ConnectionInfo Element

The meat of the connection information is here, and each type of data connection requires specific elements. So, the contents of this element will change depending on the Type and Purpose attributes.

Let’s run ‘em down, shall we? I’m confident that you can learn by example, so I’m not going to do a lot of explaining here. As I noted in a previous post, we’re working on a schema reference that will fill in the details.


1. Web Service
This example is for a query connection. For a submit connection, the Purpose would be WriteOnly, and the ServiceUrl and SoapAction would be contained within an UpdateCommand element rather than SelectCommand.

Web service is the only connection type that can have both a SelectCommand and an UpdateCommand in the same file. The two operations need to share the same WSDL, and the Purpose in that case is ReadWrite.

<udc:ConnectionInfo Purpose=”ReadOnly”>
   <udc:WsdlUrl>
   http://www.someserver.com/service/service1.asmx?wsdl
 </udc:WsdlUrl>
 <udc:SelectCommand>
  <udc:ServiceUrl>
   http://www.someservice.com/service/service1.asmx
  </udc:ServiceUrl>
  <udc:SoapAction>
   http://www.someservice.com/service/SomeOperation
  </udc:SoapAction>
 </udc:SelectCommand>
</udc:ConnectionInfo>

2. Database
A database connection is always marked as ReadOnly. InfoPath determines at design time whether submit can be enabled for the connection.


<udc:ConnectionInfo Purpose=”ReadOnly”>
 <udc:SelectCommand>
  <udc:ConnectionString>
Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:\temp\Database1.accdb;Mode=Share Deny None;Jet OLEDB:System database=&quot;&quot;;Jet OLEDB:Registry Path=&quot;&quot;;Jet OLEDB:Database Password=&quot;&quot;;Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password=&quot;&quot;;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;
  </udc:ConnectionString>
  <udc:Query>SELECT * FROM Table1</udc:Query>
 </udc:SelectCommand>
</udc:ConnectionInfo>


3. SharePoint list query
<udc:ConnectionInfo Purpose=”ReadOnly”>
 <udc:SelectCommand>
  <udc:ListId>{8fe80d4c-2203-4fa3-a411-f57f2e8be459}</udc:ListId>
  <udc:WebUrl>http://someserver/sites/somesite</udc:WebUrl>
 </udc:SelectCommand>
</udc:ConnectionInfo>

4. SharePoint library submit
The FolderName element specifies the default folder name that is used to prepopulate the data connection wizard.  Form designers will still need to specify the file name, as it is stored in the form template.


<udc:ConnectionInfo Purpose=”WriteOnly”>
 <udc:UpdateCommand>
  <udc:FileName>ExpenseReport.xml<udc:FileName>
  <udc:FolderName AllowOverwrite=”true”>
   http://someserver/sites/somesite/somelibrary
  </udc:FolderName>
 </udc:UpdateCommand>
</udc:ConnectionInfo>

5. XML file query
<udc:ConnectionInfo Purpose=”ReadOnly”>
 <udc:SelectCommand>
  <udc:Query>http://someserver/somefile.aspx</udc:Query>
 </udc:SelectCommand>
</udc:ConnectionInfo>


6. HTTP Post
<udc:ConnectionInfo Purpose=”WriteOnly”>
 <udc:UpdateCommand>
  <udc:Submit>http://someserver/somefile.aspx</udc:Submit>
 </udc:UpdateCommand>
</udc:ConnectionInfo>


Authentication

I’ve described the contents of the Authentication element in previous blog posts, so I won’t go into a detailed explanation. Two things are important here:
1. The Authentication element must be the last child of ConnectionInfo
2. If both SSO and UseExplicit are specified, Forms Services will use the credentials specified in the SSO element and will ignore the UseExplicit element.
This section is used only for forms running in the browser – InfoPath always ignores the authentication element.


<udc:Authentication>
 <udc:UseExplicit CredentialType="">
  <udc:UserId/>
  <udc:Password/>
 </udc:UseExplicit>
 <udc:SSO AppId="" CredentialType=""/>
</udc:Authentication>


The Universal Data Connection 2.0 schema

Download the XSD from here.

How to create and Manage UDC files

Today I am going to cover about How to Create and Manage UDC(Universal Data Connection) files which are very important in real world infopath applications using external connections. I have looked lot of places but no single article gives you all the details that are required about How to create and manage UDC files.
Note: We will use DCL for Data Connection Library.

To create UDC file you need to follow the steps.

1. Create a Data Connection Library in your sharepoint site.Let's say the url of DCL is http://servername:123/DLClib1
2. Create a Data Connection the normal way in infopath first.
3. Then Click on convert button 



4. It will ask you to give the URL of your DCL. Type the URL of your DCL and name of udcx file like http://servername:123/DLClib1/birthdays.udcx
5.You are still not done. If you check your DCL now it will have one record which will be checked out and you won't be able to check it in and then approve it. So you need to download this file from DCL on your machine and then upload it again into DCL and then approve it.
6. Once you are done with this, If you want to manage the udcx file from central admin then you need to upload the same udcx file in central administration and then in dataconnection you have to chagne to pick up the udcx settings from central admin.


References:
Ref1

Ref2

Ref3

Ref4

Populate DropDownList using code - InfoPath

on Wednesday, October 21, 2009

Problem

You have a drop-down list box in InfoPath, which you would like to dynamically populate (=fill) through code with items from an
XML document.

Solution

Bind the drop-down list box to a repeating node in either the Main or a secondary data source
of the InfoPath form
, and then loop through the nodes in the XML document to populate the data source that is bound to
the drop-down list box with the data from the XML document.



Discussion

A drop-down list box in InfoPath consists of:

  1. A field in which to store the value of the item that is selected from the drop-down list box. This is the field that you drag-and-drop onto the InfoPath form and
    which represents the drop-down list box.
  2. A repeating node in the Main or a secondary data source, which the drop-down list box is bound to, and
    which contains the items to display in the drop-down list box.
Thus populating a drop-down list box in InfoPath means populating the data source the drop-down list box has been bound to
instead of setting the value of the drop-down list box. The latter will set the selected value on the drop-down list box
instead of filling the drop-down list box with items.
This is an important concept to understand when using list boxes in InfoPath and wanting to fill them programmatically
with items.
You can accomplish this functionality as follows:
  1. Open Microsoft Office InfoPath 2007, create a new blank form, and add a Drop-Down List Box
    to the form (also see: Insert a drop-down list box). Your form should resemble the following figure:


    The InfoPath form template in Design mode

    Figure 1. The InfoPath form template in Design mode.

  2. Create an XML file with the following content:


    <?xml version="1.0" encoding="UTF-8" ?>
    <lists>
    <list><title/></list>
    <list>
    <title/></list>
    </lists>



    This XML file will serve as a secondary data source that will contain the items for the drop-down list box, so create
    a Receive data connection to an XML document for this XML file, and bind
    the Drop-Down List Box to the data connection you created
    (also see: Working with the data source and data connections).
    Note: The XML file contains two list nodes instead of one. This is necessary for
    InfoPath to recognize the list node as a repeating node. One of these
    nodes will be deleted later through code before populating the drop-down list box. The remaining node will
    be copied repeatedly to create the items for the drop-down list box.


    Binding the drop-down list box to a secondary data source

    Figure 2. Binding the drop-down list box to a secondary data source.

  3. Create an XML document and add a Receive data connection for this XML document
    to the InfoPath form template. You will use this secondary data source to populate the secondary data source that
    is bound to the drop-down list box, so that the content of this XML document will show up as items in the drop-down
    list box. Note: You are not required to use an XML document to populate the drop-down list box, but could use any
    other type of data source or file that contains rows of data, which can be loaded into InfoPath, looped through,
    and used to populate
    the secondary data source that is bound to the drop-down list box.

  4. Add the following code to the Loading event handler of the form:


    C#
    RemoveFirstItem();
    XPathNodeIterator players = DataSources["doclibs"].CreateNavigator().Select(
    "//player", NamespaceManager);
    foreach (XPathNavigator player in players)
    {
    string title = player.SelectSingleNode("title", NamespaceManager).Value;

    AddItem(title);
    }






  5. Add the following code for the RemoveFirstItem and AddItem methods to the form's code.
    The RemoveFirstItem method removes one of the list nodes from the secondary data source that is bound to
    the drop-down list box (see note in step 2). The AddItem method adds an item to the secondary data source
    that is bound to the drop-down list box, thereby filling the title.
    for each item that will appear in the drop-down list box.


    C#

    private void AddItem(string title)
            {
                XPathNavigator nav = this.DataSources["doclibs"].CreateNavigator();
                XPathNavigator group1 = nav.SelectSingleNode("/lists", NamespaceManager);
                XPathNavigator node = group1.SelectSingleNode("list", this.NamespaceManager);
                XPathNavigator newNode = node.Clone();
                newNode.SelectSingleNode("title").SetValue(title);
                group1.AppendChild(newNode);

            }
            private void RemoveFirstItem()
            {
                XPathNavigator firstNode = this.DataSources["doclibs"].CreateNavigator().SelectSingleNode("/lists/list", this.NamespaceManager);
                firstNode.DeleteSelf();
            }




  6. Save your work, build the project, and test the form.

You should now have a fully functional InfoPath form so that once the form loads, the items from the XML document
will appear as items in the drop-down list box.

Debug InfoPath Code at runtime

on Wednesday, October 14, 2009


In VSTA open project properties and then in Build 





click on Advanced Button in Output section.In the Advanced window select full in debug info







After that publish your form.

Now to debug the code you have to open visual studio and Click on Tools -->Options and uncheck Enable Just My code option.








Make sure you select to open the form as a web page and not in client application in form library.

Then open the .cs file for your form's code and then attach all the w3wp.exe processes.

Now whenever you try to create a new item in your form library it will be able to get into this visual studio for you to debug the code.