배포 단계에서 오류가 발생했습니다. '기능 활성화': 객체 참조 객체의 인스턴스로 설정되지 않음 : 기능 수신기 오류
-
29-09-2020 - |
문제
SP 2013에서 Visual Web Part를 Cretaed합니다. SharePoint 목록이 필요합니다.따라서 웹 파트 피처 수신기의 코드 아래에 썼습니다. 솔루션을 배포 할 때 i 오류가 발생하면 배포 단계 '기능 활성화'에서 오류가 발생했습니다. '개체 참조가 객체의 인스턴스로 설정되지 않았습니다.i activateondefault="false"를 설정하고 alwaysforceinstall="true"를 설정합니다. 내 기능 매니페스트 파일을 살펴보십시오.
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Title="SharePointProject1 Feature1" ActivateOnDefault="FALSE" AlwaysForceInstall="TRUE" Id="8f7163d5-6c65-40d8-9045-8f74192f07d7" ReceiverAssembly="SharePointProject1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9c2f0c3a8e22f6a0" ReceiverClass="SharePointProject1.Features.Feature1.Feature1EventReceiver" Scope="Site">
<ElementManifests>
<ElementManifest Location="VisualWebPart1\Elements.xml" />
<ElementFile Location="VisualWebPart1\VisualWebPart1.webpart" />
</ElementManifests>
</Feature>
.
이것은 내 feature1EventReceiver 코드입니다.
SPWeb spWeb = properties.Feature.Parent as SPWeb;
SPList laptopList = spWeb.Lists["Laptops"];
if (laptopList != null)
{
laptopList.Delete();
}
SPListCollection lists = spWeb.Lists;
lists.Add("Laptops", "The Laptops", SPListTemplateType.GenericList);
laptopList = spWeb.Lists["Laptops"];
laptopList.Fields.Add("Name", SPFieldType.Text, true);
laptopList.Fields.Add("Model", SPFieldType.Text, true);
laptopList.Fields.Add("Image", SPFieldType.URL, false);
.
코드 아래
가있는 경우 배포 할 수 있습니다. SPWeb spWeb = properties.Feature.Parent as SPWeb;
. 해결책
Visual Web Part는 결과 properties.feature.parent는 Swite 객체 인 경우 사이트 범위 기능에 사이트 범위 기능에 포함되어야합니다.기능 범위를 확인하고 범위가 사이트 인 경우 아래 코드를 사용해야합니다.
SPSite site = properties.Feature.Parent as SPSite;
if (site != null){
SPWeb web = site.RootWeb;
SPList laptopList = web.Lists["Laptops"];
...
}
. 다른 팁
web.lists []는 목록이 없으면 예외가 발생합니다.
TryGetList 대신
나를 위해 일한 것 사이트 에서 웹
에서 기능 범위 수준을 변경했습니다.제휴하지 않습니다 sharepoint.stackexchange