Place the script below in the root of your site. Very Handy.
<script runat="server">
public string Database
{
get
{
return string.IsNullOrEmpty(Request["dbname"]) ? "web" : Request["dbname"];
}
}
protected void Page_Load(object sender, EventArgs e)
{
var database = Sitecore.Configuration.Factory.GetDatabase(Database);
if (Request.QueryString["index"] == "true")
RebuildIndexes(database);
if (Request.QueryString["link"] == "true")
RebuildLinksDatabase(database);
}
protected void RebuildIndexes(Sitecore.Data.Database database)
{
if (database != null)
{
DateTime start = DateTime.Now;
for (int i = 0; i < database.Indexes.Count; i++)
{
WriteToResponse(String.Format("Rebuilding {0} index", database.Indexes[i].Name));
try {
database.Indexes[i].Rebuild(database);
}
catch(Exception ex)
{
WriteToResponse("Error while rebuilding index: " + ex.Message);
}
WriteToResponse(String.Format("Index {0} rebuilt", database.Indexes[i].Name));
}
TimeSpan elapsed = DateTime.Now - start;
WriteToResponse("Indexes rebuilt in " + elapsed.TotalMilliseconds + " ms");
}
}
protected void RebuildLinksDatabase(Sitecore.Data.Database database)
{
WriteToResponse("Rebuilding Links database");
DateTime start = DateTime.Now;
Sitecore.Globals.LinkDatabase.Rebuild(database);
TimeSpan elapsed = DateTime.Now - start;
WriteToResponse("Links database rebuilt in " + elapsed.TotalMilliseconds + " ms");
}
protected void WriteToResponse(string str)
{
Response.Write(str + "<br/>");
Response.Flush();
}
</script>
No comments:
Post a Comment