1

I am using the PreSaveAction JavaScript function to handle list item add and edit operations. But depending upon whether the item is being added ( newform.aspx ) or being updated ( editform.aspx ) I need to implement separate functionality.

How can I check this in PreSaveAction?

or is there any other function to override?

1
  • How are you overriding PreSaveAction? As you noted, there are two different pages for the New and Edit forms, so if you're adding the PreSaveAction override method directly to the forms (such as via a content editor web part with a content link property to an html file), there should be no need to differentiate-- you'd just have different code on each form.
    – Thriggle
    Feb 24 '15 at 18:26
2

If you are in SharePoint 2013, you can use OOB object WPQ2FormCtx. It has a property named FormControlMode which gives the mode of the form. For EditForm the value is 2 and for NewForm the value is 3. So, use WPQ2FormCtx.FormControlMode.

2

There is probably a proper way to do this, but until then I'll just leave this ugliness for now:

var isEditForm = !!window.location.href.match(/editform\.aspx/i);

(Note custom forms will have a different url, so this is not a very good solution).

Update 1:
As commented by @PirateEric looking for ID querystring parameter is another option.
E.g.,

var isEditForm = !!window.location.search.match(/id=\d+/i)

Update 2:
As mentioned in comments JSRequest.QueryString["ID"] can be used to get the ID querystring. It must first be initialized with JSRequest.EnsureSetup();

Another option is to use GetUrlKeyValue. I have tested this on 2010, 2010 custom forms, 2013, 2013 custom forms, O365 and O365 with custom forms and it works in all.
This is the snippet I used:

var isEditForm = !!(GetUrlKeyValue('ID') || GetUrlKeyValue('id'));

ID is as far as I know always upper case, but it is best to be on the safe side.

12
  • Another alternative would just be looking for the existance of the ID query string param, no ID, newform, ID exists, editform Feb 24 '15 at 13:08
  • 2
    @MohamedDerhalli JSRequest.QueryString doesn't seem to always be populated, actually didn't get populated for me on any of these: 2010 form, 2010 custom form, 2013 form, 365 form or 365 custom form
    – eirikb
    Feb 24 '15 at 13:59
  • 2
    How about GetUrlKeyValue. I have seen it working in SP 2010, SP 2013 and O365. Note that ID is case sensitive Feb 24 '15 at 14:26
  • 2
    @eirikb, you need to use: JSRequest.EnsureSetup(); to make sure that the JSRequest object is ready to use, then you can use the object.
    – user19105
    Feb 24 '15 at 15:16
  • 1
    @eirikb, you are right. It doesn't appear there. Feb 25 '15 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.