2010-03-04 35 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; 
} 
相關問題