在SharePoint中引用Web/站点对象以进行计时器作业



哪种是从下面给出的示例中参考专门用于计时器作业的当前网站/Web的最佳方法,为什么?

示例1:

SPWebApplication webApp = this.Parent as SPWebApplication; SPWeb oWeb = webApp.Sites[0].RootWeb;

示例2:

using(SPSite site=new SPSite(SPCOntext.Current.Web.Url)){ using(SPWeb web=site.OpenWeb()){
} }

timerjob内部没有spcontext.current。这不是一个选项。您的示例1是一个选择,但是我一直在与其他解决方法一起工作。

当您通过代码部署TimerJob并使用特定的时间表激活它们时,您可以设置TimerJob属性,该属性比" execute"方法被称为称为。

这是我在SpweBapplication功能中的解决方案之一中使用的代码:

var webApplication = (SPWebApplication) properties.Feature.Parent;
var newTimerJob = new new MyTimerJob("Timerjobname", webApplication);
newTimerJob.Properties.Add("key", "url");
newTimerJob.Schedule = schedule;
newTimerJob.Update();

执行方法比包含这样的内容:

if (Properties["key"] != null && Properties["key"].ToString() != string.Empty)
        { var mySite = new SPSite(Properties["key"].ToString()); }

最新更新