Search code examples
c#listdata-structuresfor-loopsharepoint-deployment

Lists and Values of the list in C#


I am from PHP, and I don't think I am understanding how to walk through data structures properly. The documentation for SPFieldCollection.Add is pretty clear what kind of arguments it takes, so I wrote a class that does:

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

From there I created a list that does:

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

The problem is, it doesn't like col.Value[0], col.Value[1] because one is not a SPFieldType and the other isn't a boolean by strict definition. I think I have the right Idea, but I am looking for guidance on how to make this work.

Because C# has type hinting I assumed that it would use the AddParams class to see the types.

The idea is to pass in a series of params and create a new list based on those params.

This is more of a C# question and data structure iteration question then a SP development question.


Solution

  • Change

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

    to

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