@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
91 lines • 4.17 kB
JavaScript
import { formatting } from './formatting.js';
import { odata } from './odata.js';
import { cli } from '../cli/cli.js';
const graphResource = 'https://graph.microsoft.com';
export const teams = {
/**
* Retrieve a team by its name.
* @param displayName Name of the team to retrieve.
* @throws Error if the team cannot be found.
* @throws Error when multiple teams with the same name and prompting is disabled.
* @returns The Teams team.
*/
async getTeamByDisplayName(displayName) {
const teams = await odata.getAllItems(`${graphResource}/v1.0/teams?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'`);
if (!teams.length) {
throw Error(`The specified team '${displayName}' does not exist.`);
}
if (teams.length === 1) {
return teams[0];
}
const resultAsKeyValuePair = formatting.convertArrayToHashTable('id', teams);
const result = await cli.handleMultipleResultsFound(`Multiple teams with name '${displayName}' found.`, resultAsKeyValuePair);
return result;
},
/**
* Retrieve the id of a team by its name.
* @param displayName Name of the team to retrieve.
* @throws Error if the team cannot be found.
* @throws Error when multiple teams with the same name and prompting is disabled.
* @returns The ID of the team.
*/
async getTeamIdByDisplayName(displayName) {
const teams = await odata.getAllItems(`${graphResource}/v1.0/teams?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'&$select=id`);
if (!teams.length) {
throw Error(`The specified team '${displayName}' does not exist.`);
}
if (teams.length === 1) {
return teams[0].id;
}
const resultAsKeyValuePair = formatting.convertArrayToHashTable('id', teams);
const result = await cli.handleMultipleResultsFound(`Multiple teams with name '${displayName}' found.`, resultAsKeyValuePair);
return result.id;
},
/**
* Retrieves a channel by its name in a Microsoft Teams team.
* @param teamId The ID of the team.
* @param name The name of the channel.
* @throws Throws an error if the specified channel does not exist in the team.
* @returns The Teams channel.
*/
async getChannelByDisplayName(teamId, name) {
const channelRequestOptions = {
url: `${graphResource}/v1.0/teams/${teamId}/channels?$filter=displayName eq '${formatting.encodeQueryParameter(name)}'`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json'
};
const response = await odata.getAllItems(channelRequestOptions);
// Only one channel can have the same name in a team
const channelItem = response[0];
if (!channelItem) {
throw Error(`The channel '${name}' does not exist in the Microsoft Teams team with ID '${teamId}'.`);
}
return channelItem;
},
/**
* Retrieves the channel ID by its name in a Microsoft Teams team.
* @param teamId The ID of the team.
* @param name The name of the channel.
* @returns The ID of the channel.
* @throws Throws an error if the specified channel does not exist in the team.
*/
async getChannelIdByDisplayName(teamId, name) {
const channelRequestOptions = {
url: `${graphResource}/v1.0/teams/${teamId}/channels?$filter=displayName eq '${formatting.encodeQueryParameter(name)}'&$select=id`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json'
};
const response = await odata.getAllItems(channelRequestOptions);
// Only one channel can have the same name in a team
const channelItem = response[0];
if (!channelItem) {
throw Error(`The channel '${name}' does not exist in the Microsoft Teams team with ID '${teamId}'.`);
}
return channelItem.id;
}
};
//# sourceMappingURL=teams.js.map