简体   繁体   中英

Why is the SiteCollection Always null?

I have the function:

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

        SPList spList = null;

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

            return spList;
        }

        return spList;
    }

Yet for some reason when debugging the siteCollection is coming back null which is causing it to not do what I want it to. When I inspect properties.Feature.Parent I see the site Name: 'Team Site'

So why is this null?

Update:

This project, is site scoped

There are two possible reasons that siteCollection might be null after the line instantiating it runs:

  1. properties.Feature.Parent is null
  2. properties.Feature.Parent is not an SPSite (or derived from SPSite )

If you're saying that you can see in the debugger that properties.Feature.Parent is not null when siteCollection is, that means that properties.Feature.Parent is not an SPSite instance.

You can see the type of this property when debugging by opening a Watch window and entering the expression properties.Feature.Parent.GetType() .

Because it is not a kind of SPSite? So when you cast it as something it's not it becomes null?

Perhaps the properties.Feature.Parent is not an SPSite object. The code as SPSite will set siteCollection to Null if it's an SPWeb object

If you're trying to debug this, after this line SPSite siteCollection = properties.Feature.Parent as SPSite; , if you run this in either your watch window or immediate window properties.Feature.Parent.GetType() you should be able to identify the type of object it's returning, note that it has to be a compatible type (to SPSite) to be able to cast otherwise you get the null object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM