Example #1
0
        public ActionResult CreateContent(string node, string contentName, string contentType, string templateName, string back)
        {
            AssertPermission();

            node         = HttpUtility.UrlDecode(node);
            contentName  = HttpUtility.UrlDecode(contentName);
            contentType  = HttpUtility.UrlDecode(contentType);
            templateName = HttpUtility.UrlDecode(templateName);
            back         = HttpUtility.UrlDecode(back);

            if (string.IsNullOrEmpty(contentName))
            {
                contentName = !string.IsNullOrEmpty(templateName) ? templateName : contentType;
            }

            var parent = Node.LoadNode(node);

            if (parent == null)
            {
                return(this.Redirect(back));
            }

            //var template = SNCR.ContentTemplateResolver.Instance.GetNamedTemplate(contentType, templateName);
            var template = ContentTemplate.GetNamedTemplate(contentType, templateName);

            SNCR.Content newContent = null;

            if (template != null)
            {
                newContent = ContentTemplate.CreateTemplated(parent, template, contentName);
            }
            //else
            //    SNCR.Content.CreateNew(contentType, parent, contentName, null);

            try
            {
                if (newContent != null)
                {
                    newContent.Save();
                }
            }
            catch (Exception ex)
            {
                Logger.WriteException(ex);
            }

            return(this.Redirect(back));
        }
        private Content CreateContent(JObject model, ODataRequest odataRequest)
        {
            var contentTypeName = GetPropertyValue <string>("__ContentType", model);
            var templateName    = GetPropertyValue <string>("__ContentTemplate", model);

            var name = GetPropertyValue <string>("Name", model);

            if (string.IsNullOrEmpty(name))
            {
                var displayName = GetPropertyValue <string>("DisplayName", model);
                name = ContentNamingProvider.GetNameFromDisplayName(displayName);
            }
            else
            {
                // do not allow saving a content with unencoded name
                name = ContentNamingProvider.GetNameFromDisplayName(name);
            }

            var parent = Node.Load <GenericContent>(odataRequest.RepositoryPath);

            if (string.IsNullOrEmpty(contentTypeName))
            {
                var allowedChildTypeNames = parent.GetAllowedChildTypeNames();

                if (allowedChildTypeNames is AllContentTypeNames)
                {
                    contentTypeName = typeof(ContentRepository.File).Name;
                }
                else
                {
                    var allowedContentTypeNames = parent.GetAllowedChildTypeNames().ToArray();
                    contentTypeName = allowedContentTypeNames.FirstOrDefault();
                    if (string.IsNullOrEmpty(contentTypeName))
                    {
                        contentTypeName = typeof(ContentRepository.File).Name;
                    }
                }
            }

            Content content;
            Node    template = null;

            if (templateName != null)
            {
                template = ContentTemplate.GetNamedTemplate(contentTypeName, templateName);
            }

            if (template == null)
            {
                content = Content.CreateNew(contentTypeName, parent, name);
            }
            else
            {
                var templated = ContentTemplate.CreateFromTemplate(parent, template, name);
                content = Content.Create(templated);
            }


            UpdateFields(content, model);

            if (odataRequest.MultistepSave)
            {
                content.Save(SavingMode.StartMultistepSave);
            }
            else
            {
                content.Save();
            }

            return(content);
        }