Sunday, July 1, 2012

Set ListViewWebPart Toolbar Type Programmatically

While adding a ListViewWebPart to web part page programmatically, had to set the List View's tool bar to "Summary View". After trying for hours found this code:

  //Method to Set the Tool bar type
        public static void SetToolbarType(SPView spView, String toolBarType)
        {
            spView.GetType().InvokeMember("EnsureFullBlownXmlDocument", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, spView, null, System.Globalization.CultureInfo.CurrentCulture);

            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 toolbarNode = node.SelectSingleNode("Toolbar");

            if (toolbarNode != null)
            {
                toolbarNode.Attributes["Type"].Value = toolBarType;

                // If the toolbartype is Freeform (i.e. Summary Toolbar) then we need to manually 
                // add some CAML to get it to work.
                if (String.Compare(toolBarType, "Freeform", true, System.Globalization.CultureInfo.InvariantCulture) == 0)
                {
                    string newItemString = "";
                    XmlAttribute positionNode = toolbarNode.OwnerDocument.CreateAttribute("Position");
                    positionNode.Value = "After";
                    toolbarNode.Attributes.Append(positionNode);

                    switch (spView.ParentList.BaseTemplate)
                    {
                        case SPListTemplateType.Announcements:
                            newItemString = "announcement";
                            break;
                        case SPListTemplateType.Events:
                            newItemString = "event";
                            break;
                        case SPListTemplateType.Tasks:
                            newItemString = "task";
                            break;
                        case SPListTemplateType.DiscussionBoard:
                            newItemString = "discussion";
                            break;
                        case SPListTemplateType.Links:
                            newItemString = "link";
                            break;
                        case SPListTemplateType.GenericList:
                            newItemString = "item";
                            break;
                        case SPListTemplateType.DocumentLibrary:
                            newItemString = "document";
                            break;
                        default:
                            newItemString = "item";
                            break;
                    }

                    if (spView.ParentList.BaseType == SPBaseType.DocumentLibrary)
                    {
                        newItemString = "document";
                    }

                    // Add the CAML
                    toolbarNode.InnerXml = @"<IfHasRights><RightsChoices><RightsGroup PermAddListItems=""required"" /></RightsChoices><Then><HTML><![CDATA[ <table width=100% cellpadding=0 cellspacing=0 border=0 > <tr> <td colspan=""2"" class=""ms-partline""><IMG   src=""/_layouts/images/blank.gif"" width=1 height=1 alt=""""></td> </tr> <tr> <td class=""ms-addnew"" style=""padding-bottom: 3px""> <img src=""/_layouts/images/rect.gif"" alt="""">&nbsp;<a class=""ms-addnew"" ID=""idAddNewItem"" href=""]]></HTML><URL Cmd=""New"" /><HTML><![CDATA["" ONCLICK=""javascript:NewItem(']]></HTML><URL Cmd=""New"" /><HTML><![CDATA[', true);javascript:return false;"" target=""_self"">]]></HTML><HTML>Add new " + newItemString + @"</HTML><HTML><![CDATA[</a> </td> </tr> <tr><td><IMG src=""/_layouts/images/blank.gif"" width=1 height=5 alt=""""></td></tr> </table>]]></HTML></Then></IfHasRights>";
                }

                spView.Update();

            }
        } 
    }


Above script credit goes to Jalil Sear

Call the above method wherever required:

                //Create the object SPLimitedWebPart Manager
                SPLimitedWebPartManager WebPartMgr = web.GetLimitedWebPartManager("Home.aspx", PersonalizationScope.Shared);
 
              //Add Client Notes List view web part
                SPList ProjectNotesList = web.Lists["Project Notes"];
                ListViewWebPart ProjectNotesListViewWP = new ListViewWebPart();
                 //Set the properties of the webpart
                ProjectNotesListViewWP.ChromeType = PartChromeType.None;
                ProjectNotesListViewWP.Title = "Client Notes";
                ProjectNotesListViewWP.ListName = ProjectNotesList.ID.ToString("B").ToUpper();
                ProjectNotesListViewWP.ViewGuid = ProjectNotesList.DefaultView.ID.ToString("B").ToUpper();
                //Define the zone in which webparts need to be added
                WebPartMgr.AddWebPart(ProjectNotesListViewWP, "Left", 1);
                
                //Set the Tool bar
                System.Reflection.PropertyInfo ProjectNotesViewProp = ProjectNotesListViewWP.GetType().GetProperty("View", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                spView = ProjectNotesViewProp.GetValue(ProjectNotesListViewWP, null) as SPView;
                Helper.SetToolbarType(spView, "Freeform");

                WebPartMgr.SaveChanges(ProjectNotesListViewWP);



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

3 comments :

  1. Thank you. After much searching this one works perfectly.

    ReplyDelete
  2. Thank you very much. You saved a lot of my time.

    ReplyDelete
  3. In SP2016, it looks like ViewGuid property must be assigned to the web part after adding the web part.

    ReplyDelete

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...