+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Implementing scheduled services in Sitefinity 3.x

by Ivan Dimitrov
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.

3 comments

Leave a comment
  1. Tony Montata Jun 16, 2010
    Very helpful post! Thank you!
  2. dissanpointed Dec 01, 2010
    Dont like it. I'm little bit dissapointed, that there's no automatic indexing option. As a CMS we expect to have this fixed or supported out of the box. CommunityServer has that from the start. It's annoyning to write everything (not just indexing) custom. Why use sitefinity then?
  3. sathi Dec 08, 2010
    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
    Dog Boots

    Leave a comment