import { ContentType , ContentTypeCreationInformation } from "gd-sprest-def/lib/SP" ; import { IcreateContentType } from "../../../@types/helper/methods" ; import { Web } from "../../lib" ; declare var SP ; param ctInfo param parentInfo param webUrl param listName export const createContentType = ( ctInfo , parentInfo , webUrl ? , listName ? ) : PromiseLike < ContentType > => { return new Promise ( ( resolve , reject ) => { let ctx = webUrl ? new SP . ClientContext ( webUrl ) : SP . ClientContext . get_current ( ) ; let parentContentType = ( parentInfo . Url ? ctx . get_site ( ) . openWeb ( parentInfo . Url ) : ctx . get_web ( ) ) . get_contentTypes ( ) . getById ( parentInfo . Id ) ; let ct = new SP . ContentTypeCreationInformation ( ) ; ctInfo . Description != null ? ct . set_description ( ctInfo . Description ) : null ; ctInfo . Group != null ? ct . set_group ( ctInfo . Group ) : null ; ct . set_name ( ctInfo . Name ) ; ct . set_parentContentType ( parentContentType ) ; let src = listName ? ctx . get_web ( ) . get_lists ( ) . getByTitle ( listName ) : ctx . get_web ( ) ; let contentTypes = src . get_contentTypes ( ) ; contentTypes . add ( ct ) ; ctx . load ( contentTypes ) ; ctx . executeQueryAsync ( ( ) => { let cts = ( listName ? Web ( ) . Lists ( listName ) : Web ( ) ) . ContentTypes ( ) ; cts . query ( { Filter : "Name eq '" + ctInfo . Name + "'" } ) . execute ( cts => { resolve ( cts . results [ 0 ] as any ) ; } ) ; } , ( sender , args ) => { console . log ( "[gd-sprest][Create Content Type] Error adding the content type." , ctInfo . Name ) ; reject ( args . get_message ( ) ) ; } ) ; } ) ; } ;