Question

Coming from PHP I am not use to assigning or returning specific types because PHP really doesn't care. But Coming back to the world of Java and C# these languages do care, when you say pass me this type it expects that type. So what am I do doing wrong and how can I create this to be of type SPList

I have a very basic function such as:

    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");
    }

This will create you a List, a SharePoint 2010 list upon activation of the web part. the name is Fake List and we see we pass in some collumns with their respected parameters. Lets look at this SPAPI.Lists.Create method:

    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();
        }
    }

We can see here that all were doing is creating a SPList object for use in Sharepoint. Upon deployment we have a new list we can add to our pages. So whats the problem?

Well in Php I could pass the createNewList(SPFeatureReceiverProperties properties) to a function requesting the object of type SPList and it would work (unless im missing something >.>) Here its like, no that's not a SPList go away.

So My question is:

What do I have to change so that I can both create the list and return the SPLIst object? is it as simple as return new SPAPI.Lists.Create(properties, param, "Fake List", "Sample Description", SPListTemplateType.GenericList, "Sample View Description");

Because that seems right to me.

Update

Turning the method signature to SPList and returning the return new .... did not work.

Était-ce utile?

La solution

You need to return an SPList from both your methods:

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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top