Skip to content

Instantly share code, notes, and snippets.

@karlstal
Last active May 17, 2019 08:20
Show Gist options
  • Save karlstal/5b40217d3bec5bfd3ae4f49b30f3677e to your computer and use it in GitHub Desktop.
Save karlstal/5b40217d3bec5bfd3ae4f49b30f3677e to your computer and use it in GitHub Desktop.
Serialize a CMS page
<%@ Page Language="C#" %>
<%@ Import Namespace="EPiServer.Find.Framework" %>
<%@ Import Namespace="EPiServer.Find.Helpers" %>
<%@ Import Namespace="Newtonsoft.Json.Serialization" %>
<%@ Import Namespace="EPiServer" %>
<%@ Import Namespace="EPiServer.ServiceLocation" %>
<%@ Import Namespace="EPiServer.Core" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Serialize a Page</title>
</head>
<body>
<h1>Output:</h1>
<%
// When having a message like @RD234437s72F6: An exception occurred while indexing content [Link 538] [GUID ebf..." you should have contentId=538 in your URL
string contentLink = Request.QueryString["contentId"];
var lang = "en";
// Could be en-GB or any language variant you need
if (!String.IsNullOrEmpty(Request.QueryString["lang"]))
{
lang = Request.QueryString["lang"];
}
// Let's say you have something like this in your model:
// public virtual string Foo => HttpContext.Current.Request.Url.ToString();
// That will render great while testing and for visitors, but not while indexing.
// Therefore, we turn off HttpContext temporarily
var httpContext = HttpContext.Current;
HttpContext.Current = null;
var languageSelector = new LanguageSelector(lang);
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var document = contentLoader.Get<IContent>(new ContentReference(contentLink).ToReferenceWithoutVersion(), languageSelector);
// JsonRequest contains a HttpRequest object, so we need an URL in the constructor. Nothing will be sent, so the URL can be random.
var request = new EPiServer.Find.Connection.JsonRequest("http://example.com/", EPiServer.Find.Connection.HttpVerbs.Post, null);
// Get and customize a serializer to work as if it were a Find indexing.
var serializer = EPiServer.Find.Json.Serializer.CreateDefault();
var conventions = SearchClient.Instance.Conventions;
serializer.ContractResolver = conventions.ContractResolver;
var customizeSerializer = conventions.CustomizeSerializer;
if (customizeSerializer.IsNotNull())
{
customizeSerializer(serializer);
}
// Add a trace writer to the serializer to be able to trace all it does.
ITraceWriter traceWriter = new MemoryTraceWriter();
serializer.TraceWriter = traceWriter;
// Serialize
try
{
EPiServer.Find.Json.Serializer.SerializeToJsonRequest(serializer, request, document);
}
catch(Exception e) {
Response.Write(string.Format("<strong>An exception:</strong> {0}<br /><br />", e.ToString()));
}
finally
{
HttpContext.Current = httpContext;
Response.Write(string.Format("<strong>The trace writer:</strong> {0}<br /><br />", traceWriter.ToString()));
}
%>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment