Sharepoint 2010客户端对象模型-获取当前列表的名称

Sharepoint 2010 Client Object Model - Get the name of the current list

本文关键字:列表 获取 2010 客户端 对象模型 Sharepoint      更新时间:2023-09-26

我正在尝试为Sharepoint 2010中的Ribbon菜单创建一个简单的自定义操作按钮。

我想保持它的泛型,所以没有硬编码的库名等

我怎样才能找到正在查看的当前列表的名称?我认为这是可能的,而不必从Url解析它。

多谢!

我花了一点时间去挖掘,但最后我找到了答案。你可以在Javascript中使用

来获取列表的Id:
//Get the Id of the list
var listId = SP.ListOperation.Selection.getSelectedList();

您会发现在SPContext类

SPList list = SPContext.Current.List;
string listTitle = list.Title;
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.aspx

要解析url,可以使用如下命令

VB。净

Private Function TryGetListName() As String
    If String.IsNullOrEmpty(Me.ListName) Then
        Dim path() As String = Me.Page.Request.Url.AbsolutePath.Trim("/"c).Split("/"c)
        Dim listName As String = String.Empty
        For i As Integer = 0 To path.Length - 1
            If path(i).ToLower = "lists" Then
                If i < path.Length - 1 Then
                    listName = path(i + 1)
                End If
                Exit For
            End If
        Next
        Return listName
    Else
        Return Me.ListName
    End If
End Function
c#

private string TryGetListName()
{
    if (string.IsNullOrEmpty(this.ListName)) {
        string[] path = this.Page.Request.Url.AbsolutePath.Trim('/').Split('/');
        string listName = string.Empty;
        for (int i = 0; i <= path.Length - 1; i++) {
            if (path[i].ToLower() == "lists") {
                if (i < path.Length - 1) {
                    listName = path[i + 1];
                }
                break;
            }
        }
        return listName;
    } else {
        return this.ListName;
    }
}
好运