SharePoint 2003: Impersonating and using administrative functions

I have a case where I need to perform administrative functions (i.e. from the Microsoft.SharePoint.Administration namespace) on a custom page running in SharePoint. The page may be accessed by a user who does not have administrative rights, but the code needs to run anyway. To do this, I ensured that there was no reference to the normal SharePoint digest on the page and instead added a __REQUESTDIGEST hidden field as per the article http://msd2d.com/Content/Tip_viewitem_03NoAuth.aspx?section=Sharepoint&id=15A38A41-F5F1-4FDD-B5D3-6F80E54D988F, but whilst impersonating. I was then subsequently able to user administrative functions when impersonating in later methods. The code shown below references an impersonation class.

Note that I am not sure if this is the correct way to achieve the task, and would be interested to hear any suggestions.

To go in the aspx.cs file


protected override void Page_Load(object sender, System.EventArgs e)
{
    Impersonator imp = new Impersonator();
    imp.Impersonate();
    SPGlobalAdmin globalAdmin = new SPGlobalAdmin();
    Context.Items[SPGlobalAdmin.RequestFromAdminPort] = true;
    Page.RegisterHiddenField("__REQUESTDIGEST", globalAdmin.AdminFormDigest);
    imp.Undo();
}
private void buttonUpdateVersions_Click(object sender, System.EventArgs e)
{
    Impersonator imp = new Impersonator();
    imp.Impersonate();
    // Do something here...
    imp.Undo();
}

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>