기존 WordPress 데이타를 RSS Feed URL을 통하든지 아니면 MySQL에서 Article에 관련된 모든 정보를 파일 (예, .csv file)로 export하여, 사이트코어 데이타베이스에 Import 하면 된다고 생각할수 있지만, 데이타베이스 구조가 다를 뿐더러, 사이트코어에서 기본사항으로 지원하는 Feed Element가 한정 되어있다.
사이트코어의 Feed element를 추가하기 위하여, 리서치하는 중 @mike_i_reynolds 의 Article (https://sitecorejunkie.com/2014/11/03/add-additional-item-fields-to-rss-feeds-generated-by-sitecore/)을 찾았으며, 포스트된 코드를 응용하여, 다이나믹하게 새로운 필드 element를 추가할수 있도록 코드를 업데이트 하였다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | using System.ServiceModel.Syndication; using Sitecore.Diagnostics; using Sitecore.Configuration; using Sitecore.Data.Items; using Sitecore.Pipelines; using Sitecore.Pipelines.RenderField; using Sitecore.Syndication; using System.Linq; using System.Collections.Generic; using System.Xml.Linq; //// This config file must be added into /App_Include/RssFeedAddtionalFields.config //<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> // <sitecore> // <settings> // <!-- You can list new field names seperated by comma(,) --> // <setting name="RssFeedAddtionalFields.Fields.FieldNames" value="ThumbnailImage,Categories,Tags" /> // <setting name="RssFeedAddtionalFields.Pipelines.RenderField" value="renderField" /> // </settings> // </sitecore> //</configuration> namespace Sitecore.Custom.RSS { public class NewRssFields : PublicFeed { private static List<string> FieldNames { get; set; } private static string RenderFieldPipelineName { get; set; } static NewRssFields() { FieldNames = Settings.GetSetting("RssFeedAddtionalFields.Fields.FieldNames").Split(',').ToList(); RenderFieldPipelineName = Settings.GetSetting("RssFeedAddtionalFields.Pipelines.RenderField"); } protected override SyndicationItem RenderItem(Item item) { SyndicationItem syndicationItem = base.RenderItem(item); AddHtmlToContent(syndicationItem, GetFieldHtmls(item)); return syndicationItem; } protected virtual Dictionary<string, string> GetFieldHtmls(Item item) { if (FieldNames.Count <= 0) { return null; } return GetFieldHtmls(item, FieldNames); } private static Dictionary<string, string> GetFieldHtmls(Item item, List<string> FieldNames) { Assert.ArgumentNotNull(item, "item"); Assert.ArgumentNotNullOrEmpty(RenderFieldPipelineName, "renderField"); Dictionary<string, string> argsResult = new Dictionary<string, string>(); foreach (var fieldName in FieldNames) { if (item == null) { return argsResult; } RenderFieldArgs args = new RenderFieldArgs { Item = item, FieldName = fieldName }; CorePipeline.Run(RenderFieldPipelineName, args); if (!args.Result.IsEmpty) { string contents = args.Result.ToString(); string multilistItems = ""; // Image Field Type - Get only image path if (args.FieldTypeKey == "image") { Sitecore.Data.Fields.ImageField iFld = args.Item.Fields[fieldName]; Sitecore.Resources.Media.MediaUrlOptions opt = new Sitecore.Resources.Media.MediaUrlOptions(); //opt.AlwaysIncludeServerUrl = true; opt.AbsolutePath = true; string mediaUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(iFld.MediaItem, opt); contents = mediaUrl; } // Multilist Field Type - Convert Item ID to Item Name if (args.FieldTypeKey == "multilist") { List<string> itemId = args.Result.ToString().Split('|').ToList(); int count = 1; foreach (string i in itemId) { Item theItem = item.Database.GetItem(Sitecore.Data.ID.Parse(i)); if (theItem != null) { multilistItems += theItem.Name; if (count != itemId.Count) { multilistItems += "|"; } } count++; } contents = multilistItems; } argsResult.Add(fieldName, contents); } } return argsResult; } protected virtual void AddHtmlToContent(SyndicationItem syndicationItem, Dictionary<string, string> getHtml) { if (!(syndicationItem.Content is TextSyndicationContent)) { return; } foreach (KeyValuePair<string, string> pair in getHtml) { syndicationItem.ElementExtensions.Add(new XElement(pair.Key, new XCData(pair.Value)).CreateReader()); } TextSyndicationContent content = syndicationItem.Content as TextSyndicationContent; syndicationItem.Content = new TextSyndicationContent("![CDATA[" + content.Text + "]]", TextSyndicationContentKind.Html); } } } |