Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#215 #219

Merged
merged 2 commits into from
Feb 11, 2020
Merged

#215 #219

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface ISPContentType {
StringId: string;
Name: string;
Fields: ISPField[];
FieldLinks?: { Name: string, Required: boolean }[];
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { dateAdd, stringIsNullOrEmpty, TypedHash } from '@pnp/common';
import { CamlQuery, Web, ListEnsureResult } from '@pnp/sp';
import { Logger, LogLevel } from '@pnp/logging';
import { CamlQuery, ListEnsureResult, Web } from '@pnp/sp';
import { default as initSpfxJsom, ExecuteJsomQuery } from 'spfx-jsom';
import { makeUrlAbsolute } from '../../helpers/makeUrlAbsolute';
import { transformFieldXml } from '../../helpers/transformFieldXml';
Expand Down Expand Up @@ -109,7 +110,7 @@ export class PortalDataService {
public async syncList(url: string, listName: string, contentTypeId: string, properties?: TypedHash<string>): Promise<ListEnsureResult> {
const targetWeb = new Web(url);
const { jsomContext } = await initSpfxJsom(url, { loadTaxonomy: true });
const [sourceContentType, targetSiteFields, ensureList] = await Promise.all([
const [sourceContentType, destSiteFields, ensureList] = await Promise.all([
this._getHubContentType(contentTypeId),
this._getSiteFields(targetWeb),
targetWeb.lists.ensure(listName, '', 100, false, { Hidden: true, EnableAttachments: false }),
Expand All @@ -119,22 +120,35 @@ export class PortalDataService {
for (let field of sourceContentType.Fields) {
let [[listField], [siteField]] = [
listFields.filter(fld => fld.InternalName === field.InternalName),
targetSiteFields.filter(fld => fld.InternalName === field.InternalName && fld.SchemaXml.indexOf('ShowInEditForm="FALSE"') === -1),
destSiteFields.filter(fld => fld.InternalName === field.InternalName && fld.SchemaXml.indexOf('ShowInEditForm="FALSE"') === -1),
];
if (listField) continue;
if (listField) {
Logger.log({ message: `(PortalDataService) (syncList) Field [${field.InternalName}] already exists on list [${listName}].`, level: LogLevel.Info });
continue;
}
try {
let [fieldLink] = sourceContentType.FieldLinks.filter(fl => fl.Name === field.InternalName);
Logger.log({ message: `(PortalDataService) (syncList) Adding field [${field.InternalName}] to list [${listName}].`, level: LogLevel.Info, data: { fieldLink, siteField: !!siteField } });
if (siteField) {
let spSiteField = jsomContext.web.get_fields().getByInternalNameOrTitle(siteField.InternalName);
spList.get_fields().add(spSiteField);
let newField = spList.get_fields().add(spSiteField);
if (fieldLink && fieldLink.Required) {
newField.set_required(true);
newField.updateAndPushChanges(true);
}
} else {
let newField = spList.get_fields().addFieldAsXml(transformFieldXml(field.SchemaXml, { DisplayName: field.InternalName }), false, SP.AddFieldOptions.addToDefaultContentType);
if (fieldLink && fieldLink.Required) {
newField.set_required(true);
}
newField.set_title(field.Title);
newField.updateAndPushChanges(true);
}
await ExecuteJsomQuery(jsomContext);
} catch (error) { }
}
try {
Logger.log({ message: `(PortalDataService) (syncList) Attempting to add field [TemplateParameters] to list ${listName}.`, level: LogLevel.Info });
let newField = spList.get_fields().addFieldAsXml(`<Field Type="Note" DisplayName="TemplateParameters" ID="{b8854944-7141-471f-b8df-53d93a4395ba}" StaticName="TemplateParameters" Name="TemplateParameters" UnlimitedLengthInDocumentLibrary="TRUE" ShowInEditForm="FALSE" />`, false, SP.AddFieldOptions.addToDefaultContentType);
newField.updateAndPushChanges(true);
await ExecuteJsomQuery(jsomContext);
Expand All @@ -153,8 +167,8 @@ export class PortalDataService {
private async _getHubContentType(contentTypeId: string): Promise<ISPContentType> {
let contentType = await this._web.contentTypes
.getById(contentTypeId)
.select('StringId', 'Name', 'Fields/InternalName', 'Fields/Title', 'Fields/SchemaXml', 'Fields/InternalName')
.expand('Fields')
.select('StringId', 'Name', 'Fields/InternalName', 'Fields/Title', 'Fields/SchemaXml', 'Fields/InternalName', 'FieldLinks/Name', 'FieldLinks/Required')
.expand('Fields', 'FieldLinks')
.get<ISPContentType>();
return contentType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import '@pnp/polyfill-ie11';
import { sp } from '@pnp/sp';
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { ApplicationInsightsLogListener } from 'shared/lib/logging';
import HubSiteService, { IHubSite } from 'sp-hubsite-service';
import { IBaseWebPartComponentProps } from '../../components/BaseWebPartComponent';
import SPDataAdapter from '../../data';
Expand Down