c# - 在 SharePoint 计时器作业中创建发布页面会引发 MissingMethodException

标签 c# sharepoint sharepoint-2007 moss timer-jobs

我正在使用每隔几分钟运行一次的计时器作业为共享点网站做一个简单的新闻导入程序。如果我在 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 Method了解详情。

问候

关于c# - 在 SharePoint 计时器作业中创建发布页面会引发 MissingMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3761458/

相关文章:

c# - 在 C# 多行逐字字符串中转义 string.Format 的参数

sql-server - 在哪里可以找到有关 Sharepoints 后端数据库的文档?

java - 将权限掩码转换为 GetPermissionCollection 的角色

sharepoint - 如何修改共享点网站上的表格?

c# - 使用 vbs 但不使用 C# 的 SendKey 命令

c# - 是否可以使用指向基类型的 AncestorType 进行 WPF RelativeSource 绑定(bind)?

c# - Entity Framework 4.0 中的反序列化问题

sharepoint - 使用PowerShell自动部署解决方案

sharepoint - 在具有多个 WFE 的服务器场环境中部署计时器作业的最佳实践

sharepoint - 从支持 SSL 的 SharePoint 站点打开文件