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

nai*_*sts 0 sharepoint sharepoint-api splistitem

有时,用户需要更改SharePoint列表项中不可编辑的信息,例如,以编辑形式隐藏的字段(在我的情况下,它是记录编号).

我决定创建一个小型Windows GUI应用程序,管理员可以在服务器上运行该应用程序并进行所请求的更改.但是,获取SPListItem我发现的实例的最简单方案是:

  • 管理员输入根站点的URL
  • 一个SPSiteojbect创建,使用给定的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);

这是一条很长的路径,管理员不喜欢打字,点击和等待.
他们想复制listitem显示表单的URL(从Web浏览器或某人的电子邮件中),将其粘贴到更新工具中,然后只需单击"Find it!"即可.

我需要提示如何做到这一点.

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

所以,问题是 - 是否有一些简单的方法来找到SPListItem它的URL?SPContext从URL 重建一个很好.

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

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

nai*_*sts 5

我自己找到了一个解决方案,我不知道的诀窍是,如果你在构造函数中使用了一个长URL SPSite,它会为你SPWeb提供与你的url匹配的"尽可能最深"的地址(在这里描述:http:// msdn .microsoft.com/zh-CN/library/ms473155.aspx)

不过,我必须遍历所有列表以找出哪个列表具有所需的URL.一个简单的功能,可以满足我的需求:

更新@ 2012-08-01:

  • 无需遍历列表集合,对象中有一个GetList方法SPWeb.
  • 可以使用HttpUtility.ParseQueryString方法而不是对URL进行正则表达式

因此代码现在看起来像这样:

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
Run Code Online (Sandbox Code Playgroud)