I am a newbie in sharepoint and only for 1 month. I am using sharepoint 2007,.aspx. and doing for an email form with some validation, after user pass the validation the email send.
But I'm getting the error thisWeb is undefined, I create this inside javascript function. I have SPUtility.js and I try to add line like SPWeb thisWeb = site.OpenWeb(SPContext.Current.Web.ID; and this causes my whole function not working. Do I need some .js for SPWeb? Please give me some instruction.

Here is my code:

<script type="text/javascript">
function notEmpty(){
    var txt="";
    var myTo= document.getElementById('txtTo2');
    var myName= document.getElementById('txtName2');
    var myFrom= document.getElementById('txtFrom2');
    var myBody= document.getElementById('txtBody2');
    var sendButton = document.getElementById('sendEmail');
    var validateButton = document.getElementById('button1');

    var body = ("This email is send from : "+ myName + ", Note here : " +myBody);

    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

        alert("Please Fill In All Fields!");
        return false;

    else if(!(myTo.value.match(mailformat))||!(myFrom.value.match(mailformat)))
        alert("Invalid Email Address!");
        return false;
    else if(myBody.value.length >5 )
        alert("Please check body field! 5 characters only");
        return false;
            SPUtility.SendEmail(thisWeb, true, false, myTo, myFrom, body);                                                     
            txt="There was an error on this page.\n\n";
            txt+="Error description: " + err.message + "\n\n";
            txt+="Click OK to continue.\n\n";
    return true;

On SharePoint2007, you cannot access SharePoint object like SPWeb from JavaScript. Only on SharePoint 2010, the client object model was added that enables interaction with the SPWeb object (see this link)

Specifically, the easiest solution for the problem of sending an email from client side code is wtriting an Ajax callback, the client code uses the callback and the server-side handler calls SPUtility.SendEmail. (See this thread for more solutions)

| improve this answer | |
  • thanks for the quick reply, now I change my code to something like protected void Btn_SendMail_Click(object sender, EventArgs e) { Microsoft.SharePoint.Utilities.SPUtility.SendEmail(SPContext.Current.Web, true, false, toField, subject, body); ClientScript.RegisterClientScriptBlock (this.GetType(), "confirm", "alert('Thank you. Your message has been successfully sent!')", true); } and It working. Now other problems. It is an asp button. and everytimes I click ok for the validation, the page reload. How to make it not reload? thanks – clipper Jul 29 '12 at 7:20
  • Use Ajax. Otherwise, when using an ASP.NET button, a POST is sent which causes the page to reload. – Ami Luttwak Jul 29 '12 at 7:34
  • Use ASP.NET Update Panel. Read this post for more info. – Ami Luttwak Jul 29 '12 at 8:35

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.