Answered by:
Get URL for List and ListItem

Question
-
Hi,
I have a SharePoint List (SPList) and need to provide the URL for the list. It should be possible to copy this URL to the browsers address field and navigate to the corresponding Details or Overview View (or default view) of the list.
Additionally, I have a List Item object (SPListItem) and need the URL to directly navigate to the DispForm.aspx of this item.
I tried it like this:
string listURL = ... + "/Lists/" + myList.Title;
string itemURL = listURL + "/DispForm.aspx?ID=/" + id;
The URLs are correct as long as the "Internal Name" of the list is the same as the displayed name of the list. But in SharePoint it is possible to add a List-Template with no blanks in the name (e.g. 'MyList'), but later rename it and include a blank (e.g. 'My List'). In this case the listURL I retrieve with the above code snippet does not work anymore! For SharePoint only a URL with the list name without blanks is existent. In short: the URL does not include the blank for the list in the url but myList.Title does!!
So my question is, how can I get a URL that directly leads to my list's Details view (or default view) and the URL that leads directly to the list items "DispForm.aspx"??Wednesday, September 23, 2009 5:26 PM
Answers
-
Hi,
It seems you need to get correct ListItem URL from ListItem’s property, and thanks for all helpful suggestions.
In this situation, would you please try using List’s Form property instead of “SubString”, code like this:
---------------------------------------------------------------------------------------------
string itemURL = yourweb.Url+"/"+yourlist.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url + "?ID=" + item.ID;
---------------------------------------------------------------------------------------------
Then you will actual retrieve the correct path to the DispForm.aspx page.
Hope this sample can help.
Best Regards,
-Aaron
- Marked as answer by Chengyi Wu Friday, October 2, 2009 8:48 AM
Wednesday, September 30, 2009 7:29 AM
All replies
-
I'm actually currently working on something similar where I need to start a workflow on a list Item.
If I understand correctly this is the code you're looking for:
string subsite = "name of subsite";
string listName = "name of list";
string itemID= "whatever your item ID is"
SPWeb web = SPContext.Current.Web; // this navigates to the root of the web you are currently in
SPList list = web.Webs[subsite].Lists[listName]; // we then navigate to the correct subsite and navigate to the correct list
SPListItem item = list.GetItemById(int.Parse(itemID)); // we then navigate to the correct list item
string itemURL = item.Url; // we return the item url
edit: fixed some typos
KevinWednesday, September 23, 2009 6:27 PM -
Hi,
the problem here is, that the Url-Properties of the API don't provide the entire URL, which means:
http://www.mydomain.com/moss/site/lists/mylist/overview.aspx or
http://www.mydomain.com/moss/site/lists/mylist/displayform.aspx?ID=10
The first URL would lead to the Overview-View of the list with the name "mylist" and the second would lead to the DisplayForm of the ListItem with the ID=10.
I am missing something like the URL to "http://www.mydomain.com/moss/site/lists/mylist/" then it would be possible to concatenante the rest of it.
Your solution provides a way to get the URL for a specific list item, which is good, but how would I come to the overview view of the list itself or later navigate to the DisplayForm.aspx of the List Item?
Faced with this problem, I tried to get/build the URL with my code snippet. But as described it gives me a wrong name in the URL if the Title of the list has been changed within SharePoint (example without blanks in the name and with blanks).Wednesday, September 23, 2009 7:13 PM -
You can get default list view url like this
SPContext.Current.Web.Url + _list.DefaultViewUrl
chnage of name should not have effect- Proposed as answer by orange juice jones Sunday, June 17, 2012 10:59 AM
Thursday, September 24, 2009 8:12 AM -
list item Url you can get like this
_item.Url.Substring(0,_item.Url.LastIndexOf("/"
i've tried any other way and didn't find anything, this is the fastest way, for nowThursday, September 24, 2009 8:19 AM -
This solution has a little downside, at least on my system. The DefaultViewUrl property starts with a part that is also included at the end the SPContext.Current.Web.Url.
What do you think about the solution with _list.RootFolder? I could concatenate it like
string myURL = SPContext.Current.Web.Url + "/" + _list.RootFolder;
Redirecting to this URL would finally open the default view of my list as well, as SharePoint enhances the missing part of the view.
Is there a downside of the RootFolder-Property?Thursday, September 24, 2009 4:16 PM -
Hi,
this exmaple just extracts the the entire URL but the last "/" and then adds the DispForm with the appropriate ID of the item.
The Problem is the _item.Url. If this property is not correct, I will redirected to the wrong item. The other problem is, that I don't know the URL of the Overview-Page of my list which contains all the list items.Thursday, September 24, 2009 4:18 PM -
Hi,
It seems you need to get correct ListItem URL from ListItem’s property, and thanks for all helpful suggestions.
In this situation, would you please try using List’s Form property instead of “SubString”, code like this:
---------------------------------------------------------------------------------------------
string itemURL = yourweb.Url+"/"+yourlist.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url + "?ID=" + item.ID;
---------------------------------------------------------------------------------------------
Then you will actual retrieve the correct path to the DispForm.aspx page.
Hope this sample can help.
Best Regards,
-Aaron
- Marked as answer by Chengyi Wu Friday, October 2, 2009 8:48 AM
Wednesday, September 30, 2009 7:29 AM -
Hi Aaron, thanks. This information was really helpful.
Sherazad
Monday, June 2, 2014 8:14 PM