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