我想在Sharepoint 2010的功能区菜单中创建一个简单的自定义操作按钮。
我希望它是通用的,不需要硬编码库名称等信息。
如何找到当前正在查看的列表的名称? 我认为这是可能的,而无需从Url解析它。
非常感谢!
我希望它是通用的,不需要硬编码库名称等信息。
如何找到当前正在查看的列表的名称? 我认为这是可能的,而无需从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.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;
}
}
祝你好运