开发者

using SPSecurity.RunWithElevatedPrivileges gets an error

the error i get is The security validation for this page is invalid. Click Back in your Web browser, refresh the page and try the operation again. i am using moss 2007

protected void btnSubmit_Click(Object sender, EventArgs e)
    {
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPUtility.ValidateFormDigest();
    using (SPSite mySite = new SPSite(_sLibUrl))
    {

        TextBox txtFirstName = (TextBox)usercontrol.FindControl("txtFirstName");
        TextBox txtLastName = (TextBox)usercontrol.FindControl("txtLastName");
        TextBox txtPhone = (TextBox)usercontrol.FindControl("txtPhone");
        TextBox txtEmail = (TextBox)usercontrol.FindControl("txtEmail");
        TextBox txtSubject = (TextBox)usercontrol.FindControl("txtSubject");
        TextBox txtContant = (TextBox)usercontrol.FindControl("txtContant");

        mySite.AllowUnsafeUpdates = true;
        SPListItemCollection listItems = mySite.AllWebs[WebName].Lists[_sLibName].Items;
        SPListItem item = listItems.Add();
        item["FirstName"] = txtFirstName.Text;
        item["LastName"] = txtLastName.Text;
        item["Phone"] = txtPhone.Text;
        item["Email"] = txtEmail.Text;
        item["Subject"] = txtSubject.Text;
        item["Contant"] = txtContant.Text;

        item.Update();
        mySite.AllowUnsafeUpdates = false;
        mySite.AllWebs[WebName].Lists[_sLibName].Update();
        txtFirstName.Text = string.Empty;
        txtLastName.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtEmail.Text = string.Empty;
        txtSubject.Text = string.Empty;
        txtContant.Text = string.Empty;

    }
    Label lblMessage = (Label)usercontrol.FindControl("lblMessage");
    // lblMessage.Text = "טופס נשלח בהצלחה";
});
        }

        catch (Exception ex)
        {
开发者_运维知识库            Label lbl = (Label)usercontrol.FindControl("lblMessage");
            lbl.Text = ex.Message;
        }
    }


Try putting mySite.AllowUnsafeUpdates = false; after mySite.AllWebs[WebName].Lists[_sLibName].Update();


i found the solution what i need to do is to remove the

mySite.AllowUnsafeUpdates = true; and mySite.AllowUnsafeUpdates = false; and it works


I work with this solution always

using (var site = new SPSite(SPContext.Current.Site.ID))
using (var web = site.OpenWeb())
{
    web.AllowUnsafeUpdates = true;
    //add, update and etc. programatically crud operations with lists 
    web.AllowUnsafeUpdates = false;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜