How to set a Job Description for a SharePoint timer custom task

I wonder how I can set a job description for a custom SharePoint timer job. When we look at the properties of job definitions through central management, there is the line "Job Description". But it is always empty in the timer job. I found several articles that should solve the problem.

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

But they both did not help.

If anyone had a familiar problem and resolved it, please share this solution. I would appreciate any help.

+4
source share
2 answers

Both of your links give the correct answer.

Description property SPJobDefinition is implemented as:

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

So, in order to have a custom description, you need to define your custom job definition as follows:

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

I wrote my timer job as follows:

 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; } } 

After that you need to add your work to the event event receiver

 [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(); } } } 

At the end, you can see your work in the Administration Center → Monitoring → Timer Jobs - View job definitions. There you can reset to define your schedule.

+1
source

Source: https://habr.com/ru/post/1398572/


All Articles