-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a process query method to the web object. Updated the create co…
…ntent type helper method.
- Loading branch information
1 parent
0e53c99
commit 660f2f7
Showing
9 changed files
with
230 additions
and
60 deletions.
There are no files selected for viewing
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "gd-sprest", | ||
"version": "8.3.3", | ||
"version": "8.3.4", | ||
"description": "An easy way to develop against the SharePoint REST API.", | ||
"author": "Gunjan Datta <[email protected]> (https://gunjandatta.github.io)", | ||
"license": "MIT", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,133 @@ | ||
import { ContentType, ContentTypeCreationInformation } from "gd-sprest-def/lib/SP"; | ||
import { IcreateContentType } from "../../../@types/helper/methods"; | ||
import { IcreateContentType, IcreateContentTypeProps } from "../../../@types/helper/methods"; | ||
import { Web } from "../../lib"; | ||
declare var SP; | ||
|
||
/** | ||
* Creates a content type in a web or specified list. | ||
* @param ctInfo - The content type information. | ||
* @param parentInfo - The parent content type id and url containing it. | ||
* @param webUrl - The relative url to create the content type in. | ||
* @param listName - The list name to add the content type to. | ||
* @param listName - The list name to add the content type to. If blank, it will add it to the web content types. | ||
*/ | ||
export const createContentType: IcreateContentType = (ctInfo: ContentTypeCreationInformation, parentInfo: { Id: string, Url?: string }, webUrl?: string, listName?: string): PromiseLike<ContentType> => { | ||
export const createContentType: IcreateContentType = (props: IcreateContentTypeProps): PromiseLike<ContentType> => { | ||
// Return a promise | ||
return new Promise((resolve, reject) => { | ||
// Set the context | ||
let ctx = webUrl ? new SP.ClientContext(webUrl) : SP.ClientContext.get_current(); | ||
|
||
// Get the parent content type | ||
let parentContentType = (parentInfo.Url ? ctx.get_site().openWeb(parentInfo.Url) : ctx.get_web()).get_contentTypes().getById(parentInfo.Id); | ||
|
||
// Create the content type | ||
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) | ||
|
||
// Add the content type | ||
let src = listName ? ctx.get_web().get_lists().getByTitle(listName) : ctx.get_web(); | ||
let contentTypes = src.get_contentTypes(); | ||
contentTypes.add(ct); | ||
ctx.load(contentTypes); | ||
|
||
// Execute the request | ||
ctx.executeQueryAsync( | ||
// Success | ||
() => { | ||
// Set the content type collection | ||
let cts = (listName ? Web(webUrl).Lists(listName) : Web(webUrl)).ContentTypes(); | ||
|
||
// Find the content type | ||
cts.query({ Filter: "Name eq '" + ctInfo.Name + "'" }).execute(cts => { | ||
// See if we are targeting a list | ||
if (props.listName) { | ||
// Process the request | ||
Web.processQuery(ListTemplate(props.listName, props.ctInfo, props.parentContentTypeId), props.webUrl).then(() => { | ||
// Get the content type and resolve the request | ||
Web(props.webUrl).Lists(props.listName).ContentTypes().query({ | ||
Filter: "Name eq '" + props.ctInfo.Name + "'" | ||
}).execute(cts => { | ||
// Resolve the request | ||
resolve(cts.results[0] as any); | ||
}); | ||
}, | ||
|
||
// Error | ||
(sender, args) => { | ||
// Log | ||
console.log("[gd-sprest][Create Content Type] Error adding the content type.", ctInfo.Name); | ||
|
||
// Reject the request | ||
reject(args.get_message()); | ||
}); | ||
}, reject); | ||
}, reject); | ||
} else { | ||
// Process the request | ||
Web.processQuery(WebTemplate(props.ctInfo, props.parentContentTypeId), props.webUrl).then(() => { | ||
// Get the content type and resolve the request | ||
Web(props.webUrl).ContentTypes().query({ | ||
Filter: "Name eq '" + props.ctInfo.Name + "'" | ||
}).execute(cts => { | ||
// Resolve the request | ||
resolve(cts.results[0] as any); | ||
}, reject); | ||
}, reject); | ||
} | ||
}); | ||
} | ||
|
||
// List Template | ||
const ListTemplate = (listName: string, ctInfo: ContentTypeCreationInformation, parentId: string = "0x01"): string => { | ||
return `<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="Javascript Library"> | ||
<Actions> | ||
<ObjectPath Id="1" ObjectPathId="0" /> | ||
<ObjectPath Id="3" ObjectPathId="2" /> | ||
<ObjectPath Id="5" ObjectPathId="4" /> | ||
<ObjectPath Id="7" ObjectPathId="6" /> | ||
<ObjectPath Id="9" ObjectPathId="8" /> | ||
<ObjectPath Id="11" ObjectPathId="10" /> | ||
<ObjectIdentityQuery Id="12" ObjectPathId="10" /> | ||
<ObjectPath Id="14" ObjectPathId="13" /> | ||
<ObjectPath Id="16" ObjectPathId="15" /> | ||
<ObjectIdentityQuery Id="17" ObjectPathId="15" /> | ||
<Query Id="18" ObjectPathId="13"> | ||
<Query SelectAllProperties="true"> | ||
<Properties /> | ||
</Query> | ||
<ChildItemQuery SelectAllProperties="true"> | ||
<Properties /> | ||
</ChildItemQuery> | ||
</Query> | ||
</Actions> | ||
<ObjectPaths> | ||
<StaticProperty Id="0" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" /> | ||
<Property Id="2" ParentId="0" Name="Web" /> | ||
<Property Id="4" ParentId="2" Name="ContentTypes" /> | ||
<Method Id="6" ParentId="4" Name="GetById"> | ||
<Parameters> | ||
<Parameter Type="String">${parentId}</Parameter> | ||
</Parameters> | ||
</Method> | ||
<Property Id="8" ParentId="2" Name="Lists" /> | ||
<Method Id="10" ParentId="8" Name="GetByTitle"> | ||
<Parameters> | ||
<Parameter Type="String">${listName}</Parameter> | ||
</Parameters> | ||
</Method> | ||
<Property Id="13" ParentId="10" Name="ContentTypes" /> | ||
<Method Id="15" ParentId="13" Name="Add"> | ||
<Parameters> | ||
<Parameter TypeId="{168f3091-4554-4f14-8866-b20d48e45b54}"> | ||
<Property Name="Description" Type="${ctInfo.Description ? "String" : "Null"}">${ctInfo.Description || ""}</Property> | ||
<Property Name="Group" Type="${ctInfo.Group ? "String" : "Null"}">${ctInfo.Group || ""}</Property> | ||
<Property Name="Id" Type="Null" /> | ||
<Property Name="Name" Type="String">${ctInfo.Name || ""}</Property> | ||
<Property Name="ParentContentType" ObjectPathId="6" /> | ||
</Parameter> | ||
</Parameters> | ||
</Method> | ||
</ObjectPaths> | ||
</Request>`; | ||
} | ||
|
||
// Web Template | ||
const WebTemplate = (ctInfo: ContentTypeCreationInformation, parentId: string = "0x01"): string => { | ||
return `<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="Javascript Library"> | ||
<Actions> | ||
<ObjectPath Id="26" ObjectPathId="25" /> | ||
<ObjectIdentityQuery Id="27" ObjectPathId="25" /> | ||
<Query Id="28" ObjectPathId="4"> | ||
<Query SelectAllProperties="true"> | ||
<Properties /> | ||
</Query> | ||
<ChildItemQuery SelectAllProperties="true"> | ||
<Properties /> | ||
</ChildItemQuery> | ||
</Query> | ||
</Actions> | ||
<ObjectPaths> | ||
<Property Id="4" ParentId="2" Name="ContentTypes" /> | ||
<Method Id="25" ParentId="4" Name="Add"> | ||
<Parameters> | ||
<Parameter TypeId="{168f3091-4554-4f14-8866-b20d48e45b54}"> | ||
<Property Name="Description" Type="${ctInfo.Description ? "String" : "Null"}">${ctInfo.Description || ""}</Property> | ||
<Property Name="Group" Type="${ctInfo.Group ? "String" : "Null"}">${ctInfo.Group || ""}</Property> | ||
<Property Name="Id" Type="Null" /> | ||
<Property Name="Name" Type="String">${ctInfo.Name || ""}</Property> | ||
<Property Name="ParentContentType" ObjectPathId="6" /> | ||
</Parameter> | ||
</Parameters> | ||
</Method> | ||
<Property Id="2" ParentId="0" Name="Web" /> | ||
<Method Id="6" ParentId="4" Name="GetById"> | ||
<Parameters> | ||
<Parameter Type="String">${parentId}</Parameter> | ||
</Parameters> | ||
</Method> | ||
<StaticProperty Id="0" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" /> | ||
</ObjectPaths> | ||
</Request>`; | ||
} |
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
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