簡體   English   中英

如何設置自定義SharePoint計時器作業的“作業說明”

[英]How to set “Job Description” of a custom SharePoint Timer Job

我想知道,如何設置自定義SharePoint計時器作業的作業說明。 當我們通過集中管理查看作業定義屬性時,會出現“作業描述”行。 但它在自定義計時器作業中始終為空。 我找到了一些必須解決問題的文章。

http://thedotnetter.wordpress.com/2011/09/07/setting-the-job-description-of-a-custom-sharepoint-timer-job/

http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/77da488a-b218-4922-b79b-f7b08f68fb3c#345fdac3-25cd-4a1e-b6e2-6aaf4bbb119a

但他們都沒有帶來任何幫助。

如果,任何人都有熟悉的問題並解決了,請分享決定。 我將不勝感激任何幫助。

您的兩個鏈接都給出了正確的答案。

SPJobDefinitionDescription屬性實現為:

public virtual string Description
{
    get
    {
        return string.Empty;
    }
}

因此,為了獲得自定義描述,您需要定義自定義作業定義,如下所示:

public class MyCustomJobDefinition : SPJobDefinition
{
    public override string Description
    {
        get
        {
            return "This is my custom description";
        }
    }
}

我寫了這樣的計時器工作:

public class YourJob : SPJobDefinition
{
    private static string JOB_NAME = "YourJobName";
    public override string Description
    {
        get
        {
            return "YourDescription";
        }
    }
    public YourJob() : base() { }

    public YourJob(SPWebApplication webApp)
        : base(JOB_NAME, webApp, null, SPJobLockType.None)
    {
        this.Title = JOB_NAME;
        this.Schedule = GetSchedule();
    }
    //This job start to run every day between 00:00 to 00:30
    //There are several options
    private SPSchedule GetSchedule()
    {
        SPDailySchedule myDailySchedule = new SPDailySchedule();
        myDailySchedule.BeginHour = 00;
        myDailySchedule.BeginMinute = 00;
        myDailySchedule.BeginSecond = 0;
        myDailySchedule.EndHour = 00;
        myDailySchedule.EndMinute = 30;
        myDailySchedule.EndSecond = 0;

        return myDailySchedule;
    }

    public override void Execute(Guid targetInstanceId)
    {
        //Write here your code.
        //In this example we get value from SP (in every zone) web config to do something with it.
        foreach (SPUrlZone urlZone in Enum.GetValues(typeof(SPUrlZone)))
        {
            if (((SPWebApplication)this.Parent).IisSettings.ContainsKey(urlZone))
            {
                var zone = ((SPWebApplication)this.Parent).IisSettings[urlZone];
                var appName = zone.ServerComment;

                var WebConfigKey = GetAppSettings(appName, "WebConfigKey");
            }
        }
    }

    private string GetAppSettings(string appName, string Key)
    {
        string result = String.Empty;
        SPWebApplication webApplication = this.Parent as SPWebApplication;
        Configuration config = WebConfigurationManager.OpenWebConfiguration("/", appName);
        if (config.HasFile && config.AppSettings.Settings[Key] != null)
        {
            result = config.AppSettings.Settings[Key].Value;
        }
        return result;
    }
}

之后,您需要將您的工作添加到功能事件接收器

[Guid("46b3a9b4-793e-4ab9-99ba-b003a3601e3a")]
public class MainEventReceiver : SPFeatureReceiver
{
    public static string JOB_NAME = "YourJobName";

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPSite site = properties.Feature.Parent as SPSite;

        // Make sure the job isn't already registered.
        foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
        {
            if (job.Name == JOB_NAME)
                job.Delete();
        }

        YourJob job = new YourJob(site.WebApplication);
        job.Update();
    }

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        SPSite site = properties.Feature.Parent as SPSite;

        // Delete the job.
        foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
        {
            if (job.Name == JOB_NAME)
                job.Delete();
        }
    }
}

最后,您可以在管理中心 - >監控 - >計時器作業 - 查看作業定義中查看您的工作。 在那里,您可以重置您的計划定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM