Esempio n. 1
0
        protected virtual void WriteItems([NotNull] JsonContentResultWriter output, [NotNull] RequestParameters requestParameters, IEnumerable <Item> items)
        {
            items = FilterEnumerable(requestParameters, items);

            var count = items.Count();

            if (requestParameters.Skip > 0)
            {
                items = items.Skip(requestParameters.Skip);
            }

            if (requestParameters.Take > 0)
            {
                items = items.Take(requestParameters.Take);
            }

            WriteMetaData(output);

            output.WritePropertyString("count", count);
            output.WritePropertyString("skip", requestParameters.Skip);
            output.WritePropertyString("take", requestParameters.Take);

            output.WriteStartArray("items");

            foreach (var child in items)
            {
                output.WriteStartObject();
                WriteItemHeader(output, child, requestParameters.Flatten == 0);
                WriteItemFields(output, requestParameters, child);
                WriteItemChildren(output, requestParameters, child, requestParameters.Children);
                output.WriteEndObject();
            }

            output.WriteEndArray();
        }
Esempio n. 2
0
        public override ActionResult GetTemplate(RequestParameters requestParameters, string templateName)
        {
            var templates = GetTemplatesByName(templateName).ToList();

            if (!templates.Any())
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Template not found"));
            }

            if (templates.Count > 1)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Ambiguous, "Ambiguous template name"));
            }

            var templateItem = templates.First();

            if (templateItem == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Template not found"));
            }

            if (templateItem.TemplateId != TemplateIDs.Template.Guid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Item found, but it is not a template"));
            }

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            WriteItemHeader(output, templateItem);

            output.WriteStartArray("fields");

            foreach (var field in templateItem.Children.SelectMany(section => section.Children))
            {
                var isSystemField = field.Name.StartsWith("__");

                if (isSystemField && !requestParameters.IncludeSystemFields)
                {
                    continue;
                }

                output.WriteStartObject();
                output.WritePropertyString("id", field.Id.ToString("B").ToUpperInvariant());
                output.WritePropertyString("uri", DatabaseName + "/" + templateItem.Id + "/" + field.Id.ToString("B").ToUpperInvariant());
                output.WritePropertyString("name", field.Name);
                output.WritePropertyString("displayName", field.DisplayName);
                output.WritePropertyString("type", field["Type"]);
                output.WritePropertyString("source", field["Source"]);
                output.WritePropertyString("sharing", field["Shared"] == "1" ? "shared" : field["Unversioned"] == "1" ? "unversioned" : "versioned");
                output.WritePropertyString("section", field.Parent?.Name ?? string.Empty);
                output.WritePropertyString("kind", "own");
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
Esempio n. 3
0
        public override ActionResult GetItems(RequestParameters requestParameters)
        {
            SetContext(requestParameters);

            List <ID>          result;
            IEnumerable <Item> items;
            var index = ContentSearchManager.GetIndex("sitecore_" + Database.Name.ToLowerInvariant() + "_index");

            using (var context = index.CreateSearchContext())
            {
                var queryable = FilterQueryable(context.GetQueryable <SearchResultItem>(), requestParameters);

                result = queryable.Where(i => i.Name != "$name" && i.Name != "__Standard Values").Select(item => item.ItemId).ToList().Distinct().ToList();

                items = result.Select(Database.GetItem).Where(i => i != null).OrderBy(t => t.Name).ThenBy(t => t.Paths.Path);

                if (requestParameters.Skip > 0)
                {
                    items = items.Skip(requestParameters.Skip);
                }

                if (requestParameters.Take > 0)
                {
                    items = items.Take(requestParameters.Take);
                }

                items = items.ToList();
            }

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("count", result.Count);
            output.WritePropertyString("skip", requestParameters.Skip);
            output.WritePropertyString("take", requestParameters.Take);

            output.WriteStartArray("items");

            foreach (var item in items)
            {
                output.WriteStartObject();
                WriteItemHeader(output, item);
                WriteItemFields(output, requestParameters, item);
                WriteItemChildren(output, requestParameters, item, requestParameters.Children);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
Esempio n. 4
0
        public override ActionResult GetItems(RequestParameters requestParameters)
        {
            var result = Filter(Items.AsQueryable(), requestParameters).Where(i => i.Name != "$name" && i.Name != "__Standard Values").ToList();
            var items  = result.OrderBy(t => t.Name).ThenBy(i => i.Path) as IEnumerable <FileDatabaseItem>;

            if (requestParameters.Skip > 0)
            {
                items = items.Skip(requestParameters.Skip);
            }

            if (requestParameters.Take > 0)
            {
                items = items.Take(requestParameters.Take);
            }

            items = items.ToList();

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("count", result.Count);
            output.WritePropertyString("skip", requestParameters.Skip);
            output.WritePropertyString("take", requestParameters.Take);

            output.WriteStartArray("items");

            foreach (var item in items)
            {
                output.WriteStartObject();
                WriteItemHeader(output, item);
                WriteItemFields(output, requestParameters, item);
                WriteItemChildren(output, requestParameters, item, requestParameters.Children);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
        public virtual ActionResult GetDatabases()
        {
            var output = new JsonContentResultWriter(new StringWriter());

            output.WriteStartObject("metadata");
            output.WritePropertyString("version", Constants.Version);
            output.WriteEndObject();

            output.WriteStartArray("databases");

            foreach (var database in ContentDeliveryManager.Databases)
            {
                output.WriteStartObject();
                output.WritePropertyString("name", database.DatabaseName);
                output.WritePropertyString("type", database.GetType().FullName ?? string.Empty);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
Esempio n. 6
0
        public override ActionResult GetTemplates(RequestParameters requestParameters)
        {
            var result = Filter(Items.AsQueryable(), requestParameters).Where(t => t.TemplateId == TemplateIDs.Template.Guid).Distinct().ToList();
            var items  = result.OrderBy(t => t.Name).ThenBy(i => i.Path) as IEnumerable <FileDatabaseItem>;

            if (requestParameters.Skip > 0)
            {
                items = items.Skip(requestParameters.Skip);
            }

            if (requestParameters.Take > 0)
            {
                items = items.Take(requestParameters.Take);
            }

            items = items.ToList();

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("count", result.Count);
            output.WritePropertyString("skip", requestParameters.Skip);
            output.WritePropertyString("take", requestParameters.Take);

            output.WriteStartArray("templates");

            foreach (var item in items)
            {
                output.WriteStartObject();
                WriteItemHeader(output, item);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
Esempio n. 7
0
        public override ActionResult GetDatabase(RequestParameters requestParameters)
        {
            var rootItem = Items.FirstOrDefault(i => i.Parent == null);

            if (rootItem == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Root item not found"));
            }

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("type", "items");
            output.WritePropertyString("name", DatabaseName);
            output.WritePropertyString("icon16x16", string.Empty);
            output.WritePropertyString("icon32x32", string.Empty);

            output.WriteStartArray("languages");
            foreach (var language in Items.First(i => i.Path == "/sitecore/system/Languages").Children.OrderBy(l => l.Name))
            {
                output.WriteStartObject();
                output.WritePropertyString("name", language.Name);
                output.WritePropertyString("displayName", language.Name);
                output.WritePropertyString("cultureName", language["Iso"]);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            output.WriteStartObject("root");
            WriteItemHeader(output, rootItem);
            WriteItemFields(output, requestParameters, rootItem);
            output.WriteEndObject();

            return(output.ToContentResult());
        }
Esempio n. 8
0
        public override ActionResult GetChildren(RequestParameters requestParameters, string itemName)
        {
            var items = GetItemsByName(itemName);

            if (!items.Any())
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Item not found"));
            }

            if (items.Count() > 1)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Ambiguous, "Ambiguous item name"));
            }

            var item = items.First();

            if (item == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Item not found"));
            }

            var children = item.Children;

            foreach (var pair in requestParameters.Parameters)
            {
                var fieldName = pair.Key;
                var value     = pair.Value;

                if (!fieldName.EndsWith("]"))
                {
                    children = children.Where(i => i[fieldName] == value);
                    continue;
                }

                var n = fieldName.LastIndexOf('[');
                if (n < 0)
                {
                    throw new SyntaxErrorException("[ expected");
                }

                var op = fieldName.Mid(n + 1, fieldName.Length - n - 2);
                fieldName = fieldName.Left(n).Trim();

                switch (op)
                {
                case "not":
                    children = children.Where(i => i[fieldName] != value);
                    break;

                case "in":
                    var l1 = value.Split('|');
                    children = children.Where(i => l1.Contains(i[fieldName]));
                    break;

                case "not in":
                    var l2 = value.Split('|');
                    children = children.Where(i => !l2.Contains(i[fieldName]));
                    break;

                case "has":
                    children = children.Where(t => !string.IsNullOrEmpty(t[fieldName]));
                    break;
                }
            }

            children = children.ToList();
            var count = children.Count();

            if (requestParameters.Skip > 0)
            {
                children = children.Skip(requestParameters.Skip);
            }

            if (requestParameters.Take > 0)
            {
                children = children.Take(requestParameters.Take);
            }

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("count", count);
            output.WritePropertyString("skip", requestParameters.Skip);
            output.WritePropertyString("take", requestParameters.Take);

            output.WriteStartArray("items");

            foreach (var child in children)
            {
                output.WriteStartObject();
                WriteItemHeader(output, child);
                WriteItemFields(output, requestParameters, child);
                WriteItemChildren(output, requestParameters, child, requestParameters.Children);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
Esempio n. 9
0
        public override ActionResult GetTemplate(RequestParameters requestParameters, string templateName)
        {
            SetContext(requestParameters);

            var templates = GetTemplatesByName(templateName).ToList();

            if (!templates.Any())
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Template not found"));
            }

            if (templates.Count > 1)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Ambiguous, "Ambiguous template name"));
            }

            var templateItem = templates.First();

            if (templateItem == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Template not found"));
            }

            var template = new TemplateItem(templateItem);

            if (template.InnerItem.TemplateID != TemplateIDs.Template)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Item found, but it is not a template"));
            }

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            WriteItemHeader(output, template);

            output.WriteStartArray("fields");

            foreach (var field in template.Fields.OrderBy(f => f.Section.Sortorder).ThenBy(f => f.Section.Name).ThenBy(f => f.Sortorder).ThenBy(f => f.Name))
            {
                var isOwnField    = field.InnerItem.Parent.Parent == template.InnerItem;
                var isSystemField = field.Name.StartsWith("__");

                if (isSystemField && !requestParameters.IncludeSystemFields)
                {
                    continue;
                }

                output.WriteStartObject();
                output.WritePropertyString("id", field.ID.ToString());
                output.WritePropertyString("uri", template.Database.Name + "/" + template.ID + "/" + field.ID);
                output.WritePropertyString("name", field.Name);
                output.WritePropertyString("displayName", field.DisplayName);
                output.WritePropertyString("type", field.Type);
                output.WritePropertyString("source", field.Source);
                output.WritePropertyString("sharing", field.IsShared ? "shared" : field.IsUnversioned ? "unversioned" : "versioned");
                output.WritePropertyString("section", field.Section.Name);
                output.WritePropertyString("kind", isOwnField ? "own" : isSystemField ? "system" : "inherited");
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }
Esempio n. 10
0
        public override ActionResult GetDatabase(RequestParameters requestParameters)
        {
            SetContext(requestParameters);

            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            output.WritePropertyString("type", "items");
            output.WritePropertyString("name", DatabaseName);
            output.WritePropertyString("icon16x16", Images.GetThemedImageSource(Database.Icon, ImageDimension.id16x16));
            output.WritePropertyString("icon32x32", Images.GetThemedImageSource(Database.Icon, ImageDimension.id32x32));

            output.WriteStartArray("languages");
            foreach (var language in Database.GetLanguages().OrderBy(l => l.Name))
            {
                output.WriteStartObject();
                output.WritePropertyString("name", language.Name);
                output.WritePropertyString("displayName", language.GetDisplayName());
                output.WritePropertyString("cultureName", language.CultureInfo.Name);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            output.WriteStartArray("roots");

            ICollection <Item> children = new List <Item>();

            if (string.IsNullOrEmpty(RootItemPath))
            {
                children.Add(Database.GetRootItem());
            }
            else
            {
                var rootItem = Database.GetItem(RootItemPath);
                if (rootItem == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Root Item Path not found"));
                }

                foreach (Item child in rootItem.Children)
                {
                    children.Add(child);
                }
            }

            var rootItems = FilterEnumerable(requestParameters, children);

            foreach (var rootItem in rootItems)
            {
                output.WriteStartObject();
                WriteItemHeader(output, rootItem, requestParameters.Flatten == 0);
                WriteItemFields(output, requestParameters, rootItem);
                output.WriteEndObject();
            }

            output.WriteEndArray();

            return(output.ToContentResult());
        }