Skip to content

Improved image supportΒ #1667

Open

Description

@jansenbe

Improved support for setting image properties by supporting all the available options in

public async Task<string> GetInlineImageAsync(IPageText textEditorInstance, string serverRelativeUrl, PageImageOptions imageOptions = null)
{
if (EditorType == EditorType.CK4 && textEditorInstance == null)
{
throw new ArgumentNullException(nameof(textEditorInstance));
}
if (string.IsNullOrEmpty(serverRelativeUrl))
{
throw new ArgumentNullException(nameof(serverRelativeUrl));
}
if (imageOptions == null)
{
imageOptions = new PageImageOptions
{
IsInlineImage = true,
};
}
else
{
imageOptions.IsInlineImage = true;
}
if (EditorType == EditorType.CK4)
{
var inlineImageWebPart = await GetImageWebPartAsync(serverRelativeUrl, imageOptions).ConfigureAwait(false);
(inlineImageWebPart as PageWebPart).RichTextEditorInstanceId = textEditorInstance.InstanceId.ToString();
// Add the image web part to collection of inline web parts
(textEditorInstance as PageText).InlineWebParts.Add(inlineImageWebPart as PageWebPart);
// Prepare the text snippet to insert
string alignmentValue = "webPartInRteAlignLeft";
if (imageOptions != null)
{
if (imageOptions.Alignment == PageImageAlignment.Center)
{
alignmentValue = "webPartInRteAlignCenter";
}
else if (imageOptions.Alignment == PageImageAlignment.Right)
{
alignmentValue = "webPartInRteAlignRight";
}
}
return inlineImageCK4Html.Replace(inlineImageTextControl, inlineImageWebPart.InstanceId.ToString())
.Replace(inlineImageAlignment, alignmentValue);
}
else
{
// Find the server relative image
var image = await PnPContext.Web.GetFileByServerRelativeUrlAsync(serverRelativeUrl, p => p.UniqueId, p => p.ListId).ConfigureAwait(false);
// Set defaults in case height and width are not set
if (!imageOptions.Height.HasValue)
{
// Assume by default 0, aligned with the SPO behavior
imageOptions.Height = 0;
}
if (!imageOptions.Width.HasValue)
{
// Assume by default 0, aligned with the SPO behavior
imageOptions.Width = 0;
}
if (!imageOptions.WidthPercentage.HasValue)
{
// Assume by default 100% of the editor width, aligned with the SPO behavior
imageOptions.WidthPercentage = 100;
}
imageOptions.Link ??= "";
imageOptions.Caption ??= "";
imageOptions.AlternativeText ??= "";
return inlineImageCK5Html.Replace(inlineImageUrl, serverRelativeUrl)
.Replace(inlineImageAlignment, imageOptions.Alignment.ToString())
.Replace(inlineImageCaption, imageOptions.Caption)
.Replace(inlineImageAlternativeText, imageOptions.AlternativeText)
.Replace(inlineImageLinkUrl, imageOptions.Link)
.Replace(inlineImageWidth, imageOptions.Width.Value.ToString())
.Replace(inlineImageHeight, imageOptions.Height.Value.ToString())
.Replace(inlineWidthPercentage, imageOptions.WidthPercentage.Value.ToString())
.Replace(inlineWebId, PnPContext.Web.Id.ToString())
.Replace(inlineSiteId, PnPContext.Site.Id.ToString())
.Replace(inlineListId, image.ListId.ToString())
.Replace(inlineUniqueId, image.UniqueId.ToString());
}
}

Image

Activity

  1. added theissue type on Jul 15, 2026
  2. added
    help wanted
    on Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Candidate for fixingP1area: pages API πŸ“„help wanted

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions