[英]Creating publishing page in in SharePoint timer job throws MissingMethodException
我正在使用每隔幾分鍾運行的計時器作業為共享點站點做一個簡單的新聞導入器。 如果我在Web部件中運行以下代碼,則該代碼運行良好,但是在計時器作業的Execute方法中運行時,拋出MissingMethodException。
完整的例外:
System.MissingMethodException was unhandled by user code
Message="No parameterless constructor defined for this object."
Source="mscorlib"
StackTrace:
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at Microsoft.SharePoint.WebPartPages.SPWebPartSerializer.get_DefaultControl()
at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Serialize(PersonalizationScope scope)
at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.get_Links()
at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartToStore(WebPart webPart, Int32 viewId, String viewGuid)
at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart superset, Boolean throwIfLocked)
at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPartInternal(WebPart webPart, String zoneId, Int32 zoneIndex, Boolean throwIfLocked)
at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPart(WebPart webPart, String zoneId, Int32 zoneIndex)
at Microsoft.SharePoint.Publishing.PublishingPage.CopyAllWebParts(String destinationPageUrlServerRelative, SPWeb destinationPageWeb, String sourcePageUrlServerRelative, SPWeb sourcePageWeb, Boolean shouldOverwriteDestinationWebParts)
at Microsoft.SharePoint.Publishing.PublishingPageCollection.<>c__DisplayClass5.<Add>b__0()
at Microsoft.Office.Server.Diagnostics.FirstChanceHandler.ExceptionFilter(Boolean fRethrowException, TryBlock tryBlock, FilterBlock filter, CatchBlock catchBlock, FinallyBlock finallyBlock)
at Microsoft.Office.Server.Diagnostics.ULS.SendWatsonOnExceptionTag(ULSTagID tagID, ULSCat categoryID, String output, Boolean fRethrowException, TryBlock tryBlock, CatchBlock catchBlock, FinallyBlock finallyBlock)
at Microsoft.SharePoint.Publishing.PublishingPageCollection.Add(String name, PageLayout layout)
at Virtua.SharePoint.Mercator.NewsImportJob.Execute(Guid contentDbId)
at Microsoft.SharePoint.Administration.SPTimerJobInvoke.Invoke(TimerJobExecuteData& data, Int32& result)
InnerException:
編碼:
var SPWebUri = new Uri("http://.../News/");
using (SPSite site = new SPSite(SPWebUri.AbsoluteUri))
{
using (SPWeb web = site.OpenWeb(SPWebUri.AbsolutePath)) // tried 0x29A's anwser, but it didn't work
// using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
SPContentTypeId contentTypeId = new SPContentTypeId("...");
PageLayout layout = publishingWeb.GetAvailablePageLayouts(contentTypeId).First(l => l.Name == "....aspx");
PublishingPageCollection pages = publishingWeb.GetPublishingPages();
var spFilename = "testing";
// error line below
var newPage = pages.Add(String.Format("{0}.aspx", spFilename), layout);
// set newPage's fields
// check-in and publish page
}
}
不確定,但是可能是您需要為site.OpenWeb()提供url參數。如果url參數為空,則必須打開頂級站點。 這可以解釋為什么它可以在Web部件中工作,但不能在計時器作業中工作。 有關詳細信息,請參見MSDN-Site.OpenWeb方法 。
問候
問題是我的頁面布局上的Web部件有故障。 我在構造函數中使用SPContext.Current...
現在,顯然,執行計時器作業代碼時SPContext
東西不存在( SPContext
不足為奇了)。
由於未處理錯誤,因此Web部件管理器試圖用ErrorWebPart
替換我的部件(該部件通常告訴用戶出了問題)。 由於某些原因,在瀏覽器中顯示頁面時,此方法工作正常。 但是在計時器作業中,SharePoint嘗試使用無參數構造函數通過反射來創建ErrorWebPart
,該構造函數 沒有在ErrorWebPart
定義 ,並且整個事情因MissingMethodException
崩潰。
將有問題的代碼移到CreateChildControls
方法(我在其中進行錯誤處理)解決了該問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.