SPContext - current list
I have a list with fields includng a URL field, with a list item has a URL field pointing to a custom .aspx page.
When the user click the URL, the aspx web page opens and I want to use the SPContext object to find out the current list and list item in which the user clicks.
I am finding that the SPContent does not have List and ListItem information initialized.
This is some test code I am trying. The List Id is zero, and the Item is always null. The currentSite object is initialized ok though.
Should this work, or do I need to include the ListId and ItemID in the URL field as arguments so that the invoked page knows where it came from?
The code below is a part of a custom .aspx page referred to by a URL field in a list item:
SPSite
currentSite =SPContext.Current.Web.Site;SPContext spContext =SPContext.Current;
ListBox1.Items.Add("List ID " + spContext.ListId.ToString());
SPListItem item = (SPListItem)SPContext.Current.Item;
if (item ==null)
{
ListBox1.Items.Add("item is null");
}
else
{
ListBox1.Items.Add(
"item is not null");}
Why do you expect the SPContext object to have a ListId if the page it is running is not a list form?
You are not running in the context of a list, or an item so you will not get these attributes.
What you want to do is to check the server variable for the referrer, and see where the user came from - that will give you a url that you can find the list by. Running a CAML query on the list will give you the item that has the link to the current page.
I developp a custom web service.I run the web service and specify the url of a library (myWebService.Url = http//btz-biztalk:90/Docs/Document%20Library/_vti_bin/service.asmx. In my web service i use the function
SPList spList = SPContext.current.List;
to return the my SPList of my library.
but i have this error
the server can't manage the request --> the object reference is not definate to an object instance.
Some help. Thanks
Ishai Sagi
<quote>
It would be smart for you to start a new thread and not ask here.
Anyway - you cannot use "SPContext.current" in a web service, since the web service is not running in the context of the list.
</quote>
Can you explain me how can i run a web service in the context of the list.
Thanks for your help.
Ok but it don't explain me totally that i can't use the method of the WSS v3
- SPContext.Current.List
- SPContext.Current.Item
- SPContext.Current.ListItem
This method exist at the same level of SPContext.Current.Web or SPContext.Current.Site.
Can you give me an explain, if it's possible, that i can't use this method and where can i use it.
Ok and i can use SPContext.Current.Web for a site and it's work.
Thanks.