4

We have email templates that are stored in Sitecore. We are using Richtext field to store html of emails. When I add images from Sitecore media library to that rich text field links are generating as

  <img src="-/media/Id.ashx?la=en-gb" alt="email-header" width="600" height="149">

because of this, gmail rendering the image wrong as: http://-/media/Id.ashx?la=en-gb

We can search and replace the html while we sending emails, but is there is a way to tell Sitecore to use full image url's for rich text field?

1 Answer 1

6

From what I remember, the logic responsible for RTE links is in ExpandLinks processor of renderField pipeline:

<processor type="Sitecore.Pipelines.RenderField.ExpandLinks, Sitecore.Kernel"/>
public virtual void Process(RenderFieldArgs args)
{
  Assert.ArgumentNotNull((object) args, "args");
  if (Context.PageMode.IsExperienceEditorEditing)
    return;
  args.Result.FirstPart = DynamicLink.ExpandLinks(args.Result.FirstPart, Settings.Rendering.SiteResolving);
  args.Result.LastPart = DynamicLink.ExpandLinks(args.Result.LastPart, Settings.Rendering.SiteResolving);
}

You could write your own logic here to make sure that links include host name as well.

You can also update LinkProvider method ExpandDynamicLinks to force it to include host names:

public virtual string ExpandDynamicLinks(string text, bool resolveSites)
{
  Assert.ArgumentNotNull((object) text, "text");
  LinkProvider.LinkParser linkParser = this.CreateLinkParser();
  UrlOptions defaultUrlOptions = this.GetDefaultUrlOptions();
  defaultUrlOptions.SiteResolving = resolveSites;

  defaultUrlOptions.AlwaysIncludeServerUrl = true; // <---

  return linkParser.ExpandDynamicLinks(text, defaultUrlOptions);
}

EDIT

If you want Sitecore to expand those links for you in custom code, you can run something like that:

protected virtual string RunPipeline(Field field)
{
    using (new SiteContextSwitcher(SiteContext.GetSite("website")))
    {
        RenderFieldArgs renderFieldArgs = new RenderFieldArgs()
        {
            Item = field.Item,
            FieldName = field.Name,
            DisableWebEdit = true
        };
        CorePipeline.Run("renderField", (PipelineArgs) renderFieldArgs);
        return renderFieldArgs.Result.FirstPart + renderFieldArgs.Result.LastPart;
    }
}

string bodyOfEmail = RunPipeline(item.Fields[FieldIds.EmailBodyField]);
9
  • Make sense, this might work when i render the RTE field on the page. Commented Jan 26, 2017 at 15:32
  • What i am after is html code on RTE field, i want full url's in there, is it possible to do that? Commented Jan 26, 2017 at 15:34
  • It's called while rendering any field on a page. Please be aware that it has if condition which checks Context.PageMode.IsExperienceEditorEditing mode and that you have to update or patch configs to replace original ExpandLinks with your class.
    – Marek Musielak
    Commented Jan 26, 2017 at 15:34
  • @DheerajPalagiri how do you send content of the field to the external mail system?
    – Marek Musielak
    Commented Jan 26, 2017 at 15:35
  • 1
    I've added code to the answer describing how to tell Sitecore to expand the value of the field in custom c# code. Not sure if this is exactly what you need but should point you in the right direction. I don't think you can store expanded values in the fields as that would break all the functionality for tracking links (e.g. for broken links) and multisite links.
    – Marek Musielak
    Commented Jan 26, 2017 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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