通过其显示表单的URL查找SharePoint列表项 [英] Finding SharePoint list item by its display form's URL
问题描述
有时用户需要更改SharePoint列表项中对他们不可编辑的信息,例如,以编辑形式隐藏的字段(在我的情况下是记录编号).
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).
我决定创建一个小的Windows GUI应用程序,管理员可以在服务器上运行该Windows GUI应用程序并进行请求的更改.但是,获取我发现的SPListItem
实例的最简单方案是:
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:
- 管理员输入根站点的URL
- 使用给定的URL创建
SPSite
对象:SPSite oSite=new SPSite(this.txtURL.text);
- 管理员输入所需网站的相对URL
-
SPWeb
对象创建为SPWeb oWeb = oSite.OpenWeb(this.txtWebUrl.text);
- 一个下拉框填充了
oWeb.Lists
中的所有列表标题
- 管理员从列表框中选择一个列表,然后输入所请求项目的ID;
- 所需的
SPListItem
被发现为oWeb.Lists[this.lstAllLists.selectedValue].GetItemById(this.txtItemId.value);
- 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);
这是一条很长的路,管理员不喜欢键入,单击并等待.
他们想复制列表项的显示表单的URL(从Web浏览器或某人的电子邮件中),将其粘贴到更新工具中,然后单击查找!".
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.
我知道我可以用正则表达式来解析URL,因为它通常以http://server/sites/[somesite]/[someweb/somesubweb]/lists/[somelist]/forms/dispform.aspx?ID=[123]
的形式出现,但是存在变体-例如,http://[server]/[DocumentLibrary]/Forms/RenamedDispForm.aspx?ID=[1234]
的结构与第一个示例完全不同.
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.
所以,问题是-是否有一些简单的方法可以通过URL查找SPListItem
?从URL重构SPContext
会很棒.
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.
刚刚发现可以通过传递更长的URL来构造有效的SPSite
对象:
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")
推荐答案
我自己找到了一个解决方案,我不知道的诀窍是,如果在SPSite
的构造函数中使用长网址,它会为您提供SPWeb
对象,该对象具有与您的网址相匹配的尽可能深的"地址(此处描述: http://msdn.microsoft.com/en-us/library/ms473155.aspx )
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)
仍然,我必须遍历所有列表以找出哪个列表具有所需的URL.一个满足我需要的简单函数:
2012年1月1日更新:
UPDATE @ 2012-08-01:
- 无需遍历列表集合,有一个
GetList
方法 在SPWeb
对象中. - 除了可以在URL上进行正则表达式外,还可以使用
HttpUtility.ParseQueryString
方法
- 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
这篇关于通过其显示表单的URL查找SharePoint列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!