How to use the AllowUnsafeUpdates on the LayoutsPageBase in SharePoint 2013?

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/139616

  •  03-10-2020
  •  | 
  •  

Вопрос

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();    
    }
    }
}
Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top