Created
January 29, 2018 02:04
-
-
Save i-sync/8a084bd8e15c4bc55d957406c278fa83 to your computer and use it in GitHub Desktop.
Sitecore Svg Render
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 static string SvgImage(Item item, string fieldName, string parameters = "") | |
| { | |
| if (item == null || string.IsNullOrEmpty(fieldName)) | |
| return string.Empty; | |
| ImageField field = item.Fields[fieldName]; | |
| if (field?.MediaItem == null) | |
| return $"<!--This field name :{fieldName} is not image field.-->"; | |
| var mediaItem = new MediaItem(field.MediaItem); | |
| if (mediaItem.MimeType != "image/svg+xml" || Sitecore.Context.PageMode.IsExperienceEditor) | |
| return Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, fieldName, parameters); | |
| string result = string.Empty; | |
| using (var reader = new StreamReader(MediaManager.GetMedia(mediaItem).GetStream().Stream)) | |
| { | |
| result = reader.ReadToEnd(); | |
| } | |
| if (string.IsNullOrWhiteSpace(result)) | |
| return Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, fieldName, parameters); | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment