开发者

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

I'm trying to create a simple custom action button for the Ribbon menu in Sharepoint 2010.

I want to keep it generic, so no hard coding of library names etc.

How can I find ou开发者_运维知识库t the name of the current list being viewed? I would imagine that this is possible without having to parse it from the Url.

Many thanks!


It took a bit of digging, but I found the answer in the end. You can get the Id of the list in Javascript by using:

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


You'll find that in the SPContext class

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

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

To parse the url you can use something like this

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

Good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜