Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8277315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:39:36+00:00 2026-06-08T08:39:36+00:00

Among the two approaches which one is preferred with run with elevated privileges? First

  • 0

Among the two approaches which one is preferred with run with elevated privileges?

First Approach:

SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite curSite = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb web = curSite.OpenWeb(SPContext.Current.Web.ID))
                        {
                            try
                            {
                                web.AllowUnsafeUpdates = true;
                                \\ do your stuff
                            }
                            catch (Exception e)
                            {

                            }
                            finally
                            {
                                web.AllowUnsafeUpdates = false;
                                web.Dispose();
                            }
                        }
                    }
                });

Second Approach:

SPSite oSite = SPContext.Current.Site;
SPWeb oWeb = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite curSite = new SPSite(oSite.ID))
                    {
                        using (SPWeb web = curSite.OpenWeb(oWeb.ID))
                        {
                            try
                            {
                                web.AllowUnsafeUpdates = true;
                                \\ do your stuff
                            }
                            catch (Exception e)
                            {

                            }
                            finally
                            {
                                web.AllowUnsafeUpdates = false;
                                web.Dispose();
                                oWeb.Dispose();
                                oSite.Dispose();
                            }
                        }
                    }
                });

Are any of them suspected to throw “The web being updated was changed by an external process” exception ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:39:38+00:00Added an answer on June 8, 2026 at 8:39 am

    First of all, you should make a call to SPWeb.ValidateFormDigest() or SPUtility.ValidateFormDigest() before elevating the code. This will get rid of unsafe update not allowed with GET request, and avoid you to setup the AllowUnsafeUpdate property.

    Secondly, as Nigel Whatling mentioned, you are disposing context object in your second code. You don’t have to dispose them. To be simple, only dispose object you are instantiating yourself. A code like yours can cause side effects, as other SharePoint component may require access to SPContext.Current.XX objects. This is probably the root of your issue.

    Thirdly, as you are using the using construct, you don’t have to call .Dispose() on the variable you set up in the using header. Actually, the role (and actually benefit) of the using construct is that you don’t have to care to dispose the object. As soon as the block code exits, even if there was an exception, the .Dispose() method is called on your object.

    To conclude, your code should be changed to that :

    SPUtility.ValidateFormDigest();
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite curSite = new SPSite(SPContext.Current.Site.ID))
        {
            using (SPWeb web = curSite.OpenWeb(SPContext.Current.Web.ID))
            {
                  // Do stuff here
            }
        }
    });
    

    A side note: to elevate a code, you have two options. The one you use here (call to SPSecurity.RunWithElevatedPrivileges) or instantiating a new SPSite with the SystemAccount token :

    using (SPSite curSite = new SPSite(
        SPContext.Current.Site.ID, 
        SPContext.Current.Site.SystemAccount.UserToken
    ))
    {
        using (SPWeb web = curSite.OpenWeb(SPContext.Current.Web.ID))
        {
              // Do stuff here
        }
    }
    

    This will allow you to run elevated code outside a webapplication.

    You should also consider using some utility code to wrap such operations in a more functional way. I’m used to use code like this :

    public static void RunWithElevatedPrivileges(this SPWeb web, Action<SPSite, SPWeb> codeToRunElevated)
    {
        if (CheckIfElevated(web))
        {
            codeToRunElevated(web.Site, web);
        }
        else
        {
            using (var elevatedSite = new SPSite(web.Site.ID, web.AllUsers["SHAREPOINT\\system"].UserToken))
            {
                using (var elevatedWeb = elevatedSite.OpenWeb(web.ID))
                {
                    codeToRunElevated(elevatedSite, elevatedWeb);
                }
            }
        }
    }
    
    /// <summary>
    /// Indicates whether the context has been elevated
    /// </summary>
    public static bool CheckIfElevated(SPWeb web)
    {
        return web.CurrentUser.LoginName == "SHAREPOINT\\system";
    }
    

    Using such code, you can simply do, somewhere in your code :

    SPContext.Current.Web.RunWithElevatedPrivileges((elevatedSite, elevatedWeb)=>{
         // do something will all privileges
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone suggest a better approach for a string comparison among two strings with
I am making an hours calendar using MVC's Code First approach. This is the
Consider a scenario in which two applications have to share data among them. I
string XML1 = <Root><InsertHere></InsertHere></Root>; string XML2 = <Root><child1><childnodes>data</childnodes><childnodes>data1</childnodes></child1><child2><childnodes>data</childnodes><childnodes>data1</childnodes></child2></Root>; Among below mentioned two code samples..
I have two excel files. Among many columns in both these files, they have
I have two Activities along with one Application class, and whenever I call my
I'm using Adobe LiveCycle Designer to create dynamic forms which are automatically filled from
I have a question about the merits of two different approaches to implementing a
This is one of an interview question which I had recently. I would like
I have a web-application which is dependent on two other modules. For simplicity let's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.