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

时间:2011-09-17 23:26:48

标签: javascript sharepoint client

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

我想保持它的通用性,所以没有硬编码库名等。

如何查找当前正在查看的列表的名称?我想这可以在不必从Url解析它的情况下实现。

非常感谢!

2 个答案:

答案 0 :(得分:7)

需要一些挖掘,但我最终找到了答案。您可以使用以下方法在Javascript中获取列表的ID:

//Get the Id of the list
var listId = SP.ListOperation.Selection.getSelectedList();

答案 1 :(得分:0)

你会在SPContext类中找到它

SPList list = SPContext.Current.List;
string listTitle = list.Title;

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.aspx

要解析网址,您可以使用类似

的内容

VB.NET

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;
    }
}
祝你好运