martes, 21 de abril de 2015

¿Cómo anular la barra de herramientas a la vista predeterminada de una lista en SharePoint con C#?

SPView wpVista = oWebsite.Lists["Charter"].DefaultView;
SetToolbarType(wpVista, "None");
wpVista.Update();

La función SetToolbarType la pueden obtener de este post:

Dejo el código de ese post de Jalil:

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, nullas 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();
}

0 comentarios:

Publicar un comentario en la entrada