How to retrieve Sitecore media item from the URL we have?
The URL is dynamic URL e.g. /~/media/14BDED00E4D64DFD8F74019AED4D74EB.ashx
.
This is generated when you add item in rich text field.
How to retrieve Sitecore media item from the URL we have?
The URL is dynamic URL e.g. /~/media/14BDED00E4D64DFD8F74019AED4D74EB.ashx
.
This is generated when you add item in rich text field.
You can use the code below:
DynamicLink dynamicLink;
if (!DynamicLink.TryParse("/~/media/14BDED00E4D64DFD8F74019AED4D74EB.ashx", out dynamicLink))
return;
MediaItem mediaItem = Sitecore.Context.Database.GetItem(dynamicLink.ItemId, dynamicLink.Language ?? Sitecore.Context.Language);
When adding an item in the Rich Text field, you can use the FieldRenderer to render out the output - Sitecore will then create the correct URL automatically. That way you won't even have to worry about the URL.
The FieldRenderer control can be used like so:
<sc:FieldRenderer ID="renderer" runat="server" FieldName="fieldname" />
Or if you're using XSLT:
<sc:text field="fieldname" />
In codebehind you could do something like
FieldRenderer.Render(Sitecore.Context.Item, fieldname);
© 2022 - 2024 — McMap. All rights reserved.