Use the SystemAccount Property When Building SPUserToken Objects

I saw a developer doing this with some funky impersonation today, so I thought I would throw out some sample code. Consider the below shell code, demonstrating the SPSite.SystemAccount property use, subsequently using it to expose the SystemAccount.Token property to pass into the SPSite constructor.

[csharp]
SPWeb primaryWeb = SPContext.Current.Web;
using (var primarySite = new SPSite(primaryWeb.Site.ID))
{
SPUserToken userToken = primarySite.SystemAccount.UserToken;
using (var secondarySite = new SPSite(primarySite.ID,userToken))
{
using (SPWeb secondaryWeb =secondarySite.OpenWeb(primaryWeb.ID))
{

try
{

}
catch (Exception)
{
}

}
}
}
[/csharp]

Share

2 thoughts on “Use the SystemAccount Property When Building SPUserToken Objects”

Leave a Reply

Your email address will not be published. Required fields are marked *