@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
77 lines • 4.27 kB
JavaScript
import request from '../request.js';
import { formatting } from './formatting.js';
import { cli } from '../cli/cli.js';
import config from '../config.js';
import { odata } from './odata.js';
const graphResource = 'https://graph.microsoft.com';
export const spe = {
/**
* Get all container types.
* @param spoAdminUrl The URL of the SharePoint Online admin center site (e.g. https://contoso-admin.sharepoint.com)
* @returns Array of container types
*/
async getAllContainerTypes(spoAdminUrl) {
const requestOptions = {
url: `${spoAdminUrl}/_vti_bin/client.svc/ProcessQuery`,
headers: {
accept: 'application/json;odata=nometadata'
},
responseType: 'json',
data: `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${config.applicationName}" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="46" ObjectPathId="45" /><Method Name="GetSPOContainerTypes" Id="47" ObjectPathId="45"><Parameters><Parameter Type="Enum">1</Parameter></Parameters></Method></Actions><ObjectPaths><Constructor Id="45" TypeId="{268004ae-ef6b-4e9b-8425-127220d84719}" /></ObjectPaths></Request>`
};
const json = await request.post(requestOptions);
const response = json[0];
if (response.ErrorInfo) {
throw new Error(response.ErrorInfo.ErrorMessage);
}
const containerTypes = json[json.length - 1];
// Format the response to remove CSOM GUIDs and convert them to real GUIDs
containerTypes.forEach(ct => {
delete ct._ObjectType_;
ct.AzureSubscriptionId = formatting.extractCsomGuid(ct.AzureSubscriptionId);
ct.ContainerTypeId = formatting.extractCsomGuid(ct.ContainerTypeId);
ct.OwningAppId = formatting.extractCsomGuid(ct.OwningAppId);
ct.OwningTenantId = formatting.extractCsomGuid(ct.OwningTenantId);
});
return containerTypes;
},
/**
* Get the ID of a container type by its name.
* @param spoAdminUrl SharePoint Online admin center URL (e.g. https://contoso-admin.sharepoint.com)
* @param name Name of the container type to search for
* @returns ID of the container type
*/
async getContainerTypeIdByName(spoAdminUrl, name) {
const allContainerTypes = await this.getAllContainerTypes(spoAdminUrl);
const containerTypes = allContainerTypes.filter(ct => ct.DisplayName.toLowerCase() === name.toLowerCase());
if (containerTypes.length === 0) {
throw new Error(`The specified container type '${name}' does not exist.`);
}
if (containerTypes.length > 1) {
const containerTypeKeyValuePair = formatting.convertArrayToHashTable('ContainerTypeId', containerTypes);
const containerType = await cli.handleMultipleResultsFound(`Multiple container types with name '${name}' found.`, containerTypeKeyValuePair);
return containerType.ContainerTypeId;
}
return containerTypes[0].ContainerTypeId;
},
/**
* Get the ID of a container by its name.
* @param containerTypeId ID of the container type.
* @param name Name of the container to search for.
* @returns ID of the container.
*/
async getContainerIdByName(containerTypeId, name) {
const containers = await odata.getAllItems(`${graphResource}/v1.0/storage/fileStorage/containers?$filter=containerTypeId eq ${containerTypeId}&$select=id,displayName`);
const matchingContainers = containers.filter(c => c.displayName.toLowerCase() === name.toLowerCase());
if (matchingContainers.length === 0) {
throw new Error(`The specified container '${name}' does not exist.`);
}
if (matchingContainers.length > 1) {
const containerKeyValuePair = formatting.convertArrayToHashTable('id', matchingContainers);
const container = await cli.handleMultipleResultsFound(`Multiple containers with name '${name}' found.`, containerKeyValuePair);
return container.id;
}
return matchingContainers[0].id;
}
};
//# sourceMappingURL=spe.js.map