示例#1
0
        /// <summary>
        /// Adds a new item as a child of this item
        /// </summary>
        /// <typeparam name="TItem">The Synthesis type of the child to add. Must be a concrete template type.</typeparam>
        /// <param name="name">Name of the new item</param>
        /// <returns>The newly added child item</returns>
        public TItem Add <TItem>(string name)
            where TItem : class, IStandardTemplateItem
        {
            var type = typeof(TItem);

            ID templateId;

            if (type.IsInterface)
            {
                var attribute = type.GetCustomAttribute <RepresentsSitecoreTemplateAttribute>(false);

                if (attribute == null)
                {
                    throw new ArgumentException("Item interface did not have the requisite [RepresentsSitecoreTemplate] attribute.");
                }

                if (!ID.TryParse(attribute.TemplateId, out templateId))
                {
                    throw new ArgumentException("Item interface's [RepresentsSitecoreTemplate] attribute had an invalid template ID format.");
                }
            }
            else
            {
                var property = type.GetProperty("ItemTemplateId", BindingFlags.Static | BindingFlags.Public);

                if (property == null)
                {
                    throw new ArgumentException(type.FullName + " does not seem to be a generated item type (no ItemTemplateId property was present)");
                }

                var propertyValue = property.GetValue(null) as ID;

                if (propertyValue == (ID)null)
                {
                    throw new ArgumentException("ItemTemplateId property was not of the expected Sitecore.Data.ID type");
                }

                templateId = propertyValue;
            }

            Item newItem = InnerItem.Add(name, new TemplateID(templateId));

            return(newItem.As <TItem>());
        }
示例#2
0
        /// <summary>
        /// Adds a new item as a child of this item
        /// </summary>
        /// <typeparam name="TItem">The Synthesis type of the child to add. Must be a concrete template type.</typeparam>
        /// <param name="name">Name of the new item</param>
        /// <returns>The newly added child item</returns>
        public TItem Add <TItem>(string name)
            where TItem : class, IStandardTemplateItem
        {
            var type     = typeof(TItem);
            var property = type.GetProperty("ItemTemplateId", BindingFlags.Static | BindingFlags.Public);

            if (property == null)
            {
                throw new ArgumentException(type.FullName + " does not seem to be a generated item type (no ItemTemplateId property was present)");
            }

            var propertyValue = property.GetValue(null) as ID;

            if (propertyValue == (ID)null)
            {
                throw new ArgumentException("ItemTemplateId property was not of the expected Sitecore.Data.ID type");
            }

            Item newItem = InnerItem.Add(name, new TemplateID(propertyValue));

            return(newItem.As <TItem>());
        }