0

I am trying create an application page and want to make sure that AllowUnsafeUpdates property is properly used.

Please suggest me the proper usage of AllowunsafeUpdates from the following code.

I have two textbox and would be updating two Items from two different Lists.

namespace SharePointProject5.Layouts.SharePointProject5
{
    public partial class ApplicationPage1 : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Button1.click += Button1_Click;
            this.Button2.click += Button2_Click;
            this.Button3.click += Button3_Click;
            this.Button4.click += Button4_Click;
            this.Button5.click += Button5_Click;
            this.Button6.click += Button5_Click;
            this.Button7.click += Button5_Click;
        }

    void Button1_Click(object sender, EventArgs s)
    {
            SPWeb web = SPContext.Current.Web;

            web.AllowUnsafeUpdates = true;
            web.Update();

            // Process the SPListItem from List#1
            spListItem1.Update();

            web.AllowUnsafeUpdates = false;
            web.Update();


            web.AllowUnsafeUpdates = true;
            web.Update();

            // Process the SPListItem from List#2
            spListItem2.Update();

            web.AllowUnsafeUpdates = false;
            web.Update();

            SomeOtherFunction();    
    }

    void Button2_Click(object sender, EventArgs s)
    {
            SPWeb web = SPContext.Current.Web;

            web.AllowUnsafeUpdates = true;
            web.Update();

            // Process the SPListItem from List#1
            spListItem1.Update();

            // Process the SPListItem from List#2
            spListItem2.Update();

            web.AllowUnsafeUpdates = false;
            web.Update();

            SomeOtherFunction();    
    }

    void Button3_Click(object sender, EventArgs s)
    {
            SPWeb web = SPContext.Current.Web;

            web.AllowUnsafeUpdates = true;
            web.Update();

            // Process the SPListItem from List#1
            spListItem1.Update();

            // Process the SPListItem from List#2
            spListItem2.Update();

            SomeOtherFunction();    
    }


    void Button4_Click(object sender, EventArgs s)
    {
            SPWeb web = SPContext.Current.Web;

            // Process the SPListItem from List#1
            spListItem1.Update();

            // Process the SPListItem from List#2
            spListItem2.Update();

            SomeOtherFunction();    
    }


    void Button5_Click(object sender, EventArgs s)
    {
            using (SPWeb web = SPContext.Current.Web)
            {
                web.AllowUnsafeUpdates = true;
                web.Update();

                // Process the SPListItem from List#1
                spListItem1.Update();

                web.AllowUnsafeUpdates = false;
                web.Update();


                web.AllowUnsafeUpdates = true;
                web.Update();

                // Process the SPListItem from List#2
                spListItem2.Update();

                web.AllowUnsafeUpdates = false;
                web.Update();
            }
            SomeOtherFunction();    
    }

    void Button6_Click(object sender, EventArgs s)
    {
            using (SPWeb web = SPContext.Current.Web)
            {
                web.AllowUnsafeUpdates = true;
                web.Update();

                // Process the SPListItem from List#1
                spListItem1.Update();

                // Process the SPListItem from List#2
                spListItem2.Update();

                web.AllowUnsafeUpdates = false;
                web.Update();
            }
            SomeOtherFunction();    
    }

    void Button7_Click(object sender, EventArgs s)
    {
            using (SPWeb web = SPContext.Current.Web)
            {
                try
                {
                    web.AllowUnsafeUpdates = true;
                    web.Update();

                    // Process the SPListItem from List#1
                    spListItem1.Update();

                    // Process the SPListItem from List#2
                    spListItem2.Update();
                }
                catch(Exception ex)
                {
                    ...
                }
                finally
                {
                    web.AllowUnsafeUpdates = false;
                    web.Update();
                }
            }
            SomeOtherFunction();    
    }
    }
}
1

In the code you've posted, it is unnecessary. In fact, using AllowUnsafeUpdates is very rarely appropriate.

  • Does it mean that the following code is valide? void Button4_Click(object sender, EventArgs s) { SPWeb web = SPContext.Current.Web; // Process the SPListItem from List#1 spListItem1.Update(); // Process the SPListItem from List#2 spListItem2.Update(); SomeOtherFunction(); } – Karthikeyan Apr 24 '15 at 19:34
  • 1
    You don´t need AllowUnsafeUpdates behind a button. – Juan Pablo Pussacq Laborde Apr 24 '15 at 20:13
  • And if you ever need to use AllowUnsafeUpdates, you don't need to update the SPWeb object: web.AllowUnsafeUpdates = true; spListItem1.Update(); web.AllowUnsafeUpdates = false; – Paul Strupeikis Apr 27 '15 at 9:01

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.