1

I want to change the Document Library's webpart property 'Toolbar Type' from 'Full Toolbar' to 'Show Toolbar' programmatically.

enter image description here

I am working on Office 365 sandbox solution.I am creating document library & it's view programmatically. I have tried following:

myView.Toolbar = @"<Toolbar Type='FreeForm'/>"; 

// I am not sure that it is 'FreeForm' or not for 'Show Toolbar'

I want to achieve this type of view for my library. I want to show toolbar on library programmatically.

enter image description here

If any other solution is possible to get toolbar on Document Library programmatically then provide here.

2 Answers 2

2

This solution worked for me

    private static void SetToolbarType(SPView spView, string toolBarType)
{
    spView.GetType().InvokeMember("EnsureFullBlownXmlDocument",
    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,
    null, spView, null, System.Globalization.CultureInfo.CurrentCulture);
    PropertyInfo nodeProp = spView.GetType().GetProperty("Node",
    BindingFlags.NonPublic | 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();
}
1
  • Did you try it on Sandbox Solution ??.. I have tried it but showing error on 'BindingFlags'. Commented Feb 21, 2015 at 12:10
1

If you just want to change the toolbar type.Here is simple code for you

Code Block lMgr =OpenWebPartManager("Default.Aspx");

// Get web Part

wp = lMgr.WebParts[wp.ID] as WebPart;

ListViewWebPart lv = (ListViewWebPart)wp;

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

// This forces a refresh of the views internal xml or the node's cild nodes are not populated

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)

{

tBarNode.Attributes["Type"].Value = "None";

spView.Update();

}
1
  • Is 'lMgr' object of SPLimitedWebprtManager ?, if yes then it's not working on Sandbox Solution.And also 'BindingFlags' shows error. Commented Feb 21, 2015 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.