C#,Method创建一个列表但不是类型列表?

时间:2013-06-06 17:07:17

标签: c# sharepoint-2010 type-hinting splist

来自PHP我不是用来分配或返回特定类型,因为PHP实际上并不关心。但是回到Java和C#的世界,这些语言确实很关心,当你说这个类型传递给我时,它期望这种类型。那我该怎么做错了怎么能把它创建为 SPList

我有一个非常基本的功能,例如:

    protected void createNewList(SPFeatureReceiverProperties properties)
    {
        Dictionary<string, List<AddParams>> param = new Dictionary<string, List<AddParams>>();

        // Create the keys
        param.Add("Name", new List<AddParams>());
        param.Add("Type", new List<AddParams>());
        param.Add("Description", new List<AddParams>());

        // Set the values
        param["Name"].Add(new AddParams { type = SPFieldType.Text, required = true });
        param["Type"].Add(new AddParams { type = SPFieldType.Text, required = true });
        param["Description"].Add(new AddParams { type = SPFieldType.Text, required = true });

        // Create the really simple List.
        new SPAPI.Lists.Create(properties, param, "Fake List", "Sample Description", SPListTemplateType.GenericList, "Sample View Description");
    }

这将在激活Web部件时为您创建一个列表,一个SharePoint 2010列表。名称是假清单,我们看到我们传递一些带有他们尊重的参数的列。让我们看一下这个SPAPI.Lists.Create方法:

    public Create(SPFeatureReceiverProperties properties, Dictionary<string, List<AddParams>> columns, 
        string name, string description, SPListTemplateType type, string viewDescription)
    {

        SPSite siteCollection = properties.Feature.Parent as SPSite;
        if (siteCollection != null)
        {
            SPWeb web = siteCollection.RootWeb;
            Guid Listid = 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].type, col.Value[0].required);
            }

            spList.Update();

            //Create the view? - Possibly remove me.
            System.Collections.Specialized.StringCollection stringCollection =
                new System.Collections.Specialized.StringCollection();

            foreach (KeyValuePair<string, List<AddParams>> col in columns)
            {
                stringCollection.Add(col.Key);
            }

            //Add the list.
            spList.Views.Add(viewDescription, stringCollection, @"", 100,
                true, true, Microsoft.SharePoint.SPViewCollection.SPViewType.Html, false);
            spList.Update();
        }
    }

我们在这里可以看到,所有人都在创建一个在Sharepoint中使用的SPList对象。部署后,我们有一个新列表,我们可以添加到我们的页面。那问题是什么?

好吧在Php我可以将createNewList(SPFeatureReceiverProperties properties)传递给一个请求类型为SPList的对象的函数,它会工作(除非我遗漏了某些东西&gt;。&gt;)就像这样,不,这不是一个SPList就会消失

所以我的问题是:

我需要更改哪些内容才能创建列表并返回SPLIst对象?就像return new SPAPI.Lists.Create(properties, param, "Fake List", "Sample Description", SPListTemplateType.GenericList, "Sample View Description");

一样简单

因为这对我来说似乎是正确的。

更新

将方法签名转换为SPList并返回return new ....不起作用。

1 个答案:

答案 0 :(得分:0)

您需要从两个方法中返回一个SPList:

protected SPList createNewList(SPFeatureReceiverProperties properties)
{
    //Do the stuff

    SPList result = new SPAPI.Lists.Create(properties, param, "Fake List", "Sample Description", SPListTemplateType.GenericList, "Sample View Description");
    return result;
}

public SPList Create(SPFeatureReceiverProperties properties, Dictionary<string, List<AddParams>> columns, 
    string name, string description, SPListTemplateType type, string viewDescription)
{
    // Do the stuff

    return spList;
}