I want to add the following code to my page layout to fix a bug in SharePoint 2013:-
[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
}
}
but can anyone adivce , how i can add this code to my page layout , either using SP designer or visual studio ? Thanks