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");

}

[2696 byte] By [LeroyMason] at [2008-1-1]
# 1


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.

IshaiSagi at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 2

Ishai,

It seems to me Leroy answered his own question: include the List and Item Id's in the url that is associated with the list item. That way the information is immediately available. Your approach should also work, but is more involved. Is there some reason you do not like Leroy's own answer to his question?

SharePointGuy at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 3
Actually, I didn't read his entire question, but jumped to the answer and didn't realize he could pass the parameters. If you are able to do it, that is best.
IshaiSagi at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 4

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

alucard064 at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 5

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.

IshaiSagi at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 6

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.

alucard064 at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 7

You don't.

Just read my article about common coding samples for sharepoint and see how to get a list object using code, without "running in context"

Common (and simple) coding tasks in sharepoint

IshaiSagi at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 8

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.

alucard064 at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...

SharePoint Products and Technologies

Site Classified