c#的方法创建一个列表,但不是列表类型的



来自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列表。名字是Fake List,我们看到我们传入了一些列和它们的参数。让我们看看这个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();
        }
    }

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

在Php中,我可以将createNewList(SPFeatureReceiverProperties properties)传递给一个请求SPList类型对象的函数,它会工作(除非我遗漏了什么>.>)这里就像,不,那不是SPList走开。

所以我的问题是:

我需要做什么修改才能既创建列表又返回SPLIst对象?return new SPAPI.Lists.Create(properties, param, "Fake List", "Sample Description", SPListTemplateType.GenericList, "Sample View Description");一样简单吗

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

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

你需要从你的两个方法中返回一个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;
}

相关内容

  • 没有找到相关文章

最新更新