通过显示表单的 URL 查找 SharePoint 列表项

发布于 2024-08-16 16:55:12 字数 1306 浏览 12 评论 0原文

有时,用户需要更改 SharePoint 列表项中不可编辑的信息,例如,隐藏在编辑表单中的字段(在我的例子中是记录编号)。

我决定创建一个小型 Windows GUI 应用程序,管理员将在服务器上运行该应用程序并进行所请求的更改。然而,我发现获取 SPListItem 实例的最简单场景是:

  • 管理员输入根站点的 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);

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

我需要有关如何完成此操作的提示。

我知道我可能可以使用正则表达式解析 URL,因为它通常采用 http://server/sites/[somesite]/[someweb/somesubweb]/lists/[somelist]/forms/dispform 的形式。 aspx?ID=[123],但存在变体 - 例如,http://[server]/[DocumentLibrary]/Forms/RenamedDispForm.aspx?ID=[1234] 有结构与第一个示例完全不同。

所以,问题是 - 是否有一些简单的方法可以通过 URL 查找 SPListItem?从 URL 重建 SPContext 会很棒。

编辑:刚刚发现可以通过向其传递更长的 URL 来构造有效的 SPSite 对象:

Dim oSite as New SPSite("http://server/sites/site/Lists/test/DispForm.aspx?ID=136")

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花随流水 2024-08-23 16:55:12

我自己找到了一个解决方案,我不知道的技巧是,如果您在 SPSite 的构造函数中使用长 URL,它会为您提供带有“最深的”的 SPWeb 对象可能”与您的网址匹配的地址(此处描述:http://msdn.microsoft .com/en-us/library/ms473155.aspx)

不过,我必须循环遍历所有列表以找出哪个列表具有所需的 URL。一个简单的函数可以满足我的需要

更新@ 2012-08-01:

  • 无需循环列表集合,有一个 GetList 方法
    SPWeb 对象中。
  • 除了在 URL 上执行正则表达式之外,还可以使用
    HttpUtility.ParseQueryString 方法

因此代码现在如下所示:

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

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
    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
拍不死你 2024-08-23 16:55:12

我发现自己在做 一些非常相似的事情(尽管使用 SPAuditEntry 对象)。我提出的解决方案涉及解析 URL 以找出 SPSite 和 SPWeb。

由于 SPSite 需要较长的 URL,因此您也可以打开正确的 SPWeb(使用 site.OpenWeb())。我决定使用 SPSite.AllWebs.Names 来准确找出该项目所在的 SPWeb(提取部分 URL,然后对我的 SPWeb 名称集合进行二进制搜索)。我猜您必须使用 SPWeb.Lists 来确定它位于哪个列表或库中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文