Janis Veinbergs
Janis Veinbergs

Reputation: 6988

SPContext.Current throws SecurityException for most properties

Many properties for SPContext.Current objects throws SecurityException. (Like SPContext.Current.Fields, many properties in SPContext.Current.Site (see details).

Where could be the problem? Could someone, please, point me in right direction?

Thank you.

Details

From Immediate window:

SPContext.Current.Site
{Microsoft.SharePoint.SPSite}
    AllowRssFeeds: true
    AllowUnsafeUpdates: 'SPContext.Current.Site.AllowUnsafeUpdates' threw an exception of type 'System.Security.SecurityException'
    AllWebs: {Microsoft.SharePoint.SPWebCollection}
    ApplicationRightsMask: 9223372036854775807
    Audit: {Microsoft.SharePoint.SPAudit}
    CatchAccessDeniedException: true
    CertificationDate: 'SPContext.Current.Site.CertificationDate' threw an exception of type 'System.Security.SecurityException'
    ContentDatabase: {Microsoft.SharePoint.Administration.SPContentDatabase}
    CurrentChangeToken: 'SPContext.Current.Site.CurrentChangeToken' threw an exception of type 'System.Security.SecurityException'
    ...

And even exception have exception's within himself:

Exception Window http://img41.imageshack.us/img41/442/ss20100204112542.png

ULS Log somwhere in that time, when exception occurs: ULS Log http://img715.imageshack.us/img715/465/ss20100204113046.png

Event Viewer has bunch of errors

Upvotes: 0

Views: 1244

Answers (2)

Mandrake
Mandrake

Reputation: 1161

I know I have had to use code to ensure that the SPContext exists when executing from a non-web context.

public static void EnsureSPContext(this SPWeb web)
    {
        // Ensure HttpContext.Current
        if (HttpContext.Current == null)
        {
            HttpRequest request = new HttpRequest("", web.Url, "");
            HttpContext.Current = new HttpContext(request,
              new HttpResponse(TextWriter.Null));
        }

        // SPContext is based on SPControl.GetContextWeb(), which looks here
        if (HttpContext.Current.Items["HttpHandlerSPWeb"] == null)
            HttpContext.Current.Items["HttpHandlerSPWeb"] = web;

    }

Upvotes: 1

Janis Veinbergs
Janis Veinbergs

Reputation: 6988

Ahh, the issue is now resolved after stepping already like 3rd time on the same rake.

It was happening in WebPart constructor (do not know if it plays a role, but inherited from Microsoft.SharePoint.WebPartPages.WebPart) and there the SPContext object was messed up.

Interesting that it works in my development environment.

Upvotes: 0

Related Questions