- Implemented the PortalSettings Web Part with a new manifest and TypeScript file. - Created SCSS styles for the Web Part, defining various UI elements and responsive design. - Added localization support for English and German languages. - Developed tests for Provider Registry and static assets validation. - Included PowerShell script for project validation.
51 lines
3.9 KiB
TypeScript
51 lines
3.9 KiB
TypeScript
// tslint:disable:no-any max-line-length
|
|
import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';
|
|
import { WebPartContext } from '@microsoft/sp-webpart-base';
|
|
import { ITermSetInfo } from '../core/ProviderContracts';
|
|
|
|
export class TaxonomyService {
|
|
public constructor(private context: WebPartContext) { }
|
|
|
|
public getTermSets(): Promise<ITermSetInfo[]> {
|
|
const requestXml: string = '<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="PortalSettingsV3" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="2" ObjectPathId="1" /><ObjectIdentityQuery Id="3" ObjectPathId="1" /><ObjectPath Id="5" ObjectPathId="4" /><ObjectIdentityQuery Id="6" ObjectPathId="4" /><Query Id="7" ObjectPathId="4"><Query SelectAllProperties="false"><Properties><Property Name="Id" ScalarProperty="true" /><Property Name="Name" ScalarProperty="true" /><Property Name="Groups"><Query SelectAllProperties="false"><Properties /></Query><ChildItemQuery SelectAllProperties="false"><Properties><Property Name="Name" ScalarProperty="true" /><Property Name="Id" ScalarProperty="true" /><Property Name="IsSystemGroup" ScalarProperty="true" /><Property Name="TermSets"><Query SelectAllProperties="false"><Properties /></Query><ChildItemQuery SelectAllProperties="false"><Properties><Property Name="Name" ScalarProperty="true" /><Property Name="Id" ScalarProperty="true" /><Property Name="Description" ScalarProperty="true" /></Properties></ChildItemQuery></Property></Properties></ChildItemQuery></Property></Properties></Query></Query></Actions><ObjectPaths><StaticMethod Id="1" Name="GetTaxonomySession" TypeId="{981cbc68-9edc-4f8d-872f-71146fcbb84f}" /><Method Id="4" ParentId="1" Name="GetDefaultSiteCollectionTermStore" /></ObjectPaths></Request>';
|
|
const url: string = this.context.pageContext.site.absoluteUrl.replace(/\/$/, '') + '/_vti_bin/client.svc/ProcessQuery';
|
|
return this.context.spHttpClient.post(url, SPHttpClient.configurations.v1, {
|
|
headers: { 'Accept': 'application/json', 'Content-Type': 'application/xml' }, body: requestXml
|
|
}).then((response: SPHttpClientResponse): Promise<string> => {
|
|
if (!response.ok) { return Promise.reject(new Error('Termsets konnten nicht geladen werden (' + response.status + ').')); }
|
|
return response.text();
|
|
}).then((text: string): ITermSetInfo[] => this.parse(JSON.parse(text || '[]')));
|
|
}
|
|
|
|
private parse(payload: any[]): ITermSetInfo[] {
|
|
if (!Array.isArray(payload)) { return []; }
|
|
if (payload.length && payload[0] && payload[0].ErrorInfo) {
|
|
throw new Error(payload[0].ErrorInfo.ErrorMessage || 'Taxonomy-Abfrage fehlgeschlagen.');
|
|
}
|
|
let store: any;
|
|
for (let i: number = 0; i < payload.length; i++) {
|
|
if (payload[i] && payload[i]._ObjectType_ === 'SP.Taxonomy.TermStore') { store = payload[i]; break; }
|
|
}
|
|
const groups: any[] = store && store.Groups && Array.isArray(store.Groups._Child_Items_)
|
|
? store.Groups._Child_Items_ : [];
|
|
const result: ITermSetInfo[] = [];
|
|
for (let g: number = 0; g < groups.length; g++) {
|
|
if (!groups[g] || groups[g].IsSystemGroup === true) { continue; }
|
|
const sets: any[] = groups[g].TermSets && Array.isArray(groups[g].TermSets._Child_Items_)
|
|
? groups[g].TermSets._Child_Items_ : [];
|
|
for (let s: number = 0; s < sets.length; s++) {
|
|
const id: string = this.cleanGuid(sets[s] && sets[s].Id);
|
|
if (id) {
|
|
result.push({ id: id, name: sets[s].Name || id, groupName: groups[g].Name || 'Ohne Gruppe', description: sets[s].Description || '' });
|
|
}
|
|
}
|
|
}
|
|
return result.sort((left: ITermSetInfo, right: ITermSetInfo): number =>
|
|
(left.groupName + left.name).localeCompare(right.groupName + right.name));
|
|
}
|
|
|
|
private cleanGuid(value: any): string {
|
|
return String(value || '').replace('/Guid(', '').replace(')/', '').replace(')', '').replace(/[{}]/g, '').toLowerCase();
|
|
}
|
|
}
|