Add ribbon action and item context menu actions to site content type


I haven't done this for a while and I was searching for it for a while and not getting the correct answer.  So, once I found it, I have to put the information in here just to remind myself later if I have to do it again sometime in the future.

Below are functions to add ribbon action and context menu action to Document content type on the site collection level.  The actions requires edit list permission to see.

For the ribbon, you'll be passing in the command for the ribbon which looks something like:



private static void addRibbonActionToContentType(ClientContext clientContext, ContentType ct, string cmd, string actionName, string actionText)
        {
            // create the ribbon.
            UserCustomAction customAction = clientContext.Site.UserCustomActions.Add();
            customAction.Name = actionName;
            customAction.Location = "CommandUI.Ribbon.ListView";
            customAction.CommandUIExtension = cmd;
            customAction.Title = actionText;
            customAction.RegistrationId = ct.StringId;
            customAction.RegistrationType = UserCustomActionRegistrationType.ContentType;
            customAction.Sequence = 100;
            BasePermissions permission = new BasePermissions();
            permission.Set(PermissionKind.EditListItems);
            customAction.Rights = permission;
            customAction.Update();
        }

        private static void addContextMenuActionToContentType(ClientContext clientContext, ContentType ct, string actionName, string actionText, string actionUrl)
        {
            // create the ribbon.
            UserCustomAction customAction = clientContext.Site.UserCustomActions.Add();
            customAction.Name = actionName;
            customAction.Location = "EditControlBlock";
            customAction.Title = actionText;
            customAction.RegistrationId = ct.StringId;
            customAction.RegistrationType = UserCustomActionRegistrationType.ContentType;
            customAction.Sequence = 10001;
            customAction.Url = actionUrl;
            BasePermissions permission = new BasePermissions();
            permission.Set(PermissionKind.EditListItems);
            customAction.Rights = permission;
            customAction.Update();
        }

Comments

Popular posts from this blog

SharePoint 2013 App Details Page Error

SharePoint 2013 - Working with Display Template for Content Search Web Part

SharePoint 2013 Features Information List