2013-06-04 87 views
2

我來自PHP,我不認爲我理解如何正確地瀏覽數據結構。爲SPFieldCollection.Add文檔是很清楚什麼樣的需要論據,所以我寫了一個類,它的作用:C#中列表的列表和值#

namespace SPAPI.Lists.Add 
{ 
    class AddParams 
    { 
     public SPFieldType type { get; set; } 
     public bool required { get; set; } 
    } 
} 

從那裏,我創建了一個確實的列表:

public Create(SPFeatureReceiverProperties properties, Dictionary<string, List<AddParams>> columns, string name, string description, SPListTemplateType type) 
{ 
    SPSite siteCollection = properties.Feature.Parent as SPSite; 
    if (siteCollection != null) 
    { 
     SPWeb web = siteCollection.RootWeb; 
     web.Lists.Add(name, description, type); 
     web.Update(); 

     // Add the new list and the new content. 
     SPList spList = web.Lists[name]; 
     foreach(KeyValuePair<string, List<AddParams>> col in columns){ 
      spList.Fields.Add(col.Key, col.Value[0], col.Value[1]); 
     } 

     // More Code ... 
    } 
} 

的問題是,它不喜歡col.Value[0]col.Value[1],因爲嚴格定義,其中一個不是SPFieldType而另一個不是boolean。我認爲我有正確的想法,但我正在尋找如何完成這項工作的指導。

因爲C#有類型提示,我假設它會使用AddParams類來查看類型。

這個想法是傳遞一系列參數,並基於這些參數創建一個新列表。

這是更多的C#問題和數據結構迭代問題,然後SP開發問題。

+0

您是否嘗試過鑄造col.Value [0]和col.Value [1]各自的類型? – Rubixus

+0

@Rubixus施展什麼...那些都是AddParams,只是第一個和第二個。 – DonBoitnott

+0

@Don也許我不明白你的問題。對我來說,似乎你的問題是col.Value [0]應該作爲SPFieldType添加到Fields中,而col.Value [1]應該是布爾值。但在看到伊戈爾的回答後,我明白你的意思。 – Rubixus

回答

1

變化

spList.Fields.Add(col.Key, col.Value[0], col.Value[1]); 

spList.Fields.Add(col.Key, col.Value[0].type, col.Value[0].required); 
4

col.Value[0]col.Value[1]都是AddParams類型。這可能會編譯:

spList.Fields.Add(col.Key, col.Value[0].type, col.Value[0].required); 

,但你可能需要另一個foreach裏面你foreach

foreach(AddParams item in col.Value) 
{ 
} 
-1

爲什麼你有AddParams的列表?你是否期待同一領域有超過1 FieldType

我想你應該執行這樣說:

public Create(SPFeatureReceiverProperties properties, Dictionary<string, AddParams> columns, string name, string description, SPListTemplateType type) 
{ 
    SPSite siteCollection = properties.Feature.Parent as SPSite; 
    if (siteCollection != null) 
    { 
     SPWeb web = siteCollection.RootWeb; 
     web.Lists.Add(name, description, type); 
     web.Update(); 

     // Add the new list and the new content. 
     SPList spList = web.Lists[name]; 
     foreach(string key in columns.Keys){ 
      spList.Fields.Add(key, columns[key].type, columns[key].required); 
     } 

     // More Code ... 
    } 
}