Organisation Assets Library REST API

SharePoint Online provides a functionality called ‘Organisational Assets Library’ that based on the documentation allows the organisation to store assets like images or photos in easily accessible place. There is a possibility to configure one or more Document Libraries to play a role of the ‘Organisational Assets Library’. The only restriction is that all the libraries need to exist in the same site.

You can manage your organisational assets libraries using Powershell or CSOM – more information in the docs .

Note: to be able to work with Organisational Assets Libraries, first it’s required to enable Tenant CDN.

REST API

However, recently I had requirement to obtain the information about those from SPFx WebPart, so my first steps where to find out how to get those information with REST calls.

To work with the web service, these endpoints need to be used:

    • /_api/Microsoft.Online.SharePoint.TenantManagement.Office365Tenant/SetOrgAssetsLib
    • /_api/Microsoft.Online.SharePoint.TenantManagement.Office365Tenant/GetOrgAssets
    • /_api/Microsoft.Online.SharePoint.TenantManagement.Office365Tenant/AddToOrgAssetsLibAndCdn
    • /_api/Microsoft.Online.SharePoint.TenantManagement.Office365Tenant/RemoveFromOrgAssets

Based on this information I have created a OrgAssets service class that helps work with the endpoints:

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}'`);
}
}
}
view raw OrgAssetsService.ts hosted with ❤ by GitHub

Types used by the service:
export interface ResourcePath {
DecodedUrl: string;
}
export enum CdnType {
Public = 0,
Private = 1
}
export enum OrgAssetType {
Undefined = 0,
ImageDocumentLibrary = 1,
OfficeTemplateLibrary = 2
}
export interface AddOrgAssetCdnRequest {
/**
* Absolute Site URL
*/
libUrl: ResourcePath;
cdnType: CdnType;
orgAssetType: OrgAssetType;
/**
* Absolute Thumbnail URL. It must exist in the same Document Library.
*/
thumbnailUrl?: ResourcePath;
}
export interface OrgAssetsItem {
/**
* Display name of the document library
*/
DisplayName: string;
/**
* Server relative library resource path
*/
LibraryUrl: ResourcePath,
ListId: string;
/**
* Undefined = 0,
* ImageDocumentLibrary = 1,
* OfficeTemplateLibrary = 2
*/
OrgAssetType: OrgAssetType,
/**
* Site relative thumbnail resource path
*/
ThumbnailUrl: ResourcePath,
/**
* Unique ID
*/
UniqueId: string;
FileType: string;
}
export interface OrgAssetsData {
/**
* Organisation Assets Libraries information
*/
Items: OrgAssetsItem[];
/**
* Site ID.
*/
SiteId: string;
/**
* Server relative site resource path
*/
Url: ResourcePath;
/**
* WebID
*/
WebId: string;
}

Note: it might take couple of seconds to view the changes applied after the execution of the request.

Hope it’s going o be useful for you!