locked
MOSS 2007: Field validation during Check-in/Publish action in Publishing Page RRS feed

  • Question

  • I need to validate a particular field during check-in/ Publish and alert(Show message box) the user for any error.

    Basically i need something like the javascript function - Presaveaction on list forms(newform.aspx, editform.aspx).

    Thanks for any help.


    soundar

    • Edited by Soundar_SP Monday, February 6, 2012 1:55 PM
    Monday, February 6, 2012 8:11 AM

Answers

  • Finally found the required solution!!!!

    Cancel event causing "UnspecifiedError" on theform.Submit() in postback can be handled through the approach shown in the below link.

    http://tinisles.blogspot.in/2005/10/onbeforeunload-throwing-errors-in-ie.html

    Now, the cancel event worked fine. But, the form did not responded to "Check-in" events later.

    To overcome this should set the _spFormOnSubmitCalled = false;

    Final code will look like this.....

    <script type="text/javascript">

    var UNLOAD_MSG = "Field Validation Failed!";
    var __oldDoPostBack = __doPostBack;
    __doPostBack = CatchExplorerError;
     
    function CatchExplorerError (eventTarget, eventArgument)
    {
        try
        {
            return __oldDoPostBack (eventTarget, eventArgument);
        } catch (ex)
        {
            // don't want to mask a genuine error
            // lets just restrict this to our 'Unspecified' one
            if (ex.message.indexOf('Unspecified') == -1)
            {
                throw ex;
            } 
            else
            {
            window.setTimeout(function() { _spFormOnSubmitCalled = false; }, 10);
            }          
        }
    }

    function doBeforeUnload() {

      var theform;

        theform = document.forms["aspnetForm"];  
          if (!theform) {
                theform = document.aspnetForm;
    }         
           if(theform.__EVENTARGUMENT.value == "checkInPage")
           {
    //Field Validation logic
       if(window.event)
          window.event.returnValue = UNLOAD_MSG; // IE
       else
          return UNLOAD_MSG; // FX
     
          }  
      
    }

    if(window.body)
       window.body.onbeforeunload = doBeforeUnload; // IE
    else
       window.onbeforeunload = doBeforeUnload; // FX
          </script>


    soundar

    • Marked as answer by Soundar_SP Thursday, February 9, 2012 10:20 AM
    Thursday, February 9, 2012 10:20 AM

All replies

  •  

    Hi Soundar_SP,

    Java script can’t achieve it. Because java script can't capture then check in event . So you have to use event handler to do this. Here is the simple code.

        public override void ItemCheckingIn(SPItemEventProperties properties)

            {

                base.ItemCheckingIn(properties);

                SPListItem item = properties.ListItem;

                using (SPSite thisSite = new SPSite(properties.WebUrl))

                {

                    SPWeb thisWeb = thisSite.OpenWeb();

                   if(item["Title"]=="A")

                    {

                        properties.Cancel = true;

                        properties.ErrorMessage = "Item Title can't be A";

                    }

                }

            }

    More about event handler please refer to the following link.

    http://madsharepoint.blogspot.com/2009/07/basic-event-handling-in-sharepoint.html

    Thanks,

    Jack

    Tuesday, February 7, 2012 9:04 AM
    Moderator
  • Hi Jack Gao,

                     Thanks for your time and inputs.

    Actually, based on my client requirements, the alert should be a warning message which he can ignore.

    So, i basically need a Confirm box with option "Ok" and "Cancel". On clicking "Ok" the user will be retained in the page and on clicking "Cancel" he will proceed with check-in process. Hence, i cannot use an eventhandler.


    soundar


    • Edited by Soundar_SP Tuesday, February 7, 2012 10:51 AM
    Tuesday, February 7, 2012 10:50 AM
  • Had some breakthrough.

    I tried using window.onbeforeunload to make the check. But, the "Cancel" event in the message box throws some unspecified error !!

    Pl. find the same code below.

    <

    script type="text/javascript">

    window.onbeforeunload = CallAlert;

    function CallAlert() {

    var myform;

    myform = document.forms[

    "Form1"];

    if (!myform) {

    myform = document.Form1;

    }

    if(myform.__EVENTARGUMENT.value == "checkInPage")

    {

    //Perform the field validation

    if(!valid)

    {

    return "Field Validation falied!! Do you want to continue";

    }

    }

    }

    </

    script>


    soundar

    Wednesday, February 8, 2012 2:38 PM
  • Finally found the required solution!!!!

    Cancel event causing "UnspecifiedError" on theform.Submit() in postback can be handled through the approach shown in the below link.

    http://tinisles.blogspot.in/2005/10/onbeforeunload-throwing-errors-in-ie.html

    Now, the cancel event worked fine. But, the form did not responded to "Check-in" events later.

    To overcome this should set the _spFormOnSubmitCalled = false;

    Final code will look like this.....

    <script type="text/javascript">

    var UNLOAD_MSG = "Field Validation Failed!";
    var __oldDoPostBack = __doPostBack;
    __doPostBack = CatchExplorerError;
     
    function CatchExplorerError (eventTarget, eventArgument)
    {
        try
        {
            return __oldDoPostBack (eventTarget, eventArgument);
        } catch (ex)
        {
            // don't want to mask a genuine error
            // lets just restrict this to our 'Unspecified' one
            if (ex.message.indexOf('Unspecified') == -1)
            {
                throw ex;
            } 
            else
            {
            window.setTimeout(function() { _spFormOnSubmitCalled = false; }, 10);
            }          
        }
    }

    function doBeforeUnload() {

      var theform;

        theform = document.forms["aspnetForm"];  
          if (!theform) {
                theform = document.aspnetForm;
    }         
           if(theform.__EVENTARGUMENT.value == "checkInPage")
           {
    //Field Validation logic
       if(window.event)
          window.event.returnValue = UNLOAD_MSG; // IE
       else
          return UNLOAD_MSG; // FX
     
          }  
      
    }

    if(window.body)
       window.body.onbeforeunload = doBeforeUnload; // IE
    else
       window.onbeforeunload = doBeforeUnload; // FX
          </script>


    soundar

    • Marked as answer by Soundar_SP Thursday, February 9, 2012 10:20 AM
    Thursday, February 9, 2012 10:20 AM