// note: the dialog option is only because we're being lazy about what we pass along public DynamicResponse GetRenderings([FromUri] DynamicGetRequest request) //[FromUri]DynamicGetRequest request { var ret = new DynamicResponse(request.ItemId.Replace("{", "").Replace("}", "").ToLower(), request.Database); var language = LanguageManager.GetLanguage(request.Language); var db = Database.GetDatabase(request.Database); var item = db.GetItem(request.ItemId, language); ret.AddLayoutType("shared", "Shared"); ret.AddLayoutType("final", "Final"); foreach (var responseLayoutType in ret.LayoutTypes) { ID layoutFieldId = null; switch (responseLayoutType.Name) { case "final": layoutFieldId = Sitecore.FieldIDs.FinalLayoutField; break; case "shared": layoutFieldId = Sitecore.FieldIDs.LayoutField; break; default: layoutFieldId = Sitecore.FieldIDs.LayoutField; // shouldn't be needed, but just in case break; } // yes, there are two things holding the layout type - but that is because this one on the next line is for Response only //var responseLayoutType = ret.AddLayoutType(layoutTypeName); Sitecore.Data.Fields.LayoutField layoutField = item.Fields[layoutFieldId]; var layoutDefinition = Sitecore.Layouts.LayoutDefinition.Parse(item[layoutFieldId]); int i = 0; foreach (var device in Devices) { i++; // this holds a flat dictionary of all renderings so we can place our parent/child relationships correctly var allRenderings = new Dictionary <string, ResponseRendering>(); var allRenderingsReversed = new Dictionary <string, List <ResponseRendering> >(); var responseDevice = responseLayoutType.AddDevice(item, layoutDefinition, device, layoutField.GetLayoutID(device)); //Sitecore.Layouts.DeviceDefinition device = layout.Devices[i] as Sitecore.Layouts.DeviceDefinition; // TODO: issue: we don't do this for the layout also! Sitecore.Layouts.RenderingReference[] renderings = layoutField.GetReferences(device); // if (renderings == null) return; // this item doesn't have any renderings under the Default device. //string layoutXml = layoutField.Value; //responseDevice.Xml = layoutField.Value; if (renderings != null) { // add akk if them as responseRenderings first, in case my parent is after my child! foreach (var rendering in renderings) { var responseRendering = new ResponseRendering(rendering, layoutDefinition, item); // responseDevice.AddRendering(rendering); // TODO: no response needed! allRenderings.Add(responseRendering.UniqueId, responseRendering); // always add it to the flat list! } foreach (var responseRendering in allRenderings.Values) { // add to the proper rendering. Two cases here, depending on which comes first. // TODO: this line only works on Dyanmic - can we check to see if it's dynamic first? // - or rather we need to "try again" to find the used rendering if (!string.IsNullOrEmpty(responseRendering.ParentUniqueId) && allRenderings.ContainsKey(responseRendering.ParentUniqueId)) { allRenderings[responseRendering.ParentUniqueId].AddRendering(item, layoutDefinition, responseRendering); } else if (string.IsNullOrEmpty(responseRendering.ParentUniqueId)) { var possiblePlaceholders = allRenderings.SelectMany(r => r.Value.Placeholders.ToList().Where(p => p.Name == responseRendering.PlaceholderName)).ToList(); if (possiblePlaceholders.Count > 0) { // this means we have the placeholder, but it's missing the ParentUniqueId var effectivePlaceholder = possiblePlaceholders.Last(); // this is where Sitecore will put it anyway, the last match. if (effectivePlaceholder.Dynamic) { responseRendering.InvalidDynamicPlaceholder = true; } if (!effectivePlaceholder.Dynamic && possiblePlaceholders.Count > 1) { effectivePlaceholder.DynamicRecommended = true; } effectivePlaceholder.AddRendering(responseRendering); responseRendering.PossiblePlaceholderPaths.AddRange(possiblePlaceholders.Select(p => p.Name + "-" + p.ParentUniqueId)); } else // we just add it to the device if it's really not found. Due to rendering order OR it's really invalid { responseDevice.AddRendering(item, layoutDefinition, responseRendering); } } else if (allRenderingsReversed.ContainsKey(responseRendering.UniqueId)) // I am not sure what this was even for? It will never come here now! //responseRendering.RemoveRenderingRange(allRenderingsReversed); { responseRendering.AddRenderingRange(item, layoutDefinition, allRenderingsReversed[responseRendering.UniqueId]); // could have collected a bunch by now } // TODO: but I need to remove it when this happens. So don't add the ones with parents, duh // - they DEFINITELY don't go in the maincontent then lol (at least, not right now) if (!string.IsNullOrEmpty(responseRendering.ParentUniqueId)) { if (!allRenderingsReversed.ContainsKey(responseRendering.ParentUniqueId)) { allRenderingsReversed.Add(responseRendering.ParentUniqueId, new List <ResponseRendering>()); } allRenderingsReversed[responseRendering.ParentUniqueId].Add(responseRendering); } } // TODO: loop through and "arange" } } } return(ret); //HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK) //{ // Content = new StringContent("") //}; //result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml"); //application/octet-stream //return result; }
public DynamicResponse GetRenderings(string id, string language, int version, string database) //[FromUri]DynamicGetRequest request { var ret = new DynamicResponse(id.Replace("{", "").Replace("}", "").ToLower(), database); var db = Sitecore.Data.Database.GetDatabase(database); var item = db.GetItem(id); ret.AddLayoutType("shared", "Shared"); ret.AddLayoutType("final", "Final"); foreach (var responseLayoutType in ret.LayoutTypes) { ID layoutFieldId = null; switch (responseLayoutType.Name) { case "final": layoutFieldId = Sitecore.FieldIDs.FinalLayoutField; break; case "shared": layoutFieldId = Sitecore.FieldIDs.LayoutField; break; default: layoutFieldId = Sitecore.FieldIDs.LayoutField; // shouldn't be needed, but just in case break; } // yes, there are two things holding the layout type - but that is because this one on the next line is for Response only //var responseLayoutType = ret.AddLayoutType(layoutTypeName); Sitecore.Data.Fields.LayoutField layoutField = item.Fields[layoutFieldId]; var layoutDefinition = Sitecore.Layouts.LayoutDefinition.Parse(item[layoutFieldId]); int i = 0; foreach (var device in devices) { i++; // this holds a flat dictionary of all renderings so we can place our parent/child relationships correctly var allRenderings = new Dictionary <string, ResponseRendering>(); var allRenderingsReversed = new Dictionary <string, List <ResponseRendering> >(); var responseDevice = responseLayoutType.AddDevice(item, layoutDefinition, device, layoutField.GetLayoutID(device)); //Sitecore.Layouts.DeviceDefinition device = layout.Devices[i] as Sitecore.Layouts.DeviceDefinition; Sitecore.Layouts.RenderingReference[] renderings = layoutField.GetReferences(device); // if (renderings == null) return; // this item doesn't have any renderings under the Default device. //string layoutXml = layoutField.Value; //responseDevice.Xml = layoutField.Value; if (renderings != null) { // TODO: NEST! that's the WHOLE THING! foreach (var rendering in renderings) { var responseRendering = new ResponseRendering(rendering, layoutDefinition, item); // responseDevice.AddRendering(rendering); // TODO: no response needed! // add to the proper rendering. Two cases here, depending on which comes first. if (!string.IsNullOrEmpty(responseRendering.ParentUniqueId) && allRenderings.ContainsKey(responseRendering.ParentUniqueId)) { allRenderings[responseRendering.ParentUniqueId].AddRendering(item, layoutDefinition, responseRendering); } else if (allRenderingsReversed.ContainsKey(responseRendering.UniqueId)) { //responseRendering.RemoveRenderingRange(allRenderingsReversed); responseRendering.AddRenderingRange(item, layoutDefinition, allRenderingsReversed[responseRendering.UniqueId]); // could have collected a bunch by now } if (string.IsNullOrEmpty(responseRendering.ParentUniqueId)) // this means the path wasn't built with a GUID, so I couldn't find it. TODO: Fix it anyway! { responseDevice.AddRendering(item, layoutDefinition, responseRendering); } // TODO: but I need to remove it when this happens. So don't add the ones with parents, duh // - they DEFINITELY don't go in the maincontent then lol (at least, not right now) allRenderings.Add(responseRendering.UniqueId, responseRendering); // always add it to the flat list! if (!string.IsNullOrEmpty(responseRendering.ParentUniqueId)) { if (!allRenderingsReversed.ContainsKey(responseRendering.ParentUniqueId)) { allRenderingsReversed.Add(responseRendering.ParentUniqueId, new List <ResponseRendering>()); } allRenderingsReversed[responseRendering.ParentUniqueId].Add(responseRendering); } } // TODO: loop through and "arange" } } } return(ret); //HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK) //{ // Content = new StringContent("") //}; //result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml"); //application/octet-stream //return result; }