设为首页 收藏本站
查看: 754|回复: 0

[经验分享] SharePoint开发中对ListViewWebPart的几个操作

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-25 12:18:12 | 显示全部楼层 |阅读模式
  在做一个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 DSC0000.gif ..
                ' 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/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118683-1-1.html 上篇帖子: Sharepoint学习笔记—ECMAScript对象模型系列-- 5、使用ECMA操作列表对象(list) 下篇帖子: SharePoint 2010 RMS 控制台根节点 证书 个人 证书 下没有 WMSvc开头的证书的解决方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表