我有一个自定义计时器作业,将数据从外部源将数据导入我的SharePoint 2010安装中的特定网络上的特定列表。这就是工作仅与一个网络上的一个列表有关。

我有一段时间的魔鬼在我的内部获得对该网络的参考(以及列表) SPJobDefinition 班级。

我可以对网站的路径进行编码,并使用新的SPSITE(“ someurl”)命令获得它,但是我更喜欢从工作的上下文中获得它,因此它将在我的开发,测试和生产环境中无缝地工作。

我尝试通过站点指导 SPEventreceiver 这是安排工作的,但是只要我 SPJobDefinition 创建对象它似乎正在使用没有参数的构造函数,而不是我在 SPEventReciever.FeatureActivated 处理程序。

所以,我的问题是:
如何从内部获取对SpWeb对象(或Spsite对象)的引用 SPJobDefinition.Execute() 方法?

有帮助吗?

解决方案

我相信您已经创建了Web应用程序范围的计时器作业。如果没有,您可能需要这样做。看 参考 (步骤2,子弹第7号)

然后如第2步中所述,Bullet项目10中的链接10中,您可以像这样编写代码:

public override void Execute(Guid targetInstanceId)
{
  // Execute the timer job logic.
  SPWebApplication webApp = this.Parent as SPWebApplication;
  SPList taskList = webApp.Sites[0].RootWeb.Lists["Tasks"];     
}

更新:除此之外,在您的功能反应事件中,您可能需要检查该功能正在激活该功能的站点集合(或站点)是否具有计时器作业的列表。如果是,则仅创建Web应用程序范围内计时器作业的实例。

其他提示

在您的工作中有一个构造函数,该构造函数接收SPWEB或字符串URL,然后存储Web URL并列出您想要的其他属性作为作业中的持久属性。

我建议创建一个网络窃取功能来安装计时器作业,并使用具有Web ID的名称创建它(为了唯一的缘故,如果您希望在多个网上工作)。功能激活的事件将具有网络及其所有属性,以便能够传递到作业的构造函数中。

有关以下信息,请参见我的博客文章:http://spmonkeypoint.wordpress.com/2011/11/14/custom-sharepoint-2010-timer-job

这是该帖子中的代码列表:

using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace MonkeyPoints.TimerJobs
{
    [Guid("6DD003C3-2861-4F63-B974-D2653E713A74")]
    public class CustomTimerJob : SPJobDefinition
    {
        [Persisted]
        public string WebUrl;

        [Persisted]
        public string ListUrl;

        public CustomTimerJob() : base() { }

        public CustomTimerJob(SPWeb web, string listUrl) : this(JobName(web), web, listUrl) { }

        public CustomTimerJob(string jobName, SPWeb web, string listUrl)
            : base(jobName, web.Site.WebApplication, null, SPJobLockType.Job)
        {
            this.WebUrl = web.Url;
            this.ListUrl = listUrl;
        }

        protected static string JobName(SPWeb web)
        {
            return "CustomTimerJob_" + web.ID;
        }

        protected override bool HasAdditionalUpdateAccess()
        {
            return true;
        }

        public override void Execute(Guid targetInstanceId)
        {
            try
            {
                using (var site = new SPSite(WebUrl))
                using (var web = site.OpenWeb())
                {
                    // process whatever you need to on this list
                }
            }
            catch (Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(this.Name, TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
            }
        }

        public static void Install(SPWeb web, string listUrl, SPSchedule schedule)
        {
            using (SPSite site = new SPSite(web.Site.ID, web.Site.SystemAccount.UserToken))
            using (SPWeb eweb = site.OpenWeb(web.ID))
            {
                site.AllowUnsafeUpdates = true;
                eweb.AllowUnsafeUpdates = true;
                Uninstall(eweb);
                var syncJob = new CustomTimerJob(eweb, listUrl);
                syncJob.Schedule = schedule;
                syncJob.Update();
            }
        }

        public static void Uninstall(SPWeb web)
        {
            var jobName = JobName(web);
            foreach (SPJobDefinition job in web.Site.WebApplication.JobDefinitions)
            {
                if (job.Name == jobName)
                    job.Delete();
            }
        }

        public static void RunNow(string webUrl, string listUrl)
        {
            using (SPSite site = new SPSite(webUrl))
            using (SPWeb web = site.OpenWeb())
            {
                var job = new CustomTimerJob(web, listUrl);
                job.Execute(Guid.Empty);
            }
        }
    }
}

您可以创建一个针对Web应用程序配置设置的页面,然后使用Web应用程序设置来控制所使用的SPWEB。

为此,您将应用程序页面部署到SharePoint中。您将在Central Admin中提供可用(可能使用WebApplicationselector控件来选择要设置该值的Web应用程序)。

(您在Central Admin中这样做的原因是,更新Web应用程序的属性需要将访问访问到配置数据库中 - 并且您可能只在中央管理员中使用此访问权限,具体取决于您的配置)

然后,在“保存”后返回中为您提供新的,因为您将拥有类似的东西:

protected WebApplicationSelector _selector = ...

public void SaveIt() {
  SPWebAppliction webApp = _selector.CurrentItem;
  webApp.Properties["MyWeb"] = "SomeValue";
  webApp.Update();
}

然后,在计时器作业中,您应该能够访问Web应用程序的属性并阅读所需的值。

在您的TimerJob代码中,您可以通过上下文Web应用程序中的所有站点收集进行局限在该站点集合中。

许可以下: CC-BY-SA归因
scroll top