/// <summary>Get descendants of parent document. Expensive operation using CMS database.</summary>
 public static IList<Document> GetDescendants(Document cmsParent)
 {
     return cmsParent.GetDescendants().Cast<Document>().ToList();
 }
        /// <summary>Get descendants of parent document. Optionally, include self. Expensive operation using CMS database.</summary>
        public static IList<Document> GetDescendants(Document cmsParent, string typeAlias, bool allSameLevel = false, bool includeSelf = false)
        {
            IList<Document> returnItems = new List<Document>();

            if(cmsParent != null) {
            if(includeSelf && String.Equals(cmsParent.ContentType.Alias, typeAlias)) {
                returnItems.Add(cmsParent);
            }

            int currentLevel = -1;
            var cmsItems = cmsParent.GetDescendants().Cast<Document>().ToList();
            var cmsItemsCount = cmsItems.Count;

            for(int i = 0;i < cmsItemsCount;i += 1) {
                if(String.Equals(cmsItems[i].ContentType.Alias, typeAlias)) {
                    if(allSameLevel && i == 0) {
                        currentLevel = cmsItems[i].Level;
                    }
                    if(allSameLevel && currentLevel == cmsItems[i].Level) {
                        returnItems.Add(cmsItems[i]);
                    } else if(currentLevel == -1) {
                        returnItems.Add(cmsItems[i]);
                    }
                }
            }
            }
            return returnItems;
        }
Exemple #3
0
        private void UpdateMediaImages(Document root)
        {
            Dictionary<int, int> imageDictionary;
            var path = HttpContext.Current.Server.MapPath(string.Concat(BootstrapPath, "media.json"));
            if (File.Exists(path))
            {
                imageDictionary = File.ReadAllText(path).JsonToDictionary<Dictionary<int, int>>();
            }
            else
            {
                return;
            }

            int imageId;
            if (root.getProperty("bodyImage") != null && root.getProperty("bodyImage").Value is int)
            {
                if (imageDictionary.TryGetValue((int)root.getProperty("bodyImage").Value, out imageId))
                {
                    root.getProperty("bodyImage").Value = imageId;
                }
            }

            foreach (Document doc in root.GetDescendants())
            {
                if (doc.getProperty("bodyImage") != null && doc.getProperty("bodyImage").Value is int)
                {
                    if (imageDictionary.TryGetValue((int)doc.getProperty("bodyImage").Value, out imageId))
                    {
                        doc.getProperty("bodyImage").Value = imageId;
                    }
                }

                if (doc.getProperty("galleryThumbnail") != null && doc.getProperty("galleryThumbnail").Value is int)
                {
                    if (imageDictionary.TryGetValue((int)doc.getProperty("galleryThumbnail").Value, out imageId))
                    {
                        doc.getProperty("galleryThumbnail").Value = imageId;
                    }
                }
            }
        }