Last active
August 29, 2015 14:20
-
-
Save komainu85/180c706cf515cc67a550 to your computer and use it in GitHub Desktop.
Sitecore Robots.txt generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> | |
<sitecore> | |
<pipelines> | |
<publishItem> | |
<processor type="MikeRobbins.CMS.Robots.Robots,MikeRobbins.CMS" patch:after="processor[@type='Sitecore.Publishing.Pipelines.PublishItem.MoveItems, Sitecore.Kernel']" > | |
<FileName>robots.txt</FileName> | |
<ConfigTemplateName>Robots txt configuration</ConfigTemplateName> | |
<WebApplicationRoot>C:\inetpub\wwwroot\MikeRobbins8u2\Website</WebApplicationRoot> | |
</processor> | |
</publishItem> | |
</pipelines> | |
</sitecore> | |
</configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Robots : PublishItemProcessor | |
{ | |
public string FileName { get; set; } | |
public string ConfigTemplateName { get; set; } | |
public string WebApplicationRoot { get; set; } | |
public override void Process(PublishItemContext context) | |
{ | |
var target = context.PublishOptions.SourceDatabase.GetItem(context.ItemId, context.PublishOptions.Language); | |
if (target.Name != "__Standard Values" && target.TemplateName == ConfigTemplateName) | |
{ | |
var contents = GetTextFileData(target); | |
WriteFile(contents); | |
} | |
} | |
public string GetTextFileData(Item item) | |
{ | |
var fileContents = ""; | |
fileContents = item["Header elements"]; | |
fileContents = fileContents + Environment.NewLine; | |
fileContents = fileContents + item["Urls"]; | |
return fileContents; | |
} | |
public void WriteFile(string contents) | |
{ | |
try | |
{ | |
var tw = new StreamWriter(WebApplicationRoot + "\" + FileName); | |
tw.Write(contents); | |
tw.Close(); | |
} | |
catch (IOException ex) | |
{ | |
Sitecore.Diagnostics.Log.Error(ex.Message, ex, this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment