我的职责是:
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时,我看到了网站名称:“Team”
那么为什么这个值为空呢?
更新:
此项目是站点范围内的。
发布于 2013-06-06 18:40:19
在行实例化运行后,siteCollection可能是null的原因有两个:
properties.Feature.Parent是nullproperties.Feature.Parent不是SPSite (或派生自SPSite)如果您说在调试器中可以看到properties.Feature.Parent不是null,而siteCollection是,那就意味着properties.Feature.Parent不是SPSite实例。
通过打开“监视”窗口并输入表达式properties.Feature.Parent.GetType(),可以在调试时看到此属性的类型。
发布于 2013-06-06 18:39:03
因为它不是一种SPSite?所以当你把它当作某物的时候,它不是空的吗?
发布于 2013-06-06 18:39:50
也许properties.Feature.Parent不是SPSite对象。代码as SPSite将siteCollection设置为Null,如果它是SPWeb对象
https://stackoverflow.com/questions/16969619
复制相似问题