2013-06-04 52 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 ... 
    } 
}