繁体   English   中英

以编程方式编辑Infopath表单字段?

[英]Programmatically Edit Infopath Form Fields?

我的共享点站点中有一个表单库。 以编程方式,我需要填写一些字段。 我可以那样做吗? 如果有人知道,请提供一些示例代码。 首先,我需要检索信息路径文档,然后填充字段。

axel_c发布的内容非常接近。 这是一些经过清理和验证的工作代码...

public static void ChangeFields()
{
    //Open SharePoint site
    using (SPSite site = new SPSite("http://<SharePoint_Site_URL>"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            //Get handle for forms library
            SPList formsLib = web.Lists["FormsLib"];

            if (formsLib != null)
            {
                foreach (SPListItem item in formsLib.Items)
                {
                    XmlDocument xml = new XmlDocument();

                    //Open XML file and load it into XML document
                    using (Stream s = item.File.OpenBinaryStream())
                    {
                        xml.Load(s);
                    }

                    //Do your stuff with xml here. This is just an example of setting a boolean field to false.
                    XmlNodeList nodes = xml.GetElementsByTagName("my:SomeBooleanField");
                    foreach (XmlNode node in nodes)
                    {
                        node.InnerText = "0";
                    }

                    //Get binary data for new XML
                    byte[] xmlData = System.Text.Encoding.UTF8.GetBytes(xml.OuterXml);

                    using (MemoryStream ms = new MemoryStream(xmlData))
                    {
                        //Write data to SharePoint XML file
                        item.File.SaveBinary(ms);
                    }
                }
            }
        }
    }
}

感谢您提供示例代码@axel_c和@Jeff Burt

以下是杰夫·伯特(Jeff Burt)为我需要的文档集中的文件修改的相同代码。 如果您还没有“文档集”参考,则可以访问此网站,了解如何获取文档集: http : //howtosharepoint.blogspot.com/2010/12/programmatically-create-document-set.html

同样,这些代码将打开信息路径表单的.xml版本,而不是您可能会遇到的.xsn模板版本。

再次感谢大家...

    private void ChangeFields(DocumentSet docSet)
    {

        string extension = "";
        SPFolder documentsetFolder = docSet.Folder;


        foreach (SPFile file in documentsetFolder.Files)
        {

            extension = Path.GetExtension(file.Name);

            if (extension != ".xml") //check if it's a valid xml file
                return;

            XmlDocument xml = new XmlDocument();


            //Open XML file and load it into XML document, needs to be .xml file not .xsn
            using (Stream s = file.OpenBinaryStream())
            {
                xml.Load(s);
            }

            //Do your stuff with xml here. This is just an example of setting a boolean field to false.
            XmlNodeList nodes = xml.GetElementsByTagName("my:fieldtagname");
            foreach (XmlNode node in nodes)
            {
                node.InnerText = "xyz";
            }

            //Get binary data for new XML
            byte[] xmlData = System.Text.Encoding.UTF8.GetBytes(xml.OuterXml);

            using (MemoryStream ms = new MemoryStream(xmlData))
            {
                //Write data to SharePoint XML file
                file.SaveBinary(ms);
            }
        }



    }

Infopath文档只是一个常规XML文件,其结构与您在Infopath表单中定义的数据源相匹配。

您只需要通过SharePoint对象模型访问文件,使用标准方法( XmlDocument API )对其进行修改,然后将其写回到SharePoint列表中。 您必须小心保留结构并插入有效数据,否则将无法使用Infopath打开表单。

如果您打算进行任何认真的开发,则应该真正查看有关SharePoint的书。 Infopath也是一个雷区。

对象模型的用法示例: 在此处此处此处 可笑的是,不完整的MSDN参考文档在这里

编辑 :这是一些示例代码。 我已经有一段时间没有做过SharePoint了,所以我不确定这是100%正确的,但是它应该给您足够的入门知识:

// Open SharePoint site
using (SPSite site = new SPSite("http://<SharePoint_Site_URL>"))
{
  using (SPWeb web = site.OpenWeb())
  {
    // Get handle for forms library
    SPList formsLib = web.Lists["FormsLib"];
    if (formsLib != null)
    {
      SPListItem itm = formsLib.Items["myform.xml"];

      // Open xml and load it into XML document
      using (Stream s = itm.File.OpenBinary ())
      {
          MemoryStream ms;
          byte[] xmlData;


          XmlDocument xml = new XmlDocument ();
          xml.Load (s);
          s.Close ();  

        // Do your stuff with xml here ...

        // Get binary data for new XML
        xmlData = System.Text.Encoding.UTF8.GetBytes (xml.DocumentElement.OuterXml);

        ms = new MemoryStream (xmlData); 

        // Write data to sharepoint item
        itm.File.SaveBinary (ms);

        ms.Close ();

        itm.Update ();  
      }


    }
    web.Close();
  }
  site.Close();
}

这取决于您可用的工具集,技能和确切要求。

在InfoPath表单中预填充数据有两种主要方法。

  1. 导出相关字段,作为表单发布过程的一部分。 然后,这些字段将成为“文档/表单”库中的列,您可以在其中通过工作流手动或自定义代码所在的位置对其进行操作。

  2. 使用类似于Axel_c先前提供的代码直接操作表单。 这里最大的问题是:什么将触发此代码? 文档库中的事件接收器,SharePoint Designer工作流,Visual Studio工作流等?

如果您尝试从SharePoint Designer工作流中执行此操作,请查看SharePointWorkflow Power Pack 它允许将C#和VB代码直接嵌入到工作流中,而无需进行复杂的Visual Studio开发。 在此处可以找到有关如何从工作流中查询InfoPath数据的示例。 如果您具有一些开发技能,则应该可以对其进行修改以适合您的需求。

我还推荐网站www.infopathdev.com ,它们拥有出色而活跃的论坛。 您几乎肯定会在那里找到问题的答案。

我遇到了这个问题,并在Jeff Burt / Axel_c的帖子的帮助下解决了这个问题。

我试图使用XMLDocument.Save([stream])和SPItem.File.SaveBinary([stream])方法将更新的InfoPath XML文件写回到SharePoint库。 看起来XMLDocument.Save([stream])会使用错误的编码将文件写回到SharePoint,无论XML声明中说的是什么。

尝试打开更新的InfoPath表单时,我不断收到错误消息“表单中的计算尚未完成...”

我已经编写了这两个函数来获取和更新以及InfoPath表单。 只需以通常的方式处理从ReadSPFiletoXMLdocument()返回的XML,然后使用WriteXMLtoSPFile()将其发送回您的服务器即可。

private System.Xml.XmlDocument ReadSPFiletoXMLdocument(SPListItem item)
{
    //get SharePoint file XML
    System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
    try
    {
        using (System.IO.Stream xmlStream = item.File.OpenBinaryStream())
        {
            xDoc.Load(xmlStream);
        }
    }
    catch (Exception ex)
    {
        //put your own error handling here
    }
    return xDoc;
}

private void WriteXMLtoSPFile(SPListItem item, XmlDocument xDoc)
{
    byte[] xmlData = System.Text.Encoding.UTF8.GetBytes(xDoc.OuterXml);
    try
    {
        using (System.IO.MemoryStream outStream = new System.IO.MemoryStream(xmlData))
        {
            item.File.SaveBinary(outStream);
        }
    }
    catch (Exception ex)
    {
       //put your own error handling here
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM