Get current list/library from http handler
-
16-10-2019 - |
Question
I’m trying to write a Http Handler to generate rss xml (based on the CKS code).
I want to be able to get the list/library that the end point of the url is referring to.
For example if my Url is:
I want to be able to get the pages library of the 'press releases' web.
On a side note if I'm going about this in the wrong way please let me know.
Update 1
It may be easier for me to show my code (incase of retardation)
public void ProcessRequest(HttpContext context)
{
try
{
SPList list = SPContext.Current.Web.GetListFromUrl(context.Request.Path);
}
catch (Exception ex)
{
context.Response.Write("Error getting list from spcontext.current.web.GetListFromUrl: " + ex.Message + "<br />");
}
try
{
SPList list = SPContext.Current.List;
context.Response.Write("List title: " + list.Title.ToString() + "<br />");
}
catch (Exception ex)
{
context.Response.Write("Error getting list from spcontext.current.list: " + ex.Message + "<br />");
}
try
{
context.Response.Write("List Title from SPConext: " + SPContext.Current.List.Title.ToString());
}
catch (Exception ex)
{
context.Response.Write("Error getting list from spcontext.current.list.title.tostring: " + ex.Message + "<br />");
}
}
The error messages displayed are...
- Error getting list from spcontext.current.web.GetListFromUrl: Cannot complete this action. Please try again.
- Error getting list from spcontext.current.list: Object reference not set to an instance of an object.
- Error getting list from spcontext.current.list.title.tostring: Object reference not set to an instance of an object.
Solution 2
I've managed to get the list name by string manipulation.
OTHER TIPS
You have access to SPContext within a HTTP handler, so you should be able to use:
SPContext.Current.List
If for some reason that can't be used, this should get you there:
SPContext.Current.Web.GetListFromUrl()
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange