Last active
March 31, 2016 14:24
-
-
Save RyanDurkin/daeb5cfbe0f8a7170e88c9e83c71fe98 to your computer and use it in GitHub Desktop.
DXA Preview DD4T 2013 SP1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public string GetContentByUrl(string Url) | |
{ | |
LoggerService.Debug(">>GetContentByUrl({0})", LoggingCategory.Performance, Url); | |
//Custom DXA Preview code added here | |
string retVal = string.Empty; | |
if ( | |
HttpContext.Current.Request.QueryString.Count > 0 && | |
HttpContext.Current.Request.QueryString["preview"] != null && | |
HttpContext.Current.Request.QueryString["pageUrl"] == Url | |
) | |
{ | |
string pageUri = HttpContext.Current.Request.QueryString["pageUri"]; | |
string templateUri = HttpContext.Current.Request.QueryString["templateUri"]; | |
if (String.IsNullOrEmpty(pageUri) || String.IsNullOrEmpty(templateUri)) | |
{ | |
throw new Exception("If the preview query string parameter is specified, a pageUri, pageUrl and templateUri parameter must also be supplied"); | |
} | |
// Core service code | |
NetTcpBinding binding = new NetTcpBinding("CoreService_netTcpBinding"); | |
string coreServiceAddress = "net.tcp://localhost:2660/CoreService/2013/netTcp"; | |
if (ConfigurationManager.AppSettings["DXAPreviewCoreServiceEndpointAddress"] != null) | |
{ | |
coreServiceAddress = ConfigurationManager.AppSettings["DXAPreviewCoreServiceEndpointAddress"].ToString(); | |
} | |
EndpointAddress address = new EndpointAddress(coreServiceAddress); | |
SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient(binding, address); | |
try | |
{ | |
client.Impersonate(System.Web.HttpContext.Current.User.Identity.Name); | |
RenderInstructionData renderInstruction = new RenderInstructionData(); | |
renderInstruction.RenderMode = RenderMode.PreviewDynamic; | |
PublishInstructionData publishInstruction = new PublishInstructionData(); | |
publishInstruction.RenderInstruction = renderInstruction; | |
ResolveInstructionData resolveInstruction = new ResolveInstructionData(); | |
publishInstruction.ResolveInstruction = resolveInstruction; | |
string publicationTargetId = "tcm:0-1-65537"; | |
if(ConfigurationManager.AppSettings["DXAPreviewPublicationTargetID"] != null) | |
{ | |
publicationTargetId = ConfigurationManager.AppSettings["DXAPreviewPublicationTargetID"].ToString(); | |
} | |
RenderedItemData pagePreview = client.RenderItem(pageUri, templateUri, publishInstruction, publicationTargetId); | |
string pageContent = System.Text.Encoding.Default.GetString(pagePreview.Content); | |
// End of core service code | |
client.Close(); | |
return pageContent; | |
} | |
catch (CommunicationException ex) | |
{ | |
client.Abort(); | |
throw ex; | |
} | |
catch (TimeoutException ex) | |
{ | |
client.Abort(); | |
throw ex; | |
} | |
catch (Exception ex) | |
{ | |
client.Abort(); | |
throw ex; | |
} | |
} | |
else | |
{ | |
LoggerService.Debug("GetContentByUrl: about to create query", LoggingCategory.Performance); | |
using (var pageQuery = new Query()) | |
{ | |
LoggerService.Debug("GetContentByUrl: created query", LoggingCategory.Performance); | |
using (var isPage = new ItemTypeCriteria(64)) | |
{ | |
using (var pageUrl = new PageURLCriteria(Url)) | |
{ | |
using (var allCriteria = CriteriaFactory.And(isPage, pageUrl)) | |
{ | |
if (this.PublicationId != 0) | |
{ | |
using (var correctSite = new PublicationCriteria(this.PublicationId)) | |
{ | |
allCriteria.AddCriteria(correctSite); | |
} | |
} | |
pageQuery.Criteria = allCriteria; | |
} | |
} | |
} | |
LoggerService.Debug("GetContentByUrl: added criteria to query", LoggingCategory.Performance); | |
LoggerService.Debug("GetContentByUrl: about to execute query", LoggingCategory.Performance); | |
string[] resultUris = pageQuery.ExecuteQuery(); | |
pageQuery.Dispose(); | |
LoggerService.Debug("GetContentByUrl: executed query", LoggingCategory.Performance); | |
if (resultUris.Length > 0) | |
{ | |
retVal = PageContentAssembler.GetContent(resultUris[0]); | |
LoggerService.Debug("GetContentByUrl: executed PageContentAssembler", LoggingCategory.Performance); | |
} | |
LoggerService.Debug("<<GetContentByUrl({0})", LoggingCategory.Performance, Url); | |
return retVal; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment