1
votes

Has anyone found a way to make their context menu items conditional? (contribution IDs like ms.vss-work-web.work-item-context-menu)

I want to add a work item context menu item but I only want to show it for some workitem types.

1
If the provided answer solved your problem, you should mark it as accepted. This helps others to find a similar solution. - Flex

1 Answers

0
votes

As contribution you use an action like this:

{
    "id": "action-id",
    "type": "ms.vss-web.action",
    "description": "Context menu action",
    "targets": [
        "ms.vss-work-web.work-item-context-menu",
        "ms.vss-work-web.query-result-work-item-menu",
        "ms.vss-work-web.work-item-toolbar-menu",
        "ms.vss-work-web.backlog-item-menu"
    ],
    "properties": {
        "uri": "yourpagehere.html"
    }
}

Your action provider should just return an empty menu item for the work items not relevant:

function getTypesFromContext(context: any): string[] {
    // Not all areas use the same format for passing work item type names.
    // "workItemTypeName" for Query preview
    // "workItemTypeNames" for backlogs
    // "workItemType" for boards
    let types = context.workItemTypeNames;
    if (!types && context.workItemType) {
        // Boards only support a single work item
        types = [context.workItemType];
    }

    if (!types && context.workItemTypeName) {
        // Query wi preview
        types = [context.workItemTypeName];
    }

    return types;
}

const action = {
    getMenuItems: (context) => {
        const mi = {
            text: "sometext",
            title: "sometitle",
            groupId: "modify",
            icon: "someicon.png",
            action: (actionContext) => {
                // someaction
            }
        } as IContributedMenuItem;

        const types = getTypesFromContext(context);

        if (types.every((type) => [ <<Your relevant types here>> ].indexOf(type) >= 0)) {
            return [mi];
        }
        return [] as IContributedMenuItem[];
    }
} as IContributedMenuSource;

VSS.register(VSS.getContribution().id, action);