public override void Process(InsertRenderingsArgs args) { //Get the merged item layout field value Field layoutField = args.ContextItem.Fields[FieldIDs.LayoutField]; if (layoutField == null) { return; } string fieldValue = LayoutField.GetFieldValue(layoutField); if (string.IsNullOrEmpty(fieldValue)) { return; } //Parse it for easier processing XDocument layout = XDocument.Parse(fieldValue); XmlBasedRenderingParser parser = new XmlBasedRenderingParser(); //Build a shared page context Sitecore.Mvc.Presentation.PageContext pageCtx = new Sitecore.Mvc.Presentation.PageContext(); HttpContextWrapper httpCtxWrapper = new HttpContextWrapper(HttpContext.Current); RouteData routeData = CreateRouteData(); pageCtx.RequestContext = CreateRequestContext(httpCtxWrapper, routeData); string deviceId = Sitecore.Context.Device.ID.ToString(); //Loop through the renderings foreach (RenderingReference renderingReference in args.Renderings) { string templateName = renderingReference.RenderingItem.InnerItem.TemplateName; //Only do the replacement for MVC renderings if (templateName == "View rendering" || templateName == "Controller rendering" || templateName == "Item rendering") { XElement renderingXml = layout.XPathSelectElement("/r/d[@id='" + deviceId + "']/r[@id='" + renderingReference.RenderingID + "']"); Rendering rendering = parser.Parse(renderingXml, false); renderingReference.SetControl(new RenderingWrapper(rendering, pageCtx)); } } }
/// <summary> /// Gets the rendering out of the xml node and injects some values in /// </summary> /// <param name="renderingNode"></param> /// <param name="deviceId"></param> /// <param name="layoutId"></param> /// <param name="renderingType"></param> /// <param name="parser"></param> /// <returns>MVC rendering</returns> protected virtual Rendering GetRendering(XElement renderingNode, Guid deviceId, Guid layoutId, string renderingType, XmlBasedRenderingParser parser) { Rendering rendering = parser.Parse(renderingNode, false); rendering.DeviceId = deviceId; rendering.LayoutId = layoutId; if (renderingType != null) { rendering.RenderingType = renderingType; } // if the xBlock is rendering in the context of another page, renderings with no data source should be repointed to the xBlock page item // as opposed to the context page item if (string.IsNullOrWhiteSpace(rendering.DataSource)) { rendering.DataSource = Item.ID.ToString(); } return(rendering); }