首页 文章

设置以编程方式创建的XML文档,以便在SharePoint中使用InfoPath模板打开

提问于
浏览
1

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

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

我试图找出为什么从Web服务生成的XML文档在这方面的行为与从InfoPath生成的XML文档不同 . 以下是我用于在Web服务中创建XML文档的代码片段:

//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不正确的事实) . 我正在继续解决这个问题,但与此同时我认为有人可能已经解决了这个问题,并且可能能够提供一些见解 .

提前感谢您的任何答案,我感谢您提供的任何帮助 .

1 回答

  • 0

    我知道问题已经有一段时间了,但我认为我找到了解决方案,或者至少是问题所在 .

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

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

    事实证明我正在使用 XmlTextWriter 写入 MemoryStream . 在 XmlTextWriter 我正在使用 Encoding.UTF8 . 将其更改为Encoding.Default后,文件已正确保存并显示为InfoPath .

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

相关问题