【发布时间】:2013-06-04 19:10:19
【问题描述】:
我来自 PHP,我认为我不了解如何正确遍历数据结构。 SPFieldCollection.Add 的文档非常清楚它需要什么样的参数,所以我写了一个类:
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 开发问题。
【问题讨论】:
-
您是否尝试将 col.Value[0] 和 col.Value[1] 转换为各自的类型?
-
@Rubixus Cast what...那些都是 AddParams,只是第一个和第二个。
-
@Don 也许我不明白你的问题。对我来说,您的问题似乎是 col.Value[0] 应该作为 SPFieldType 添加到 Fields,而 col.Value[1] 应该是布尔值。但在看到伊戈尔的回答后,我明白你在追求什么。
标签: c# list data-structures for-loop sharepoint-deployment