Created
September 10, 2019 05:55
-
-
Save siata13/939fd8a2e2d6d89ee99023ba78ea506a to your computer and use it in GitHub Desktop.
Organisation Assets Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { WebPartContext } from "@microsoft/sp-webpart-base"; | |
import { SPHttpClient } from "@microsoft/sp-http"; | |
import { OrgAssetsData, CdnType, OrgAssetType, AddOrgAssetCdnRequest, ResourcePath } from "./OrgAssetsService.types"; | |
import { Guid } from "@microsoft/sp-core-library"; | |
export class OrgAssetsService { | |
private context: WebPartContext; | |
constructor(context: WebPartContext) { | |
this.context = context; | |
} | |
/** | |
* Gets information about available Organisation Assets Library | |
*/ | |
public async getOrgAssets(): Promise<OrgAssetsData> { | |
try { | |
const apiUrl = `${this.context.pageContext.web.absoluteUrl}/_api/Microsoft.Online.SharePoint.TenantManagement.Office365Tenant/GetOrgAssets`; | |
const orgAssetsDataResponse = await this.context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1); | |
if (!orgAssetsDataResponse || !orgAssetsDataResponse.ok) { | |
throw new Error(`Something went wrong when obtaining OrgAssets. Status='${orgAssetsDataResponse.statusText}'`); | |
} | |
const orgAssetsData : OrgAssetsData = await orgAssetsDataResponse.json(); | |
return orgAssetsData; | |
} catch (err) { | |
console.error(`[OrgAssetsService.getOrgAssets]: Err='${err.message}'`); | |
return null; | |
} | |
} | |
/** | |
* Adds new Organisation Assets Library | |
* @param libUrl Absolute List URL | |
* @param cdnType CDN Type | |
* @param orgAssetType Organisation Assets Library Type | |
* @param thumbnailUrl Absolute Thumbnail URL | |
*/ | |
public async addOrgAssetsLib(libUrl: string, cdnType: CdnType, orgAssetType: OrgAssetType = OrgAssetType.ImageDocumentLibrary, thumbnailUrl?: string): Promise<boolean> { | |
try { | |
const apiUrl = `${this.context.pageContext.web.absoluteUrl}/_api/Microsoft.Online.SharePoint.TenantManagement.Office365Tenant/AddToOrgAssetsLibAndCdn`; | |
const thumbnailResPath: ResourcePath = thumbnailUrl ? { | |
DecodedUrl: thumbnailUrl | |
} : null; | |
const reqData: AddOrgAssetCdnRequest = { | |
cdnType, | |
libUrl: { | |
DecodedUrl: libUrl | |
}, | |
orgAssetType, | |
thumbnailUrl: thumbnailResPath | |
} | |
const orgAssetsDataResponse = await this.context.spHttpClient.post(apiUrl, SPHttpClient.configurations.v1, { | |
body: JSON.stringify(reqData) | |
}); | |
if (!orgAssetsDataResponse) { | |
throw new Error(`Something went wrong when adding OrgAssetsLib. Status='${orgAssetsDataResponse.statusText}'`); | |
} | |
const result : boolean = await orgAssetsDataResponse.ok; | |
return result; | |
} catch(err) { | |
console.error(`[OrgAssetsService.setOrgAssets]: Err='${err.message}'`); | |
} | |
} | |
/** | |
* Updates Organisation Assets Library | |
* @param libUrl | |
* @param thumbnailUrl | |
*/ | |
public async updateOrgAssetsLib(libUrl: string, thumbnailUrl?: string, orgAssetType?: OrgAssetType) : Promise<boolean> { | |
try { | |
const apiUrl = `${this.context.pageContext.web.absoluteUrl}/_api/Microsoft.Online.SharePoint.TenantManagement.Office365Tenant/SetOrgAssetsLib`; | |
const thumbnailResPath: ResourcePath = thumbnailUrl ? { | |
DecodedUrl: thumbnailUrl | |
} : null; | |
const orgAssetTypeData = orgAssetType ? orgAssetType : null; | |
const requestData = { | |
libUrl: { | |
DecodedUrl: libUrl | |
} | |
} | |
if (thumbnailResPath) { | |
requestData["thumbnailUrl"] = thumbnailResPath; | |
} | |
if (orgAssetType) { | |
requestData["orgAssetType"] = orgAssetTypeData; | |
} | |
const orgAssetsDataResponse = await this.context.spHttpClient.post(apiUrl, SPHttpClient.configurations.v1, { | |
body: JSON.stringify(requestData) | |
}); | |
if (!orgAssetsDataResponse) { | |
throw new Error(`Something went wrong when obtaining OrgAssets. Status='${orgAssetsDataResponse.statusText}'`); | |
} | |
const result : boolean = await orgAssetsDataResponse.ok; | |
return result; | |
} catch(err) { | |
console.error(`[OrgAssetsService.setOrgAssets]: Err='${err.message}'`); | |
} | |
} | |
/** | |
* Removes Organisation Assets Library by List ID. | |
* @param listId Library ID | |
*/ | |
public async removeOrgAssetsLibById(listId: string) : Promise<boolean> { | |
try { | |
const apiUrl = `${this.context.pageContext.web.absoluteUrl}/_api/Microsoft.Online.SharePoint.TenantManagement.Office365Tenant/RemoveFromOrgAssets`; | |
const requestData = { | |
listId: Guid.parse(listId) | |
} | |
const orgAssetsDataResponse = await this.context.spHttpClient.post(apiUrl, SPHttpClient.configurations.v1, { | |
body: JSON.stringify(requestData) | |
}); | |
if (!orgAssetsDataResponse) { | |
throw new Error(`Something went wrong when deleting OrgAssets. Status='${orgAssetsDataResponse.statusText}'`); | |
} | |
const result : boolean = await orgAssetsDataResponse.ok; | |
return result; | |
} catch(err) { | |
console.error(`[OrgAssetsService.removeOrgAssetsLib]: Err='${err.message}'`); | |
} | |
} | |
/** | |
* Removes Organisation Assets Library by absolute URL | |
* @param libUrl Absolute Library URL | |
*/ | |
public async removeOrgAssetsLibByUrl(libUrl: string) : Promise<boolean> { | |
try { | |
const apiUrl = `${this.context.pageContext.web.absoluteUrl}/_api/Microsoft.Online.SharePoint.TenantManagement.Office365Tenant/RemoveFromOrgAssets`; | |
const requestData = { | |
libUrl: { | |
DecodedUrl: libUrl | |
} | |
} | |
const orgAssetsDataResponse = await this.context.spHttpClient.post(apiUrl, SPHttpClient.configurations.v1, { | |
body: JSON.stringify(requestData) | |
}); | |
if (!orgAssetsDataResponse) { | |
throw new Error(`Something went wrong when deleting OrgAssets. Status='${orgAssetsDataResponse.statusText}'`); | |
} | |
const result : boolean = await orgAssetsDataResponse.ok; | |
return result; | |
} catch(err) { | |
console.error(`[OrgAssetsService.removeOrgAssetsLib]: Err='${err.message}'`); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment