Saturday, March 29, 2008

Custom Script while submiting a list item in MOSS

Here is another trick to add custom validations or script while submiting a listitem in NewForm.aspx or EditForm.aspx.

When you click on OK or Submit in NewForm.aspx or EditForm.aspx, it will execute following script to submit the data into SharePoint DB.

if (!PreSaveItem()) return false;
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(<>, '', true, '', '', false, true))
theForm.submit();


So, if you want to add custom validation or script before submiting, then add a Content Editor WebPart in the same page with following script.

< language="javascript">
var submitButton = document.getElementById("<>")
submitButton.onclick = updateButton;

function updateButton()
{
//
// Add custom code to validate a page or any other functionality
// which needs to be performed before submiting a page
//

if (!PreSaveItem()) return false;
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(<>, '', true, '', '', false, true))
theForm.submit();
}
< / script>


in the sameway, you can add confirmation message for cancel button

function updateCancelButton()
{
var conf = confirm("Are you sure you want to cancel?");
if (conf)
{
STSNavigate('\u002f');return false;
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(<>, '', true, '', '', false, true))
}
}

I hope this helps. Happy coding...

11 comments:

PingBack from "Madhav.Blog.com"
Code Snippet
---------------------
CDATA script type="text/javascript" language="javascript">
CDATA[script type="text/javascript" language="javascript">

var cancelbutton = document.getElementById("ctl00_m_g_2e5d4a9d_3bf5_4b06_b550_26a081822fa9_ctl00_toolBarTbltop_RightRptControls_ctl02_ctl00_diidIOGoBack")
cancelbutton.onclick = updateCancelButton;
function updateCancelButton()
{
var conf = confirm("Are you sure you want to cancel?");
if (conf)
{
STSNavigate('http://url/');return false;
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(ctl00$m$g_2e5d4a9d_3bf5_4b06_b550_26a081822fa9$ctl00$toolBarTbltop$RightRptControls$ctl02$ctl00$diidIOGoBack, '', true, '', '', false, true))
}
}
/script>]]>

---------------------

steps followed :
1. In newform.aspx i added content editor webpart below the listview webpart and added the script code in the content editor webpart

still Cancel buton navigates to existing allitems.aspx

all i want to do is navigate the user to custom url after clicking on Cancel button

Any help would be appreciated

Thanks & Regards

This code should work fine. Jst now tested.

Perform the following steps.

1. Add Content editor webpart at the end of the page. It should be added after the Form. Then only the script will be affected.

2. Click on Modify Shared WebPart,

3. Click on Source Editor...

4. Paste the following code

< script type="text/javascript" language="javascript" >
var cancelbutton = document.getElementById("ctl00_m_g_2e5d4a9d_3bf5_4b06_b550_26a081822fa9_ctl00_toolBarTbltop_RightRptControls_ctl02_ctl00_diidIOGoBack")
cancelbutton.onclick = updateCancelButton;
function updateCancelButton()
{
var conf = confirm("Are you sure you want to cancel?");
if (conf)
{
STSNavigate('http://sqltestbed:20000/');return false;
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(ctl00$m$g_2e5d4a9d_3bf5_4b06_b550_26a081822fa9$ctl00$toolBarTbltop$RightRptControls$ctl02$ctl00$diidIOGoBack, '', true, '', '', false, true))
}
}
< /script >

Note: remove spaces in Script tag, blogger is not allowing to add the Script tags.

5. Check the Control ID.
6. Save the changes and Apply webpart.

This should allow when you click on Cancel button to prompt a custom message and redirect to custom URL without any issue.

- Phani

Hi Not working yet ,

after form you mean after listview webpart right as form tag would be in masterpage , kindly confirm me on that . incase i put it after listviewwebpart it doesnt work for me plus when i go into edit it throws error :

"Invalid postback or callback argument. Event validation is enabled using < pages enableEventValidation="true"/ > in configuration or < %@ Page EnableEventValidation="true" %> in a page"

How are you adding this script? thru SharePoint designer or thru IE using Modify Shared Page?

Designer , by default edit option wont be available edit option can only we available once we have content editor webpart , so as far i think the only way to put the webpart is

Sharepoint Designer , please correct me if i am wrong , i would be thankful if you can paste the sample page html from designer

I will send you a mail with attachment if you could share your email ID.

sober4in@gmail.com

thanks for assistance :)

How to validate checkbox (a choice field) with more than one choices (in SharePoint List); there should be atleast one choice selected.

When one clicks the 'OK' button, if the checkbox has no choice selected it should prompt and do not save the List item.

i tried this but code it getting executed at the page load itself. doesn't metter how you place the code.

In newform.aspx and Editform.aspx i added content editor webpart below the listview webpart and added the script code in the content editor webpart still Cancel buton navigates to existing allitems.aspx

this code is not working ? what is wrong?

I downloaded above the script and implemented to my project. It's very useful to me.Most of the technical guys are following this kind of information.website hosting

web hosting

Post a Comment