C# 以编程方式将浏览器InfoPath表单保存到其中';s源列表

C# 以编程方式将浏览器InfoPath表单保存到其中';s源列表,c#,sharepoint,infopath,C#,Sharepoint,Infopath,我在MOSS 2007的表单库中找到了一个InfoPath表单。我认为带有保存(手动输入文件名)和关闭功能的InfoPath浏览器工具栏会使填写表单的过程过于复杂。我试图做的是简单地有一个按钮,后面有一些代码,它可以将表单保存到库中,在库中以自动生成的文件名打开表单,然后关闭到库中。为此,我编写了以下代码: public void SaveForm(string formXml, string fileName, Dictionary<string, string> extraCol

我在MOSS 2007的表单库中找到了一个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;
}
public void存储格式(字符串格式XML、字符串文件名、字典外列)
{
SPFolder formsLibraryFolder=SPContext.Current.List.RootFolder;
string lowerCaseFilename=fileName.ToLower();
如果(!lowerCaseFilename.EndsWith(“.xml”))
文件名+=“.xml”;
web.AllowUnsafeUpdates=true;
SPFile file=formsLibraryFolder.Files.Add(文件名,Encoding.UTF8.GetBytes(formXml));
如果((列外!=null)&&(列外计数>0))
{
foreach(列外的KeyValuePair列)
{
file.Item[column.Key]=column.Value;
}
file.Item.Update();
}
web.AllowUnsafeUpdates=false;
}
这段代码的问题在于,当我想将表单保存到打开表单的库中时,SPContext.Current.List为null。我显然错误地认为,由于表单将在浏览器中完成,因此列表的上下文将是有效的。 但是,我可以访问包含列表的SPWeb,但这意味着我需要为每种类型的表单硬编码列表名称,当然,我需要提前知道每个表单的列表名称。这段代码是我编写的助手库的一部分,我在不同的项目中以多种形式引用了它,所以我真的不能硬编码值。当然,我可以将列表名作为参数传递,并在表单中硬编码列表名,但这仍然意味着我必须提前知道表单将部署在哪里。
我有没有办法确定是在哪个库中单击“新建”来开始填写表单的?

我已经找到了方法,所以我将为其他人发布它

关闭浏览器表单时,InfoPath会将您重定向回列表。多亏了以下文章中的方法2,您可以获得列表的URL:

单击我自己的保存按钮时,我将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;
}
public void存储格式(字符串格式XML、字符串文件名、字符串url、字典外列)
{
SPWeb web=SPContext.Current.web;
字符串webUrl=web.Url;
如果(!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();
如果(!lowerCaseFilename.EndsWith(“.xml”))
文件名+=“.xml”;
web.AllowUnsafeUpdates=true;
SPFile file=destinationFolder.Files.Add(文件名,Encoding.UTF8.GetBytes(formXml));
如果((列外!=null)&&(列外计数>0))
{
foreach(列外的KeyValuePair列)
{
file.Item[column.Key]=column.Value;
}
file.Item.Update();
}
web.AllowUnsafeUpdates=false;
}