Friday, February 09, 2007

Get media path by file path


This code snippet applies to 5.1.1/5.2.

Here is the code snippet that does the job:

string fullPath = @"D:\wwwroot\Clean5.2.0.12\upload\images\Devil-Skype!.png";

// mapping the path

string mappedPath = Sitecore.Resources.Media.MediaPath.MapPath(fullPath);

// switching the context to shell

Sitecore.Context.SetActiveSite("shell");

// setting the actual media path

string mediaPath = Sitecore.Resources.Media.MediaPath.CreatePath(mappedPath).Paths.FullPath;

// next you may switch back to website

The site switch is necessary since the CreatePath method deals with the content database of the context site which is null for the website by default.

If you don’t want to change the site context, you should add the content database definition to the website:

<site

name="website"

...

content="master" or “web”

...

This way your code will look like:

string fullPath = @"D:\wwwroot\Clean5.2.0.12\upload\images\Devil-Skype!.png";

string mappedPath = Sitecore.Resources.Media.MediaPath.MapPath(fullPath);

string mediaPath = Sitecore.Resources.Media.MediaPath.CreatePath(mappedPath).Paths.FullPath;

Thursday, February 08, 2007

Publish at a specific time


The nature of the Sitecore scheduled operation is cyclic. The task’s execution time relies on the frequency and interval parameters in the web.config file. This approach has significant benefits. Since there is no way to either prevent the ASP.NET process from recycling or predict the recycle time, the cyclic approach makes it easier to guarantee that your scheduled task will be executed in next timeframe despite ASPNET process terminates the task execution. However, sometimes it is necessary to call a task at a specific time. The best example is the publishing task. This module approaches this scenario. The module’s architecture is depicted below: So the module consists of two components: 1. Console Windows application that invokes a web service. 2. Web Service that calls Sitecore publishing operation. Here you can download archive with the documentation and the module itself. Any feedback is really appreciated.