5

I am using a scheduled job to import some content into Sitecore. Hereby I am inserting inline images into a rich text editor field.

I determine the src of the img tags by using:

string url = StringUtil.EnsurePrefix('/', MediaManager.GetMediaUrl(mediaItem));

This generates urls like: /sitecore/shell/-/media/a/b/images/x.png. But these urls don't work at the frontend. How can I generate urls that will work?

5

From what I understand, your plan is to create a code in your schedule job which will insert image into a Rich Text field. In that case you don't want to insert friendly url there, you want to insert a link that Sitecore will understand.

For media items that kind of link has format:

src="-/media/0907324419c54abfa00c6a05e19793dc.ashx"

You can check this by inserting an image into a RTE field and switching to HTML view.

If you want to generate link like that, use code below:

MediaUrlOptions shellOptions = MediaUrlOptions.GetShellOptions();
string mediaUrl = MediaManager.GetMediaUrl(mediaItem, shellOptions);

Thanks to that, if you will try to delete the image from media library, Sitecore will give you a warning that the image is linked from another item. And if you move or rename the image, the link will still work.

And similarly, if you plan to insert any item link inside a RTE field, use the following code:

LinkUrlOptions options = new LinkUrlOptions();
string itemUrl = LinkManager.GetDynamicUrl(selectionItem, options);

It will generate link in the DynamicLink format:

˜/link.aspx?_id=0B16893D260041838FAD668AAA551E21&_z=z

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.