1

我有一个使用委托来更新 SharePoint 网站的母版页的方法。我不会详细说明为什么需要这个,但我需要确保该方法在整个过程中同步运行,然后再继续进行下一步。

我怎样才能做到这一点?

代码如下:

[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;
                   }
               }
           });
    }
}

更新:共享点对象看起来像:

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();
    }
}
4

1 回答 1

3

根据我的经验, RunWithElevatedPrivileges 同步运行委托。委托只需要在另一个安全上下文中运行代码。可以肯定的是,您可以在委托代码的末尾写入日志消息,并在调用 RunWithElevatedPrivileges 之后作为第一个代码。如果后者是日志文件中的第一个,则 RunWithElevatedPrivileges 异步运行。

于 2013-10-28T13:04:00.680 回答