User Tools

Site Tools


notes:sharepoint2010:ribbon

Ribbon in SharePoint 2010

Trim the ribbon buttons:

using System.Collections.Generic;
using Microsoft.SharePoint;
using WebRibbon = Microsoft.SharePoint.WebControls.SPRibbon;
 
...
 
// The GetRibbonButtonsToTrim() method returns IDs of the buttons to trim.
List<string> buttons = GetRibbonButtonsToTrim();
 
WebRibbon ribbon = WebRibbon.GetCurrent(this.Page);
foreach (string buttonId in buttons)
{
    ribbon.TrimById(buttonId);
}

Trim the AddDocument button on a list that has the 'Trim Uploads' custom content type and the current list item does not derive from DocumentSet:

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using WebRibbon = Microsoft.SharePoint.WebControls.SPRibbon;
 
SPList list = SPContext.Current.List;
SPListItem item = SPContext.Current.ListItem;
 
if (list != null)
{
    SPContentTypeCollection contentTypes = list.ContentTypes;
    SPContentType ct = contentTypes["Trim Uploads"];
    if (ct != null)
    {
        if (item == null || 
           (item != null && !item.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.DocumentSet)))
        {
            WebRibbon ribbon = WebRibbon.GetCurrent(this.Page);
            ribbon.TrimById("Ribbon.Documents.New.AddDocument");
            ScriptManager.RegisterClientScriptBlock(this, 
                this.GetType(), "WBS Trim Uploads", "var WBS_TrimUploads=true;", true);
        }
    }
}
<script language="javascript" type="text/javascript">
function HideAddDocumentButton() {
    if (typeof (WBS_TrimUploads) != "undefined" && WBS_TrimUploads == true) {
        $(function () {
            //find add new button and its container
            var tags = $(".ms-addnew");
            //loop through the found elements
            tags.each(function () {
                //hide each element
                $(this).css("display", "none");
            });
        });
    }
}
</script>
 
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        HideAddDocumentButton();
    });
</script>

Force a refresh of the Ribbon using JS:

RefreshCommandUI();
notes/sharepoint2010/ribbon.txt · Last modified: 2015/06/24 by admin