0

I have multiple items in SharePoint list, and I have ribbon control in the ribbon, I want this ribbon to be enabled only if the item selected is of specific content type.

I want only the control to be available if I select announcements content type, but the list has also other content types.

I tried this code: <CustomAction ShowInLists="TRUE" Id="TestAction1" RegistrationType="ContentType" RegistrationId="0x01070100859DD43543453345" Location="CommandUI.Ribbon">

Any help is appreciated.

2
  • jakejacobsen.net/2010/06/17/…
    – Nikhil J
    Commented Sep 27, 2014 at 2:40
  • Thanks Nikhil, that will work if the content type is the only content type in that list, if you add other content types, they will have the custom action as well. I want to be specific on this content type. Commented Sep 27, 2014 at 4:02

1 Answer 1

1

I have a written a blog post showing how to enable/disable ribbon button based on whether the list supports versioning or not. You can tweak the code to check the content type of the selected item. The content type of a selected item can be found using var ctid = listItem.get_item("ContentTypeId").toString(); In the Success method you will need to check the content type of the selected item with predefined content type and update a boolen value. Code sample from the blog:

<CommandUIHandlers>
<CommandUIHandler
Command="ExportVersionHistory"
CommandAction="javascript:alert('Enable disable demo');"
EnabledScript="javascript:EnableDisableButton();"/>
</CommandUIHandlers>
The js file can be specified in CustomAction like this:

<CustomAction Id="Ribbon.Library.Actions.Scripts"
Location="ScriptLink" ScriptSrc="/_Layouts/Demo/EnableDisable.js" />

In the js file we will have following functions:

function listSuccess(sender, args) {
    this.versioningEnabled = this.list.get_enableVersioning();
    RefreshCommandUI();
}


function listFailed(sender, args) {
    alert('request failed ' + args.get_message() + 'n' + args.get_stackTrace());
}
function EnableDisableButton() {
    var items = SP.ListOperation.Selection.getSelectedItems();
    var ci = CountDictionary(items);
    if (ci > 0) {
        var context = SP.ClientContext.get_current();
        this.list = context.get_web().get_lists().getById(SP.ListOperation.Selection.getSelectedList());
        if (this.versioningEnabled === undefined) {
            context.load(this.list);
            context.executeQueryAsync(Function.createDelegate(this, this.listSuccess), Function.createDelegate(this, this.listFailed));
        }
        return this.versioningEnabled;
    }
}
3
  • Hi Nadeem, thanks for your answer, but how will I return true or false if the content type is not the one I want? the context.executeQueryAsync will be executed and passed to the next step before my function gets executed, meaning that EnableDisableButton will finish before the listSuccess finish Commented Sep 27, 2014 at 13:33
  • You may need to see if that issue can be resolved using deferreds. Commented Sep 27, 2014 at 14:35
  • Hi Nadeem, Do you have an example for that? Commented Oct 1, 2014 at 0:21

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.