What about ‘You must fill out all required properties before completing this action’ when Publishing a page

22 Aug

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 ‘You must fill out all required properties before completing this action’:

Where the OK button navigates to the edit properties url of the page.
This behavior only occurs when a page with a required managed metadata field is published before it’s successfully saved at least once.
When 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:

Apparently the validation of required fields differ when saving the page from publishing the page.

Background information can be found here.

The popup error message is confusing for a lot of users, they don’t understand why this sometimes happens.
They read the message and press ‘Ok’ 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?

How to prevent to popup error message

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.
When 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’s exactly how it should work.
If there are, the SPPageStateControl will be questioned if there are any errors. When there are no errors nothing has to be done.
When there is an error, the popup can be skipped by calling ‘EnsureItemSavedIfEditMode(false);’ 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 & Close.
No 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.

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.

[ToolboxData("<{0}:CustomValidationRequiredFieldsOnPage runat=server></{0}:CustomValidationRequiredFieldsOnPage>")]
public class CustomValidationRequiredFieldsOnPage : WebControl
{
 protected override void CreateChildControls()
 {
 base.CreateChildControls();

 if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
 {
  bool arethere = AreThereAnyMissingRequiredFieldsOnPage();

  if (arethere)
  {
   //SPPageStateControl:
   //Provides an ASP.NET control that handles the Ribbon buttons controlling the state of a Microsoft SharePoint Server wiki or publishing page,
   //such as the CheckInCheckOutButton or the PublishingButton.
   SPPageStateControl baseParentStateControl = Page.Items[typeof(SPPageStateControl)] as SPPageStateControl;

   //Publish button: SPListItem MissingRequiredFields checks this.FieldHasValue(link.Name, field);
   //the field is empty (which is right) when the page is first created (MMD field is never filled in)
   //when the field was once filled, saved and emptied the field in sp code still has the previous value and the check MissingRequiredFields succeeds
   //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

   if (baseParentStateControl.HasError)
   {
    //this overwrites the previous PageErrorState
    //and validates the page
    //no popup anymore and status updates in yellow area
    baseParentStateControl.EnsureItemSavedIfEditMode(false);
   }
  }
  else
  {
   //there are missing fields at this listitem, but they're not on the page
   //do nothing here, because the SerializedErrorState contains the navigate url to the Edit Properties page
   //and a message pops up
  }
 }
}

 /// <summary>
 /// Check if required fields are missing which are present at the page
 /// </summary>
 /// <returns></returns>
 private static bool AreThereAnyMissingRequiredFieldsOnPage()
 {
  foreach (Control control in SPContext.Current.FormContext.FieldControlCollection)
  {
   //get the control type
   string type = control.GetType().Name;

   FieldTypes controlType = (FieldTypes)Enum.Parse(typeof(FieldTypes), type);

   switch (controlType)
   {
    case FieldTypes.TaxonomyFieldControl:
     TaxonomyFieldControl tfc = control as TaxonomyFieldControl;
     if (!tfc.IsValid)
     {
      return true;
     }
     break;
    default:
     break;
   }
 }

 return false;
}

 enum FieldTypes
 {
 DateTimeField, FieldValue, TextField, RichImageField, NoteField, RichHtmlField, PublishingScheduleFieldControl, TaxonomyFieldControl, BooleanField, ComputedField
 }
}

If you want you even can add your own error status message by calling

baseParentStateControl.AddErrorStatus("We have an error...");
 

You’re unable to create an error popup as SharePoint does, but in fact it’s just a modaldialog, so you can.

Summary

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.

2 Replies to “What about ‘You must fill out all required properties before completing this action’ when Publishing a page

  1. Thank you for sharing this workaround / solution.

    Is this issue fixed with hotfix Hotfix KB2883088 http://support2.microsoft.com/kb/2883088 ?
    “Assume that you have a SharePoint 2013 page that contains a multi-value lookup field that requires a value. You check out the page and edit it, and the lookup field contains a value. In this situation, you might be unable to check in the page without saving it first. Additionally, you might receive an error message that resembles the following:
    You must fill out required properties before completing this action.”

  2. Hi Harold,

    Thanks for pointing this out!
    I’ll have to take a look at it after installing the hotfix.
    The post described the behaviour using SharePoint 2010.

    Regards, Anita

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.