为 Form Library 开发工作流,如何读取 InfoPath 表单内容

本文所说的 InfoPath 表单并非包含在 Workflow 当中。而是作为一个 Form Library 的模板存在。当用户填写完成之后结果会存放在表单库中,而工作流如何获取这个结果呢?其实表单的结果就是一个 XML 文件。用 workflowProperties.Item.File.OpenBinary() 打开他,然后放到内存里面去。然后通过 XPath 访问就行了。

废话不多说,看代码就明白了(为了避便跑题,剔除了错误处理):

Code
        XPathDocument ipForm = null;

        
using (MemoryStream ms = new MemoryStream(workflowProperties.Item.File.OpenBinary()))
        {
            ipForm 
= new XPathDocument(ms);
            ms.Close();
        }

        XPathNavigator ipFormNav 
= ipForm.CreateNavigator();

        ipFormNav.MoveToFollowing(XPathNodeType.Element);
        XmlNamespaceManager nsManager 
= new XmlNamespaceManager(new NameTable());

        
foreach (KeyValuePair<stringstring> ns in ipFormNav.GetNamespacesInScope(XmlNamespaceScope.All))
        {
            
if (ns.Key == String.Empty)
            {
                nsManager.AddNamespace(
"def", ns.Value);
            }
            
else
            {
                nsManager.AddNamespace(ns.Key, ns.Value);
            }
        }

        
string field1 = ipFormNav.SelectSingleNode("/my:myFields/my:field1", nsManager).Value;
        DateTime field2 
= ipFormNav.SelectSingleNode("/my:myFields/my:field2", nsManager).ValueAsDateTime;

 

前面都是准备,最后两行是读取表单里面的值。

参考: How to use a Visual Studio workflow to submit data from an InfoPath form to a SharePoint list

posted on 2009-03-02 17:59  Fisher.W  阅读(939)  评论(0)    收藏  举报