SharePoint开发中对ListViewWebPart的几个操作

在做一个webPart的时候需要以编程的方式显示一个DocumentLibary。其中用到了ListViewWebPart。用到了一些代码,分享一下。

         Private  docLib  As  SPDocumentLibrary  =   Nothing
        
Private  view  As  SPView  =   Nothing
        
Private  Web  As  SPWeb  =   Nothing

 

1.初始化(其中的_docLibID和_viewID是属性的私有字段,这个通过GetToolParts方法设置的WebPartToolPart已经被赋值)

         Private   Sub  InitConfiguration()

            Web 
=  SPContext.Current.Site.OpenWeb(SPContext.Current.Web.ServerRelativeUrl)

            
Dim  list  As  SPList  =   Nothing
            
Try
                list 
=  Web.Lists(_docLibID)
                
If  list.BaseType  =  SPBaseType.DocumentLibrary  Then
                    docLib 
=  TryCast(list, SPDocumentLibrary)
                
End   If
            
           
' Set view
             If   Me ._viewID  =  Guid.Empty  Then
                view 
=  docLib.DefaultView
            
Else
                view 
=  docLib.Views( Me ._viewID)
            
End   If
        
End Sub


2.如何创建一个ListViewWebPart 的ToolBar(是根据ListViewWebPart的数据源有关)

只需创建一个SPContex,然后传入view,docLib及其他的信息即可,然后把这个SPContext赋给toolBar的RenderContex即可。

        Private   Sub  AddViewToolBar()
            
Dim  toolbar  As   New  ViewToolBar
            
Dim  context  As  SPContext  =  SPContext.GetContext( Me .Context, view.ID, docLib.ID, SPContext.Current.Web)

            toolbar.RenderContext 
=  context
            Controls.Add(toolbar)
        
End Sub

 

3.如何创建ListViewWebPart
我把创建的ListViewWebPart放在了一个Panel上面,代码中有个disableToobar这个是移除ListViewWebPart中的ToolBar(看第4,当我们指定ViewGuid ,默认的模式是说含有ToolBar的,这将导致工具栏显示在webpart页)
重要的是给ListName和ViewGui赋值

       Private   Function  renderExplorerView()  As  Panel
            
Dim  panel  As   New  Panel
            
Dim  wp  As   New  ListViewWebPart
            wp.ListName 
=  docLib.ID.ToString( " B " ).ToUpper()
            wp.ViewGuid 
=  view.ID.ToString( " B " ).ToUpper
            DisableToolbar(wp)
            wp.GetDesignTimeHtml()
            panel.Controls.Add(wp)
            
Return  panel
        
End Function

 

4.如何移除ListViewWebPart中的ToolBar
主要是对SPView中的SPView.ToolbarType设置。在MSDN上是这样描述SPView.ToolbarType
Standard —The most common type of toolbar, which is used, for example, in the All Items views for most lists, and which corresponds to Full Toolbar in the Web Part tool pane.
FreeForm —Used in Default.aspx and Web Part Pages and corresponds to Summary Toolbar in the Web Part tool pane.
None —No toolbar is used in the view, corresponding to No Toolbar in the Web Part tool pane.
我们的目的就是设置ToolbarType是None
VB

  Private   Sub  DisableToolbar( ByVal  lv  As  ListViewWebPart)
            
'  Extract view 
             Dim  ViewProp  As  System.Reflection.PropertyInfo  =  lv.[ GetType ]().GetProperty( " View " , System.Reflection.BindingFlags.NonPublic  Or  System.Reflection.BindingFlags.Instance)

            
Dim  spView  As  SPView  =  TryCast(ViewProp.GetValue(lv,  Nothing ), SPView)
            
Dim  txt  As   String   =  spView.SchemaXml
            
Dim  NodeProp  As  System.Reflection.PropertyInfo  =  spView.[ GetType ]().GetProperty( " Node " , System.Reflection.BindingFlags.NonPublic  Or  System.Reflection.BindingFlags.Instance)
            
Dim  node  As  XmlNode  =  TryCast(NodeProp.GetValue(spView,  Nothing ), XmlNode)
            
Dim  tBarNode  As  XmlNode  =  node.SelectSingleNode( " Toolbar " )

            
If  tBarNode  IsNot   Nothing   Then
                
Dim  typeNode  As  XmlAttribute  =  tBarNode.Attributes( " Type " )
                
'  make the contents empty so we realy remove the toolbar .. 
                 '  otherwise you might get a different type of toolbar popup when we have a 
                 '  Migrated site from 2.0 
                tBarNode.RemoveAll()
                
'  re-add the type attribute 
                tBarNode.Attributes.Append(typeNode)
                
'  finally set the toolbar to not show
                typeNode.Value  =   " None "
            
End   If
            
' This forces a refresh of the views internal xml or the node's cild nodes are not populated 
            Web.AllowUnsafeUpdates  =   True
            spView.Update()
            Web.AllowUnsafeUpdates 
=   False
        
End Sub

C#

private   static   void  DisableToolbar(ListViewWebPart lv)
{
 
//   Extract view 
   System.Reflection.PropertyInfo ViewProp  =  lv.GetType().GetProperty( " View "
    System.Reflection.BindingFlags.NonPublic 
|  System.Reflection.BindingFlags.Instance);

   SPView spView 
=  ViewProp.GetValue(lv,  null as  SPView;

   
string  txt  =  spView.SchemaXml;

   System.Reflection.PropertyInfo NodeProp 
=  spView.GetType().GetProperty( " Node "
     System.Reflection.BindingFlags.NonPublic 
|  System.Reflection.BindingFlags.Instance);

   XmlNode node 
=  NodeProp.GetValue(spView,  null as  XmlNode;
   XmlNode tBarNode 
=  node.SelectSingleNode( " Toolbar " );

   
if  (tBarNode  !=   null )
   {
      XmlAttribute typeNode 
=  tBarNode.Attributes[ " Type " ];
      
//  make the contents empty so we realy remove the toolbar ..
      
//  otherwise you might get a different type of toolbar popup when we have a 
      
//  Migrated site from 2.0
      tBarNode.RemoveAll();
      
//  re-add the type attribute
      tBarNode.Attributes.Append(typeNode);
      
//  finally set the toolbar to not show.
       typeNode.Value  =   " None " ;
    }
// This forces a refresh of the views internal xml or the node's cild nodes are not populated 
 spView.Update();
}

 

5.参考资料
Create custom ListViewWebPart
http://www.sharepointkings.com/2008/08/create-custom-listviewwebpart.html
d
Update ListViewWebPart to Remove or Hide Toolbar ToolbarType="None"
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/96aac2dd-29fc-4e63-8730-9d1adc01b826/

你可能感兴趣的:(SharePoint)