Saturday, June 20, 2009

Referencing SPWeb / SPSite throws error on layout page

On a custom application page, when you are trying to get to SPWeb or SPSite object from SPContext, you got following error

Error : Object Reference not sent to an instance of an object.

This can be because of :

-- First reason can be Web / Site Collection itself is missing. Make sure you can access the web's default page before calling the custom application page in its context.
If you are trying to get to ( http://localhost/sites/test/_layouts/client/customPage.aspx ) and this getting error, make sure you can get to ( http://localhost/sites/test/default.aspx )

-- If you using Form Authentication and trying to get the SPWeb / SPSite object from a page which is not authenticated ( usually these are the pages inheriting from UnSecuredLayoutPage class ), then you can get this error.

ASP.Net Zindabad . you need to use the SMTPClient to send email for all such pages which can be accessed without Authentication Login.aspx

Follow me on Twitter


Mangesh,  June 25, 2009 at 1:45 PM  

In general, what is the advantage of creating a feature and deploying it, as against adding a aspx page with .cs file under a folder in _layouts? This aspx may not have the sharepoint type look? If all u need is some processing using SharePoint OM without any UI then I thing its the best/easiest way. I've implemented a redirector typr of functionality where user can access documents using GUID. We expose the Doc GUID as its metadata and user can point to the doc using URL to the _layouts aspx page. in the aspx, we just make a call to SP OM, get the file using the GUID and redirect to the resultant URL of the list item.

Sandeep K Nahta June 25, 2009 at 3:56 PM  

You can do error handling also on the page and in case of error , you can display a nice message on the page using sharepoint type look and feel

Mangesh,  June 30, 2009 at 5:36 PM  

Thanks. So what is better from SharePoint perspective? a feature with DLL going to GAC or a folder with aspx and cs files int it? I know feature gets displayed as something installed in sharepoint UI, this page may not, but that should not be a huge thing.
Also how different this page would be if we compare it to a web part?