How to extract Sitecore media elements from media URLs?

How do I extract a Sitecore media item from the URL that we have?

A URL is a dynamic URL, for example. /~/media/14BDED00E4D64DFD8F74019AED4D74EB.ashx .

This is generated when an item is added to the rich text field.

+4
source share
2 answers

You can use the following code:

 DynamicLink dynamicLink; if (!DynamicLink.TryParse("/~/media/14BDED00E4D64DFD8F74019AED4D74EB.ashx", out dynamicLink)) return; MediaItem mediaItem = Sitecore.Context.Database.GetItem(dynamicLink.ItemId, dynamicLink.Language ?? Sitecore.Context.Language); 
+6
source

When you add an item to the Rich Text field, you can use FieldRenderer to render the output - Sitecore will automatically create the correct URL. This way you don’t even have to worry about the URL.

The FieldRenderer control can be used as follows:

 <sc:FieldRenderer ID="renderer" runat="server" FieldName="fieldname" /> 

Or if you are using XSLT:

 <sc:text field="fieldname" /> 

In codebehind you can do something like

 FieldRenderer.Render(Sitecore.Context.Item, fieldname); 
+3
source

Source: https://habr.com/ru/post/1488225/


All Articles