There are some things you need to know as a SharePoint developer...
Creating a list
When you create a list using the browser you get to specify the name for the list. Basically this means both the url part as the title. Not all characters are allowed in a URL so SharePoint will just filter them out of the url part WITHOUT NOTICE !
The example above will create a list at location http://moss/My%20List1d with title 'My List??1..d'.
When you create a list using the Object Model you also specify the url part and title as one parameter and invalid characters get stripped from the url part.
Renaming a list
When a list is created it cannot be renamed. Well, you can change the title but not the url part. I use this technique quite often as follows:
- Create a list named 'kb'
- Rename the list to 'Knowledge Base'
This way the url part remains short while your users see a more descriptive title. I always tend to avoid spaces and exotic characters in the url part. Too bad they don't allow this separation at list creation time...
Caveat
There could be some confusion when you have the following situation:
- You have a list named 'Questions' (http://moss/Lists/Questions)
- You create a new list named 'Questions ???'
Guess the outcome ? The new list contains some illegal URL characters which will be stripped. However there exists a list with the same url part already...
Answer: The new list is created successfully at the following location: http://moss/Lists/Questions1
The '/Lists' segment
If you notice the URL of some lists and libraries you might notice the difference. Some of them have the '/Lists' segment and others dont.
In general:
- Lists with files: root location of the web. Example: http://moss/MyDocLib
- Other lists: '/Lists' segment. Example: http://moss/Lists/MyAnnouncements
Exceptions (found so far):
- A Site Directory 'Sites' List (type: enhanced Links List) lives in the root location of the Site Directory web
Get the instance of a list in code
When you write SharePoint code and you need to get the instance to the list you have some options:
1. Using SPFolder.ParentListId
This only works for libraries since the list has to support folders. You can get to the root folder of the document library and then get the parent list id as Guid. In turn this id can be used to get to the SPList instance.
Possibilities:
- web.Folders["url part"].ParentListId
- web.Folders[web.Url + "/url part"].ParentListId
- web.Folders[web.ServerRelativeUrl + "/url part"].ParentListId
- web.Folders[site.MakeFullUrl("url part")].ParentListId
Instead of using web.Folders[url] you can also use web.GetFolder(url)
2. Using SPWeb.Lists[title]
This works for all types of lists and uses the title or list id. The latter can be used in combination with the SPFolder.ParentListId method.
Possibilities:
- web.Lists["title"]
- web.Lists[listId]
3. Using SPWeb.GetList(url)
This works for all types of lists and uses the absolute or relative url. It cannot the url part in itself so if you only have that it must be made into a absolute or relative url.
Possibilities:
- web.GetList[web.Url + "/url part"]
- web.GetList[web.ServerRelativeUrl + "/url part"]
- web.GetList[site.MakeFullUrl("url part")]
-
4. Using SPWeb.GetListFromUrl(url)
This works for all types of lists and uses the absolute or relative url to a form. (not just any file !)
Possibilities:
- web.GetListFromUrl["url part/Forms/AllItems.aspx"]
- web.GetListFromUrl[web.Url + "/url part/Forms/AllItems.aspx"]
- web.GetListFromUrl[web.ServerRelativeUrl + "/url part/Forms/AllItems.aspx"]
- web.GetListFromUrl[site.MakeFullUrl("url part/Forms/AllItems.aspx")]
-
5. Using SPWeb.GetListFromWebPartPageUrl(url)
Seems to be identical to the above method. It doesn't seem to work with a Web Part Page url so I'm not sure what additional features it offers.
Possibilities: same as 4.
Alternative
If you're in the context of SharePoint and displaying an item or form in a list or library you can use the following:
Conclusion
It's possible to store a reference to a specific list using the title, the url or url part, or the ID in combination with the web url. Since the list title is subject to change by anyone with sufficient permissions You shouldn't rely on it to find the specific list. The title should be used for display but the url part or ID should be used to work with behind the scene.
Also, nothing prevents you from having two lists with the same title on the same web. This might be confusing but can be prevented by user education or by providing the url along with the title.
Think of this when designing your web parts, win apps, etc.