我有这个功能:
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;
}
但是出于某种原因,在调试时,siteCollection将返回null,导致它无法按照我的意愿执行操作。当我检查properties.Feature.Parent
时,我看到网站名称:'团队网站'
那为什么这是空的?
更新
此项目是网站范围
答案 0 :(得分:1)
在siteCollection
实例化它之后,null
可能properties.Feature.Parent
可能有两个原因:
null
是properties.Feature.Parent
SPSite
不是SPSite
(或源自properties.Feature.Parent
)如果您说调试器中null
在siteCollection
时properties.Feature.Parent
不是SPSite
,则表示properties.Feature.Parent.GetType()
不是{{1}实例。
通过打开Watch窗口并输入表达式{{1}},可以在调试时看到此属性的类型。
答案 1 :(得分:0)
因为它不是一种SPSite?所以当你把它当作某种东西而不是它变成空的时候?
答案 2 :(得分:0)
也许properties.Feature.Parent
不是SPSite对象。
如果as SPSite
是SPWeb对象,代码siteCollection
会将Null
设置为{{1}}
答案 3 :(得分:0)
如果您正在尝试调试此行,请在此行SPSite siteCollection = properties.Feature.Parent as SPSite;
之后,如果您在监视窗口或即时窗口properties.Feature.Parent.GetType()
中运行此操作,您应该能够识别它返回的对象类型,请注意,它必须是一个兼容的类型(对于SPSite),否则你将获得null对象。