Windows Service with Timer based on CRM configuration value

Here I will be describing about a scenario where a windows service will perform a task based on time configured in CRM.

Please find the below problem statement:

Create a custom entity named “Configuration” which will hold the interval time at which the windows service should fire an email. Following are the steps to solve the problem:

1 . Browse to CRM>Settings>Customization>Customize Entities>Configuration:

2. Click on Add and enter the following values and click on OK button:

3. Click on “Save and Close” and Publish.

4. Goto >Workspace>Extensions >Configuration

5. Click on New and enter the following values:

6. Click on Save and Close.

7. Please refer the link for how to create a windows service.

Now in your existing windows service add the following code:

public class Program : ServiceBase{
private Timer timer = null;
private Microsoft.Crm.SdkTypeProxy.CrmService service = new Microsoft.Crm.SdkTypeProxy.CrmService();
private string[] timeValues = null;
public Program()
this.ServiceName = "EmailService";
//// Set the timer to fire every sixty seconds
//// so 1000 = 1 second. )
this.timer = new Timer(60000);
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerElapsed);
public static void Main()
ServiceBase.Run(new Program());
protected override void OnStart(string[] args)
this.timeValues = <span style="background-color: yellow;">GetEmailConfiguration(this.service)</span>;//this method returns the time interval value form CRM
protected override void OnStop()
protected void TimerElapsed(object sender, ElapsedEventArgs e)
foreach (string times in this.timeValues)
if (DateTime.Now.ToString("HH:mm", CultureInfo.InvariantCulture) == times)

//Code for sending Email based on the values in CRM


8. Build the solution in release mode and Go to the Release Folder.

9. Change the configuration as per the requirements in the EmailService.exe.config.

10. Goto command prompt and copy the following:

 cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 

11. Enter and then again copy the following:

Installutil.exe <path of the exe>\ <name of the .exe file>

12. Go to services.msc and double-click on “EmailService

13. Go to “LogOn” tab and select “This Account” and give the appropriate credentials.

14. Right Click on the service and start the service as shown below:

15.  Now the service has started and will send the email at specified intervals to the respective recipients.

Happy Coding 🙂