问题
Sometimes users require to change information in SharePoint list item that is not editable for them, for instance, a field that is hidden in edit form (in my case it was the records number).
I decided to create a small Windows GUI application that the administrator would run on the server and make the requested change. However, the simplest scenario to get an instance of a SPListItem
I found was:
- the admin enters the URL of the root site
- an
SPSite
ojbect is created, using the given URL:SPSite oSite=new SPSite(this.txtURL.text);
- admin enters the relative URL of the reqired web
- an
SPWeb
object is created asSPWeb oWeb = oSite.OpenWeb(this.txtWebUrl.text);
- a dropdown box is filled with all the list titles from
oWeb.Lists
- admin chooses a list from the listbox and enters the ID of the requested item;
- the needed
SPListItem
is found asoWeb.Lists[this.lstAllLists.selectedValue].GetItemById(this.txtItemId.value);
This is a very long path and administrators do no like to do the typing, clicking and waiting.
They would like to copy the URL of the listitem's display form (from the web browser or somebody's email), paste it into the update tool, then just click "Find it!".
I need hints for how this can be done.
I know I could probably parse the URL with a regex, since it's typically in the form of http://server/sites/[somesite]/[someweb/somesubweb]/lists/[somelist]/forms/dispform.aspx?ID=[123]
, but variations exist - for instance, http://[server]/[DocumentLibrary]/Forms/RenamedDispForm.aspx?ID=[1234]
has quite different structure than the first example.
So, the question is - is there some easy way to find an SPListItem
by it's URL? Reconstructing an SPContext
from the URL would be great.
EDIT: Just found out that it is possible to construct a valid SPSite
object by passing it a much longer URL:
Dim oSite as New SPSite("http://server/sites/site/Lists/test/DispForm.aspx?ID=136")
回答1:
Found a solution myself, the trick I did not know is that if you use a long URL in the constructor of the SPSite
, it gives you the SPWeb
object with the "deepest possible" address that matches your url (described here: http://msdn.microsoft.com/en-us/library/ms473155.aspx)
Still, I have to loop through all the lists to find out which list has the required URL. A simple function that does what I need:
UPDATE @ 2012-08-01:
- no need to loop through list collection, there is a
GetList
method inSPWeb
object. - instead of doing regex on URLs, one can use
HttpUtility.ParseQueryString
method
Hence the code now looks like this:
Function GetItemByUrl(spUrl As String) As SPListItem
'A site object does not care about additional parameters after site's URL
Dim oSite As New SPSite(spUrl)
'This returns the deepest SPWeb it can find
Dim oWeb As SPWeb = oSite.OpenWeb()
'here we parse out the ID parameter
Dim oUri As New Uri(spUrl)
'HttpUtility is from System.Web namespace
Dim oQueryParams As System.Collections.Specialized.NameValueCollection
oQueryParams = HttpUtility.ParseQueryString(oUri.Query)
Dim sParamval As String = oQueryParams.Get("ID")
If (sParamval.Length <= 0) Then
Return Nothing
End If
'This is how we get the list
Dim oCurrentList As SPList = oWeb.GetList(spUrl)
'And finally fetching the item
Dim oListItem As SPListItem = oCurrentList.GetItemById(Int32.Parse(sParamval))
Return oListItem
End Function
回答2:
I found myself doing something very similar (although with SPAuditEntry objects). The solution I came up with involved parsing the URL to figure out the SPSite and SPWeb.
Since SPSite takes a longer URL, you may also be able to open the right SPWeb as well (using site.OpenWeb()). I decided to use the SPSite.AllWebs.Names to figure out exactly which SPWeb the item was in (extracting parts of the URL and then doing a binary search against my collection of SPWeb names). I'm guessing you would then have to use SPWeb.Lists to determine which list or library it was in.
来源:https://stackoverflow.com/questions/1998271/finding-sharepoint-list-item-by-its-display-forms-url