{"id":1632,"date":"2013-08-22T18:46:20","date_gmt":"2013-08-22T16:46:20","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=1632"},"modified":"2013-08-22T18:48:54","modified_gmt":"2013-08-22T16:48:54","slug":"what-about-you-must-fill-out-all-required-properties-before-completing-this-action-when-publishing-a-page","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/what-about-you-must-fill-out-all-required-properties-before-completing-this-action-when-publishing-a-page\/","title":{"rendered":"What about &#8216;You must fill out all required properties before completing this action&#8217; when Publishing a page"},"content":{"rendered":"<p>When a required managed metadata (taxonomy) field is located on a pagelayout and this field is not filled with a value when publishing the page SharePoint will throw an error message in a popup with the text \u2018You must fill out all required properties before completing this action\u2019:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/08\/MMDRequiredFieldErrorMessage.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1646 alignnone\" title=\"MMDRequiredFieldErrorMessage\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/08\/MMDRequiredFieldErrorMessage.png\" alt=\"\" width=\"502\" height=\"125\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/08\/MMDRequiredFieldErrorMessage.png 502w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/08\/MMDRequiredFieldErrorMessage-300x74.png 300w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><\/a><\/p>\n<p>Where the OK button navigates to the edit properties url of the page.<br \/>\nThis behavior only occurs when a page with a required managed metadata field is published before it\u2019s successfully saved at least once.<br \/>\nWhen a page is saved and the required managed metadata field is empty an error message is displayed in the status bar and not as a popup:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/08\/MMDRequiredFieldStatusMessages.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1647\" title=\"MMDRequiredFieldStatusMessages\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/08\/MMDRequiredFieldStatusMessages.png\" alt=\"\" width=\"801\" height=\"28\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/08\/MMDRequiredFieldStatusMessages.png 801w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/08\/MMDRequiredFieldStatusMessages-300x10.png 300w\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" \/><\/a><\/p>\n<p>Apparently the validation of required fields differ when saving the page from publishing the page.<\/p>\n<p>Background information can be found <a href=\"https:\/\/www.itidea.nl\/index.php\/background-of-what-about-you-must-fill-out-all-required-properties-before-completing-this-action-when-publishing-a-page\/\">here<\/a>.<\/p>\n<p>The popup error message is confusing for a lot of users, they don\u2019t understand why this sometimes happens.<br \/>\nThey read the message and press \u2018Ok\u2019 and they are navigated away from the page to the edit properties and then what. The field was on the page, why am I now on another page?<\/p>\n<h3>How to prevent to popup error message<\/h3>\n<p>To prevent the nasty popup some code is needed to check if there are any managed metadata fields on the page which are required and now empty when the Publish button is pressed.<br \/>\nWhen there are no required managed metadata fields on the page, there still can be required managed metadata fields at the listitem. In this case ootb SharePoint code is of course leading and no interference is needed: the popup error message will show and that\u2019s exactly how it should work.<br \/>\nIf there are, the SPPageStateControl will be questioned if there are any errors. When there are no errors nothing has to be done.<br \/>\nWhen there is an error, the popup can be skipped by calling \u2018EnsureItemSavedIfEditMode(false);\u2019 on the SPPageStateControl which validates the page by calling Page.Validate(), this results in an invalid page and the error message will be added to the status messages as with Save &amp; Close.<br \/>\nNo other code is skipped from the process other than the nasty popup and the code is quite narrowed by the check if there are required managed metadata fields on the page.<\/p>\n<p>A small control has to be created with the functionality described above and this has to be added to each pagelayout where it is appropriate to add.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ToolboxData(&quot;&lt;{0}:CustomValidationRequiredFieldsOnPage runat=server&gt;&lt;\/{0}:CustomValidationRequiredFieldsOnPage&gt;&quot;)]\r\npublic class CustomValidationRequiredFieldsOnPage : WebControl\r\n{\r\n protected override void CreateChildControls()\r\n {\r\n base.CreateChildControls();\r\n\r\n if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)\r\n {\r\n  bool arethere = AreThereAnyMissingRequiredFieldsOnPage();\r\n\r\n  if (arethere)\r\n  {\r\n   \/\/SPPageStateControl:\r\n   \/\/Provides an ASP.NET control that handles the Ribbon buttons controlling the state of a Microsoft SharePoint Server wiki or publishing page,\r\n   \/\/such as the CheckInCheckOutButton or the PublishingButton.\r\n   SPPageStateControl baseParentStateControl = Page.Items&#x5B;typeof(SPPageStateControl)] as SPPageStateControl;\r\n\r\n   \/\/Publish button: SPListItem MissingRequiredFields checks this.FieldHasValue(link.Name, field);\r\n   \/\/the field is empty (which is right) when the page is first created (MMD field is never filled in)\r\n   \/\/when the field was once filled, saved and emptied the field in sp code still has the previous value and the check MissingRequiredFields succeeds\r\n   \/\/after succeeding this check the page is validated (this.Page.Validate()) and this one fails which results SP validating the page as the Save button does\r\n\r\n   if (baseParentStateControl.HasError)\r\n   {\r\n    \/\/this overwrites the previous PageErrorState\r\n    \/\/and validates the page\r\n    \/\/no popup anymore and status updates in yellow area\r\n    baseParentStateControl.EnsureItemSavedIfEditMode(false);\r\n   }\r\n  }\r\n  else\r\n  {\r\n   \/\/there are missing fields at this listitem, but they're not on the page\r\n   \/\/do nothing here, because the SerializedErrorState contains the navigate url to the Edit Properties page\r\n   \/\/and a message pops up\r\n  }\r\n }\r\n}\r\n\r\n \/\/\/ &lt;summary&gt;\r\n \/\/\/ Check if required fields are missing which are present at the page\r\n \/\/\/ &lt;\/summary&gt;\r\n \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n private static bool AreThereAnyMissingRequiredFieldsOnPage()\r\n {\r\n  foreach (Control control in SPContext.Current.FormContext.FieldControlCollection)\r\n  {\r\n   \/\/get the control type\r\n   string type = control.GetType().Name;\r\n\r\n   FieldTypes controlType = (FieldTypes)Enum.Parse(typeof(FieldTypes), type);\r\n\r\n   switch (controlType)\r\n   {\r\n    case FieldTypes.TaxonomyFieldControl:\r\n     TaxonomyFieldControl tfc = control as TaxonomyFieldControl;\r\n     if (!tfc.IsValid)\r\n     {\r\n      return true;\r\n     }\r\n     break;\r\n    default:\r\n     break;\r\n   }\r\n }\r\n\r\n return false;\r\n}\r\n\r\n enum FieldTypes\r\n {\r\n DateTimeField, FieldValue, TextField, RichImageField, NoteField, RichHtmlField, PublishingScheduleFieldControl, TaxonomyFieldControl, BooleanField, ComputedField\r\n }\r\n}\r\n<\/pre>\n<p>If you want you even can add your own error status message by calling<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nbaseParentStateControl.AddErrorStatus(&quot;We have an error...&quot;);\r\n <\/pre>\n<p>You\u2019re unable to create an error popup as SharePoint does, but in fact it\u2019s just a modaldialog, so you can.<\/p>\n<h3>Summary<\/h3>\n<p>With a few lines of code the nasty popup error message can be prevented to be displayed. The background story behind it is quite large, but important to understand. Be careful to never overwrite SharePoint message when publishing stuff, because it can lead to inappropriate behavior.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When a required managed metadata (taxonomy) field is located on a pagelayout and this field is not filled with a value when publishing the page SharePoint will throw an error message in a popup with the text \u2018You must fill &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/what-about-you-must-fill-out-all-required-properties-before-completing-this-action-when-publishing-a-page\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[20,42],"class_list":["post-1632","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-c","tag-sharepoint-2010"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=1632"}],"version-history":[{"count":21,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1632\/revisions"}],"predecessor-version":[{"id":1661,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1632\/revisions\/1661"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=1632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=1632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=1632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}