views:

134

answers:

4

Hello,

I am trying to publish programatically in Sitecore. Publishing works fine. But doing so programatically doesn't clear the sitecore cache. What is the best way to clear the cache programatically?

I am trying to use the webservice that comes with the staging module. But I am getting a Bad request exception(Exception: The remote server returned an unexpected response: (400) Bad Request.). I tried to increase the service receivetimeout and sendtimeout on the client side config file but that didn't fix the problem. Any pointers would be greatly appreciated?

I am using the following code:

CacheClearService.StagingWebServiceSoapClient client = new CacheClearService.StagingWebServiceSoapClient();
CacheClearService.StagingCredentials credentials = new CacheClearService.StagingCredentials();

credentials.Username = "sitecore\adminuser";
credentials.Password = "***********";
credentials.isEncrypted = false;

bool s = client.ClearCache(true, dt, credentials);

I am using following code to do publish.

 Database master = Sitecore.Configuration.Factory.GetDatabase("master");
 Database web = Sitecore.Configuration.Factory.GetDatabase("web");

 string userName = "default\adminuser";

 Sitecore.Security.Accounts.User user = Sitecore.Security.Accounts.User.FromName(userName, true);

 user.RuntimeSettings.IsAdministrator = true;

 using (new Sitecore.Security.Accounts.UserSwitcher(user))
 {

     Sitecore.Publishing.PublishOptions options = new Sitecore.Publishing.PublishOptions(master, web,
     Sitecore.Publishing.PublishMode.Full, Sitecore.Data.Managers.LanguageManager.DefaultLanguage, DateTime.Now);

     options.RootItem = master.Items["/sitecore/content/"];
     options.Deep = true;
     options.CompareRevisions = true;
     options.RepublishAll = true;
     options.FromDate = DateTime.Now.AddMonths(-1);

     Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(options);
     publisher.Publish();
  }

Murugesh.

A: 

Via the SDN:

HtmlCache cache = CacheManager.ClearAll()

if (cache != null) {
  cache.Clear();
}
Mark Ursino
A: 

In Sitecore 6, the CacheManager class has a static method that will clear all caches. The ClearAll() method is obsolete.

Sitecore.Caching.CacheManager.ClearAllCaches();
CodeToaster
A: 

Just a quick note, in Sitecore 6.3, that is not needed anymore. Caches are being cleared automatically after a change happens on a remote server. Also, if you are on previous releases, instead of clearing all caches, you can do partial cache clearing. There is a free shared source component called Stager that does that. http://trac.sitecore.net/SitecoreStager If you need a custom solution, you can simply extract the source code from there.

Alex Shyba
A: 

I got this from Sitecore support. It clears all caches:

Sitecore.Context.Database = this.WebContext.Database;
Sitecore.Context.Database.Engines.TemplateEngine.Reset();
Sitecore.Context.ClientData.RemoveAll();
Sitecore.Caching.CacheManager.ClearAllCaches();
Sitecore.Context.Database = this.ShellContext.Database;
Sitecore.Context.Database.Engines.TemplateEngine.Reset();
Sitecore.Caching.CacheManager.ClearAllCaches();
Sitecore.Context.ClientData.RemoveAll();
marto