Sitefinity ASP.NET CMS

Implementing scheduled services in Sitefinity 3.x

In this post I will show you how to create scheduled services that could be executed on a certain period of time - hourly, daily, weekly, monthly etc. The post will expose web service that runs a Sitefinity search index. The implementation requires

 

1.Creating a web service in Sitefinity

2. Creating a console application that will be used to call the service

3. Using Windows Task Scheduler.

 

Creating a web service.

 

WebService

  • Leave the generated asmx in projet's root. 
  • Make sure that the generated class is located in App_Code folder

 

In the WebMethod I'm adding getting all services using IndexDataManager object. Then I am looping through all indexes and if the name of the index that I am going to pass exists in the IndexingServiceInfo I run the index programmatically using IndexingService object

 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using Telerik.Search; 
using Telerik.Search.Data; 
using System.Collections; 
using Telerik.Search.Engine; 
 
/// <summary>  
/// Summary description for IndexingService  
/// </summary>  
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
// [System.Web.Script.Services.ScriptService]  
public class IndexingService : System.Web.Services.WebService 
 
    public IndexingService() 
    { 
 
        //Uncomment the following line if using designed components   
        //InitializeComponent();   
    } 
 
    [WebMethod] 
    public void StartIndex(string indexName) 
    { 
        IndexDataManager dataManager = new IndexDataManager(); 
        IList indexes = dataManager.GetServices(); 
        IndexingServiceInfo indexServiceInfo = null
        foreach (IndexingServiceInfo service in indexes) 
        { 
 
            if (service.Name == indexName) 
            { 
                indexServiceInfo = service; 
                break
            } 
        } 
        if (indexServiceInfo != null
        { 
            Telerik.Search.Engine.IndexingService service = 
                new Telerik.Search.Engine.IndexingService(indexServiceInfo); 
            int timeout = this.Context.Server.ScriptTimeout; 
            this.Context.Server.ScriptTimeout = 4800; 
            service.Index(false); 
            this.Context.Server.ScriptTimeout = timeout; 
        } 
        else 
        { 
            throw new ApplicationException("not found name"); 
        } 
    } 

.

 

Creating Console Application

  • Create the console application

 ConsoleApplication

 

  • Add Web reference to the WebService that is located in Sitefinity website

 

  • Implement the logic in the console application to call the WebService method, pass the name of the index and finally run it.

 

namespace IndexConsoleApplication 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            StartIndex("RunFromConsole"); 
        } 
        public static void StartIndex(string indexName) 
        { 
            IndexingService client = new IndexingService(); 
            client.StartIndex(indexName); 
            Console.Write("the website was indexed at" + DateTime.Now.ToUniversalTime().ToString()); 
            Console.Write("next index is scheduled for" + DateTime.Now.ToUniversalTime().AddDays(1).ToString()); 
            Console.Read(); 
        } 
    } 
}  

 

Using Windows Task Scheduler

  • Open My Computer >> Management >> System Tools >> Task Schedler

 

  •  Click Create New Task

- Triggers tab - set the time you want to run the index

- Actions tab - set the path to the exe generated by the console application.

By Ivan Dimitrov on 22 Jan 2010 in Gotchas All Blog Posts

  • Facebook
  • DZone It!
  • Digg It!
  • StumbleUpon
  • Technorati
  • Del.icio.us
  • NewsVine
  • Reddit
  • Blinklist
  • Furl it!
Add Comment
  1. Formatting options
       
      
     
     
       
  2. Security image

Subscribe to blog feed

Categories

About Telerik

Telerik, the publisher of Sitefinity CMS, is a leading vendor of ASP.NET AJAX, ASP.NET MVC, Silverlight, WinForms and WPF controls and components, as well as .NET Reporting and .NET ORMTFSCode Analysis and Web Application Testing tools. Building on its solid expertise in interface development and Microsoft technologies, Telerik helps customers build applications with unparalleled richness, responsiveness and interactivity. Created with passion, Telerik products help thousands of developers every day to be more productive and deliver reliable applications under budget and on time. Read more about Telerik

Copyright © 2002-2010 Telerik. All rights reserved. Powered by Sitefinity