locked
An unhandled exception has occurred. RRS feed

  • Question

  • Hi,

    I'm trying to access the Sharepoint SPContext from a Handler which I have registered with IIS . When ever I'm trying to make a request for my handler It's giving me the below mentioned warning and it's not able to get the SPContext (SpContext = null)

    This is the code I'm using in my Handler --->

        public bool IsReusable
        {
          get { return true; }
        }
    
    
        public void ProcessRequest(HttpContext context)
    {
    SPSecurity.RunWithElevatedPrivileges(() =>
    {GetData();});
    }


    private string GetData() { string rootsite = String.Empty; try { rootsite = "http://" + SPContext.Current.Site.HostName + "/home"; } catch (Exception exe) { ILogger logger = new SharePointLogger(); logger.LogToOperations(string.Format("Exception -{0}--{1}",exe.Message,exe.StackTrace), EventLogEntryType.Information); } return string.Format("Hi"); }
    This is the error which is getting logged in my event Log --->

     

    Event code: 3005
    Event message: An unhandled exception has occurred.
    Event time: 12/06/2010 5:04:51 PM
    Event time (UTC): 12/06/2010 7:04:51 AM
    Event ID: b8a9bf37f9504d289365a75e7ebb76c9
    Event sequence: 110
    Event occurrence: 20
    Event detail code: 0
     
    Application information:
        Application domain: /LM/W3SVC/777599991/ROOT-1-129207991821506363
        Trust level: WSS_Medium
        Application Virtual Path: /
        Application Path: C:\inetpub\wwwroot\wss\VirtualDirectories\Resolute.srv.test.dir.Resolute.com80
        Machine name: WSAPP3550
     
    Process information:
        Process ID: 3984
        Process name: w3wp.exe
        Account name: Domain\RT947
     
    Exception information:
        Exception type: NullReferenceException
        Exception message: Object reference not set to an instance of an object.
     
    Request information:
        Request URL: http://Resolute.srv.test.dir.Resolute.com/home/general/globalmenu.asp
        Request path: /home/general/globalmenu.asp
        User host address: fe80::cdeb:7d93:ae78:e2c5WSAPP3550
        User: Domain\VT73f
        Is authenticated: True
        Authentication Type: NTLM
        Thread account name: Domain\RT947
     
    Thread information:
        Thread ID: 16
        Thread account name: Domain\RT947
        Is impersonating: True
        Stack trace:    at Resolute.SharePoint.WebControls.GlobalHttpHandler.GetMyLinks()
       at Resolute.SharePoint.WebControls.GlobalHttpHandler.<>c__DisplayClass1.<ProcessRequest>b__0()
       at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
       at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
       at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
       at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
       at Telstra.SharePoint.WebControls.GlobalHttpHandler.ProcessRequest(HttpContext context)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
     
     
    Custom event details:

     

    Kindly help me out as to how should I resolve this issue.

    Thanks,

    Resolute

     

    • Moved by Mike Walsh FIN Saturday, June 12, 2010 7:53 AM own code question (From:SharePoint - Setup, Upgrade, Administration and Operation (pre-SharePoint 2010))
    Saturday, June 12, 2010 7:49 AM

Answers

  • Hi,

    Follow these steps:

    Step 1: Your class must implement the following interfaces.

    public class YourHttpModule : IHttpModule, IRequiresSessionState

    Step 2: Add Reference Windows Sharepoint

    using System;
    using System.Web;
    using System.Web.SessionState;
    using Microsoft.SharePoint;
    using System.Linq;
    using System.Collections.Generic;

    Step 3: Register the assembly in the GAC

    Step 4: Copy the file to the folder C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin

    Step 5: Write the following lines of code to capture the SPContext

            public void Init(HttpApplication context)
            {
                if (context == null)
                    return;
                context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
            }

    Step 6: Capture o evento PreRequestHandlerExecute

            void context_PreRequestHandlerExecute(object sender, EventArgs e)
            {
                HttpContext ctx = ((HttpApplication)sender).Context; //Capture the Request/Response of your page
                SPWeb webSite = SPContext.Current.Web;
                SPSite site = SPContext.Current.Site;

                <YOUR CODE>
            }

     Step 7: Register tag in Web.Config (C:\Inetpub\wwwroot\wss\VirtualDirectories\80\)

        <httpModules>
               <add name="moduleName" type="Namespace.ClassName, AssemblyName" />
        </httpModules>

     Step 8: IISReset

    With this you will capture the SPContext

    Best Regards
    Marco A Gerônimo


    Marco
    Saturday, June 12, 2010 7:52 PM