Forum Discussion
Migrating HTML to Modern Pages - Looks great until page is edited, then content is lost
- Aug 02, 2018
The easiest way to understand what's valid HTML is to create a piece of text using all the layout and formatting options you need. Once you've that you can grab the page list item and look at the canvascontent1 field to obtain the generated HTML. You'll see that only a limited number of styles are supported and fixed set of classes for color and size information. Anything else you use can be initially displayed but will be lost during edit.
Yes, it that is certainly the case.
I am finding that if I change:
<div class="{custom-class">
<table {stuff1}>
{stuff2}
</table>
</div>
to:
<div class="canvasRteResponsiveTable">
<div class="tableWrapper">
<table title="Table" {stuff1}>
{stuff2}
</table>
</div>
</div>
That my tables are now preserved.
I have also found Bert Jansen has created the following which may help: https://github.com/SharePoint/PnP-Tools/blob/master/Solutions/SharePoint.Modernization/SharePointPnP.Modernization.Framework/Transform/HtmlTransformator.cs
I'd really like to know what other edges he has found...
The pnp page transformation engine does handle the transformation from wiki html to modern text editor compliant html. As you've noted you can simply assign html and it will show, but in order to edit the html it must be compliant with what the text editor supports.
See https://github.com/SharePoint/PnP-Tools/blob/master/Solutions/SharePoint.Modernization/SharePointPnP.Modernization.Framework/Transform/HtmlTransformator.cs for a starting basis for your own transformator.
- Kirk LiemohnAug 02, 2018Brass Contributor
Thanks, BertJansen. I was unable to @ mention you before for some reason...
I had found the HtmlTransformator.cs so it's good to know I was moving along the right path. Do you have a list of gotchas that the text editor does not handle. I can infer this from the code, but some of it does not seem to be a problem for me. For example, I don't appear to need a <P> just before an image. The biggest one so far has been <table> elements. That and the fact that a bunch of inline css is just plain stripped which I can't do much about other than find workarounds using the techniques the current editor uses.
You clearly have a lot of knowledge here so I'll take any other information you can provide.
Thanks!
Kirk
- BertJansenAug 02, 2018
Microsoft
The easiest way to understand what's valid HTML is to create a piece of text using all the layout and formatting options you need. Once you've that you can grab the page list item and look at the canvascontent1 field to obtain the generated HTML. You'll see that only a limited number of styles are supported and fixed set of classes for color and size information. Anything else you use can be initially displayed but will be lost during edit.
- faraz77Jan 14, 2024Copper Contributor
BertJansen
It might be too late for you but anyone who's still facing the issue, this is how I solved it.
For images$@" <div class=""imagePlugin"" style=""background-color:transparent;position:relative;"" data-alignment=""Center"" data-imageurl=""<Full Image Url>"" data-uploading=""0"" data-widthpercentage=""100""> <div style=""display: flex; flex-direction: column; position: relative; margin: 0px auto; width: 100%; align-items: center;""> <div class="""" style=""position: relative; outline: none;""> <div class="""" data-automation-id=""imageRead""> <figure tabindex=""0"" role=""button"" class="""" > <div class=""> <div style="" class=""> <img style='width:100%' alt='<alt-text>' data-sp-originalimgsrc=""<Full Image Url>"" src='<Full Image Url>' /> </div> </div> </figure> </div> </div> </div> </div>");
For tables
// using HtmlAgilityPack here // idea again is the same, wrap table in a few other html nodes as SP itself does. var htmlDoc = new HtmlDocument(); ListDictionary tableNodes = new ListDictionary(); if (tables != null) { foreach (var tag in tables) { HtmlNode figureNode = HtmlNode.CreateNode("<figure class=\"table canvasRteResponsiveTable tableLeftAlign\" title=\"Table\">"); var clonedTableNode = tag.Clone(); clonedTableNode.AddClass("ck-table-resized"); figureNode.AppendChild(clonedTableNode); tableNodes.Add(tag.OuterHtml, figureNode.OuterHtml); Console.WriteLine(figureNode.OuterHtml); } } foreach (DictionaryEntry tableNodeDict in tableNodes) { htmlDoc.DocumentNode.InnerHtml = htmlDoc.DocumentNode.InnerHtml.Replace(tableNodeDict.Key.ToString(), tableNodeDict.Value.ToString()); } return htmlDoc.DocumentNode.InnerHtml;