Skip to content

Instantly share code, notes, and snippets.

@lowedown
Last active August 29, 2015 14:03
Show Gist options
  • Save lowedown/a35aea634d9410921659 to your computer and use it in GitHub Desktop.
Save lowedown/a35aea634d9410921659 to your computer and use it in GitHub Desktop.
Extension of a Sitecore PublishAgent to run publish jobs under a specific user
public class AuthenticatedPublishAgent : PublishAgent
{
private readonly string _userName;
public string UserName
{
get
{
return this._userName;
}
}
public AuthenticatedPublishAgent(string sourceDatabase, string targetDatabase, string mode, string languages, string user) : base(sourceDatabase, targetDatabase, mode, languages)
{
this._userName = user;
}
public void RunAsUser()
{
if (string.IsNullOrEmpty(UserName) || !Sitecore.Security.Accounts.User.Exists(UserName))
{
Log.Error(string.Format("Unable to run AuthenticatedPublishAgent as user: {0}", UserName), this);
return;
}
var user = Sitecore.Security.Accounts.User.FromName(UserName, false);
using (new Sitecore.Security.Accounts.UserSwitcher(user))
{
base.Run();
}
}
}
<agent type="MyNameSpace.AuthenticatedPublishAgent" method="RunAsUser" interval="01:00:00">
<param desc="source database">master</param>
<param desc="target database">web</param>
<param desc="mode (full or smart or incremental)">smart</param>
<param desc="languages">en</param>
<param desc="user">sitecore\AutoPublishingUser</param>
</agent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment