通过显示表单的 URL 查找 SharePoint 列表项 [英] Finding SharePoint list item by its display form's URL

查看:13
本文介绍了通过显示表单的 URL 查找 SharePoint 列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时用户需要更改无法编辑的 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 应用程序,管理员将在服务器上运行该应用程序并进行请求的更改.但是,我发现获取 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
  • 一个 SPSite 对象被创建,使用给定的 URL:SPSite oSite=new SPSite(this.txtURL.text);
  • admin 输入所需网站的相对 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 as SPWeb 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 as oWeb.Lists[this.lstAllLists.selectedValue].GetItemById(this.txtItemId.value);

这是一条很长的路,管理员不喜欢打字、点击和等待.
他们想复制列表项显示表单的 URL(来自网络浏览器或某人的电子邮件),将其粘贴到更新工具中,然后只需点击查找!".

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的构造函数中使用了很长的URL,它为您提供 SPWeb 对象,其中包含与您的 url 匹配的最深可能"地址(此处描述: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-08-01:

UPDATE @ 2012-08-01:

  • 不需要遍历列表集合,有一个GetList方法在 SPWeb 对象中.
  • 除了对 URL 执行正则表达式之外,还可以使用HttpUtility.ParseQueryString 方法
  • no need to loop through list collection, there is a GetList method in SPWeb 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆