Refresh Coveo source from a Sitecore page

Posted 08/18/2014 by Debabrata Biswas

There are cases where you would need to refresh a coveo source (web crawler, Sitecore or any other source) programmatically. This is especially true for "Web Crawler" sources where coveo does not support "Live Indexing" like it does for say a "sitecore" source. In this case you can utilize coveo api code to rebuild/refresh a source.

Our requirement was to call this on a web crawler source. Our source pages code-behind was calling a custom webservice to fetch data and render them. As you might be knowing we can always define coveo fields and fill them through usage of same named meta-tags from code-behind. We had to periodically call these web services and update the indexed data in coveo. As you know coveo does not allow "live indexing" for web crawler sources.

Following is the coveo code that you can call to index a source.

Coveo.CES.Admin.CesAdmin admin = new C.CES.Admin.CesAdmin();
admin.Connect("localhost", "Default", false, "", "", ""); //specify the server with coveo instance
Coveo.CES.Admin.CesCollection col = admin.PhysicalIndex.Collections.ItemByName(collection);
Coveo.CES.Admin.CesSource src = col.Sources.ItemByName([SOURCE]);
string uri = [SOURCE uri as set in Coveo source "Addresses" area]
src.RefreshURI(uri, true, true, true);

The above code was in an aspx page and I wrote a console application that called this web page. This console application was configured using windows task scheduler to execure at specified times. The code for the console application was a simple one as below:

        static void Main(string[] args)
            string sURL=null;
            WebRequest wrGETURL = null; ;

   wrGETURL = WebRequest.Create(sURL);
   wrGETURL.Timeout = 3600 * 60;

            wrGETURL.Proxy = WebProxy.GetDefaultProxy();

            Stream objStream;
            objStream = wrGETURL.GetResponse().GetResponseStream();