Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I get the following exception message:

{"Attempted to perform an unauthorized operation."}
at Microsoft.SharePoint.SPSite.get_Owner()
at MySite.Intranet.QuickLinks.MySiteviewpage.GetSiteOwner()

When trying to run the following code:

    private string GetSiteOwner()
    {
        string email = string.Empty;

        SPSite siteCollection = new SPSite(SPContext.GetContext(System.Web.HttpContext.Current).Site.Url);

        SPWeb site = siteCollection.OpenWeb();

        //SPSecurity.RunWithElevatedPrivileges(delegate()
        //{
            using (site)
            {
                email = site.Owner.Email; //HITTING THE OWNER CAUSES THE EXCPTION
            }
        //});

        return email;
    }

Can anyone help please?

share|improve this question
    
I presume you're running this from a web part or something? –  SHug Nov 25 '11 at 14:00
    
Yea, it is a custom control –  Burt Nov 25 '11 at 14:22
    
Does the ULS log show anything in particular when this is running? –  SHug Nov 25 '11 at 15:25
add comment

1 Answer 1

up vote 1 down vote accepted

If you are going to use SPSecurity.RunWithElevatedPrivileges() you need to put it around where you instantiate your SPSite and SPWeb objects.

Also, when "newing up" your SPSite and SPWeb objects, you need to ensure you dispose of them otherwise you'll have memory leak issue. (Have a look here for examples)

Try this:

private string GetSiteOwner() 
{ 
    string email = string.Empty; 

    SPSecurity.RunWithElevatedPrivileges(delegate() 
    { 
        using(SPSite siteCollection = new SPSite(SPContext.Current.Site.Url))
        {
            email = siteCollection.Owner.Email; 
        } 
    });
    return email; 
} 

(I've removed the SPWeb reference as SPWeb doesn't contain an Owner property)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.