0

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?

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

1 Answer 1

1

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)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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