Prisoner ZERO
Prisoner ZERO

Reputation: 14176

How can I run a Delegate Synchronously?

I have a method which uses a delegate to update a master page for a SharePoint site. I won't go into the details of WHY I need this, but I need to ensure the method runs synchronously in-it-entirety before moving-on to the next step in the process.

How can I do this?

THE CODE LOOKS LIKE:

[DataContract]
public class CustomerPortalBasicSiteProvider : AbstractProvider<bool>, IExecutable
{
    public CustomerPortalBasicSiteProvider()
    {
    }

    List<IProviderSetting> Settings { get; set; }

    public bool Execute(ExecuteParams parameters)
    {
        SetMasterPage(parameters);
        return true;
    }

    private void SetMasterPage(ExecuteParams parameters)
    {
        // NOTE: I need the contents of this method to run synchronously
        SPSecurity.RunWithElevatedPrivileges(
           delegate
           {
               using (var elevatedSite = new SPSite(parameters.SiteUrl))
               {
                   using (var elevatedWeb = elevatedSite.OpenWeb())
                   {
                       elevatedWeb.AllowUnsafeUpdates = true;
                       elevatedWeb.CustomMasterUrl = Settings.Find(x => x.Key == "SPWeb.CustomMasterUrl").Value;
                       elevatedWeb.Update();
                       elevatedWeb.AllowUnsafeUpdates = false;
                   }
               }
           });
    }
}

UPDATE: THE SHAREPOINT OBJECT LOOKS LIKE:

public static class SPSecurity
{
    public static AuthenticationMode AuthenticationMode { get; }
    public static bool CatchAccessDeniedException { get; set; }
    public static bool WebConfigAllowsAnonymous { get; }

    public static void RunWithElevatedPrivileges(SPSecurity.CodeToRunElevated secureCode);
    [Obsolete("Use SetApplicationCredentialKey method instead.")]
    public static void SetApplicationCendentialKey(SecureString password);
    public static void SetApplicationCredentialKey(SecureString password);

    public delegate void CodeToRunElevated();

    public class SuppressAccessDeniedRedirectInScope : IDisposable
    {
        public SuppressAccessDeniedRedirectInScope();

        public void Dispose();
    }
}

Upvotes: 1

Views: 1042

Answers (1)

Markus
Markus

Reputation: 22491

From my experience, RunWithElevatedPrivileges runs the delegate synchronously. The delegate is only required to run the code in another security context. Just to be sure, you could write log messages at the end of your delegate code and as first code after the call to RunWithElevatedPrivileges. If the later is first in the log file, RunWithElevatedPrivileges runs asynchronously.

Upvotes: 3

Related Questions