コード例 #1
0
        protected void ValidateAndSave_Click(object sender, EventArgs e)
        {
            if (IsPropertyMappingValid())
            {
                // For all properties to be mapped, save the values to a temporary list
                var propertyMappings = SavePropertyMappings();

                // Get flag for if content already published
                var wasPublished = _content.Published;

                // Change the document type passing flag to clear the properties
                var newContentType = GetSelectedDocumentType();
                _content.ChangeContentType(newContentType, true);

                // Set the template if one has been selected
                if (NewTemplateList.SelectedItem != null)
                {
                    var templateId = int.Parse(NewTemplateList.SelectedItem.Value);
                    if (templateId > 0)
                    {
                        _content.Template = ApplicationContext.Current.Services.FileService.GetTemplate(templateId);
                    }
                }

                // Set the property values
                var propertiesMappedMessageBuilder = new StringBuilder("<ul>");
                foreach (var propertyMapping in propertyMappings)
                {
                    propertiesMappedMessageBuilder.AppendFormat("<li>{0} {1} {2}</li>",
                                                                propertyMapping.FromName, global::umbraco.ui.Text("changeDocType", "to"), propertyMapping.ToName);
                    _content.SetValue(propertyMapping.ToAlias, propertyMapping.Value);
                }
                propertiesMappedMessageBuilder.Append("</ul>");

                // Save
                var user = global::umbraco.BusinessLogic.User.GetCurrent();
                ApplicationContext.Current.Services.ContentService.Save(_content, user.Id);

                // Publish if the content was already published
                if (wasPublished)
                {
                    ApplicationContext.Current.Services.ContentService.Publish(_content, user.Id);
                }

                // Sync the tree
                ClientTools.SyncTree(_content.Path, true);

                // Reload the page if the content was already being viewed
                ClientTools.ReloadContentFrameUrlIfPathLoaded("/editContent.aspx?id=" + _content.Id);

                // Display success message
                SuccessMessage.Text          = global::umbraco.ui.Text("changeDocType", "successMessage").Replace("[new type]", "<strong>" + newContentType.Name + "</strong>");
                PropertiesMappedMessage.Text = propertiesMappedMessageBuilder.ToString();
                if (wasPublished)
                {
                    ContentPublishedMessage.Text    = global::umbraco.ui.Text("changeDocType", "contentRepublished");
                    ContentPublishedMessage.Visible = true;
                }
                else
                {
                    ContentPublishedMessage.Visible = false;
                }
                SuccessPlaceholder.Visible               = true;
                SaveAndCancelPlaceholder.Visible         = false;
                ValidationPlaceholder.Visible            = false;
                ChangeDocTypePane.Visible                = false;
                ChangeDocTypePropertyMappingPane.Visible = false;
            }
            else
            {
                ValidationPlaceholder.Visible = true;
            }
        }