【问题标题】:Set programmatically created XML document to open with InfoPath template in SharePoint设置以编程方式创建的 XML 文档以在 SharePoint 中使用 InfoPath 模板打开
【发布时间】:2012-09-05 22:02:55
【问题描述】:

我在获取 XML 文档时遇到问题,我以编程方式创建并添加到 SharePoint 中的表单库以使用 InfoPath 模板打开,而不是仅在 Internet Explorer 中作为纯 XML 文档打开。

基本上,我有一个 Web 服务,它检查数据库以查看某些条件是否为真。如果是,Web 服务以编程方式创建一个 XML 文档,该文档与前面提到的 InfoPath 模板创建的文件具有相同的 XML 架构。通常的过程是真实用户打开这个 InfoPath 模板,填写数据并单击 SUBMIT 按钮,这会在后台创建 XML 文档并将其保存在表单库中。当用户稍后进入表单库查看该 XML 文档时,该文档会自动使用 InfoPath 模板打开(而不是仅在 Internet Explorer 中作为 XML 文档打开)。

我试图找出为什么我从 Web 服务生成的 XML 文档在这方面的行为与从 InfoPath 生成的 XML 文档不同。这是我用来在 Web 服务中创建 XML 文档的代码的 sn-p:

        //specify path to the SharePoint site and the form library
        var clientContext = new ClientContext("http://urlToSiteContainingTheFormLibrary");
        var site = clientContext.Web;
        clientContext.Load(site);
        var folder = site.GetFolderByServerRelativeUrl("FormLibraryNameHere");
        clientContext.Load(folder);            
        clientContext.ExecuteQuery();

        //create new file to add to the form library
        var fci = new FileCreationInformation();
        var encoding = new System.Text.UTF8Encoding();

        //load the InfoPath document's XML schema from resources file
        var baseXml = XElement.Load(new StringReader(Properties.Resources.BaseXml));
        //fill out the appropriate elements and attributes of the XML here
        //......
        //

        //set remaining properties of the new FileCreationInformation object
        byte[] array = encoding.GetBytes(baseXml.ToString());

        fci.Content = array;
        fci.Overwrite = true;
        fci.Url = "DocumentName"            

        //add the new file to the form library
        var newFile = folder.Files.Add(fci);
        clientContext.Load(newFile);
        var item = newFile.ListItemAllFields;
        clientContext.Load(item);

        //set metadata for the xml file here
        item["HTML_x0020_File_x0020_Type"] = "InfoPath.Document.3";
        item["TemplateUrl"] =
            "http://templateUrlHere/template.xsn?openin=preferclient&noredirect=true&xsnlocation=/templateLocation/template.xsn";
        //set remaining item properties......

        //update changes to the item
        item.Update();

        //commit the changes
        clientContext.ExecuteQuery();

此时,我的 XML 文档已成功提交到我的表单库,但是当我打开它时,它在 Internet Explorer 中以纯 XML 的形式打开,而不是使用我指定的模板。我假设该项目的 HTML_x0020_File_x0020_Type 和 TemplateUrl 属性将为 SharePoint 指定足够的信息,以了解它需要使用 InfoPath 模板打开此文件,但也许还有一些其他属性需要我专门设置。有没有人有过类似问题的经验?起初我认为我用于模板的 URL 是错误的,但我直接从通过 InfoPath 模板创建的现有 XML 文档中复制了它,所以我认为这不是问题(我遗漏了任何真实的我上面的示例代码中的 URL 和文件名,所以请忽略这些 URL 不正确的事实)。我正在继续解决此问题,但与此同时,我认为有人可能已经解决了这个问题,并且可能能够提供一些见解。

提前感谢您的任何回答,感谢您提供的任何帮助。

【问题讨论】:

  • 你有没有想过这个问题?我正在处理类似的问题,但使用的是 SharePoint 对象模型。我目前的尝试是设置内容类型,但似乎它正在获取正确的内容类型......

标签: xml sharepoint infopath sharepoint-object-model form-library


【解决方案1】:

我知道这个问题被问到已经有一段时间了,但我想我找到了解决方案,或者至少找到了问题。

我最近解决了同样的问题。

当下载其中一个显示为 xml 的文件时,当它们实际上是 InfoPath 时,在记事本中打开它显示至少有与真实字符相同数量的空垃圾字符。

原来我使用XmlTextWriter 写信给MemoryStream。在XmlTextWriter 中,我使用的是Encoding.UTF8。将其更改为 Encoding.Default 后,文件已正确保存并显示为 InfoPath。

所以对于任何有类似问题的人,请检查 xml 文件中的值,如果有空值,请尝试删除并重新上传。然后检查创建文档的代码,如果是 UTF8,请尝试不同的编码,例如 UTF16。

【讨论】:

  • 感谢您的建议,谢谢!我还没有尝试过,但我会试一试,让你知道它是否适合我。
猜你喜欢
  • 2010-12-02
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多