2

私は 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 ...
    }
}

問題は、厳密な定義により、一方が aではなく、他方がa ではないため、 が好きcol.Value[0]ではないことです。私は正しいアイデアを持っていると思いますが、これを機能させる方法についてのガイダンスを探しています。col.Value[1]SPFieldTypeboolean

C# には型ヒントがあるため、AddParamsクラスを使用して型を確認すると想定しました。

アイデアは、一連のパラメーターを渡し、それらのパラメーターに基づいて新しいリストを作成することです。

これは、SP 開発の質問というよりも、C# の質問とデータ構造の反復に関する質問です。

4

3 に答える 3

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)
{
}
于 2013-06-04T19:19:27.543 に答える
-1

のリストがあるのはなぜAddParamsですか? 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 ...
    }
}
于 2013-06-06T17:54:42.113 に答える