2010-03-04 80 views
1

我在MOSS 2007上的表单库中有一个InfoPath表单。我认为InfoPath浏览器工具栏与它保存(手动输入文件名)并关闭,而不是过度复杂填写表格。我想要做的只是一个带有一些代码的按钮,它将表单保存到使用自动生成的文件名打开的库中,并关闭回库。要做到这一点,我写了下面的代码:以编程方式将浏览器InfoPath表单保存到源列表中

public void SaveForm(string formXml, string fileName, Dictionary<string, string> extraColumns) 
{ 
    SPFolder formsLibraryFolder = SPContext.Current.List.RootFolder; 

    string lowerCaseFilename = fileName.ToLower(); 
    if (!lowerCaseFilename.EndsWith(".xml")) 
     fileName += ".xml"; 

    web.AllowUnsafeUpdates = true; 
    SPFile file = formsLibraryFolder.Files.Add(fileName, Encoding.UTF8.GetBytes(formXml)); 

    if ((extraColumns != null) && (extraColumns.Count > 0)) 
    { 
     foreach (KeyValuePair<string, string> column in extraColumns) 
     { 
      file.Item[column.Key] = column.Value; 
     } 
     file.Item.Update(); 
    } 

    web.AllowUnsafeUpdates = false; 
} 

用事实证明这段代码的问题是,SPContext.Current.List在空的情况,我想的形式保存到库在那里它被打开了。我明显错误地认为,因为表单将在浏览器内部完成,所以列表的上下文将是有效的。 但是,我可以访问包含列表的SPWeb,但这意味着我需要为每种表格类型硬编码列表名称,当然事先知道每个表格的列表名称。这段代码是我编写和参考了许多不同项目的帮助程序库的一部分,所以我真的不能硬编码值。我当然可以传递列表名称作为参数,并在表单名称中对表单本身进行硬编码,但这仍然意味着我必须事先知道将在何处部署表单。 有没有什么办法可以解决新点击库的问题,从而开始填写表单?

回答

3

我已经想出了如何做到这一点,所以我会把它发布给其他人。

当您关闭浏览器表单时,InfoPath会将您重定向回列表。你可以得到的URL列表得益于方法2中的以下文章:

http://www.bizsupportonline.net/blog/2010/01/2-ways-retrieve-sharepoint-site-collection-infopath-browser-form/

当我自己保存按钮被点击我通过URL来更新我的节电功能。我应该指出,这不是简单的代码,有几个地方可能会破坏。但它确实适用于需要使用它的特定情况。

public void SaveForm(string formXml, string fileName, string url, Dictionary<string, string> extraColumns) 
{ 
    SPWeb web = SPContext.Current.Web; 
    string webUrl = web.Url; 
    if (!webUrl.EndsWith("/")) 
     webUrl += "/"; 

    string relativeUrl = url.Replace(webUrl, string.Empty); 
    string listName = relativeUrl.Substring(0, relativeUrl.IndexOf('/')); 
    SPList destinationList = web.Lists[listName]; 
    SPFolder destinationFolder = destinationList.RootFolder; 

    string lowerCaseFilename = fileName.ToLower(); 
    if (!lowerCaseFilename.EndsWith(".xml")) 
     fileName += ".xml"; 

    web.AllowUnsafeUpdates = true; 
    SPFile file = destinationFolder.Files.Add(fileName, Encoding.UTF8.GetBytes(formXml)); 

    if ((extraColumns != null) && (extraColumns.Count > 0)) 
    { 
     foreach (KeyValuePair<string, string> column in extraColumns) 
     { 
      file.Item[column.Key] = column.Value; 
     } 
     file.Item.Update(); 
    } 

    web.AllowUnsafeUpdates = false; 
} 
相关问题