Skip to content

Commit

Permalink
Added a process query method to the web object. Updated the create co…
Browse files Browse the repository at this point in the history
…ntent type helper method.
  • Loading branch information
gunjandatta committed Nov 4, 2024
1 parent 0e53c99 commit 660f2f7
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 60 deletions.
19 changes: 16 additions & 3 deletions @types/helper/methods/createContentType.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ import { ContentType } from "gd-sprest-def/lib/SP/entitytypes";
/**
* Creates a content type in a web or specified list.
* @category Helper
* @param props - The content type creation properties.
*/
export const createContentType: IcreateContentType;
export interface IcreateContentType {
(props: IcreateContentTypeProps): PromiseLike<ContentType>;
}

/**
* The create content type properties
* @category Helper
* @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.
*/
export const createContentType: IcreateContentType;
export interface IcreateContentType {
(ctInfo: ContentTypeCreationInformation, parentInfo: { Id: string, Url?: string }, webUrl?: string, listName?: string): PromiseLike<ContentType>;
export interface IcreateContentTypeProps {
ctInfo: ContentTypeCreationInformation;
listName?: string;
parentContentTypeId?: string;
parentContentTypeUrl?: string;
webUrl?: string;
}
7 changes: 7 additions & 0 deletions @types/lib/web.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@ export interface IWeb {
* @param pageUrl - The absolute url of the page.
*/
getWebUrlFromPageUrl(pageUrl: string): IBaseExecution<{ GetWebUrlFromPageUrl: string }>;

/**
*
* @param query - The process query.
* @param webUrl - The web url to execute the request against.
*/
processQuery(query: string, webUrl?: string): PromiseLike<void>;
}
36 changes: 28 additions & 8 deletions dist/gd-sprest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2038,6 +2038,13 @@ declare module 'gd-sprest/lib/web' {
* @param pageUrl - The absolute url of the page.
*/
getWebUrlFromPageUrl(pageUrl: string): IBaseExecution<{ GetWebUrlFromPageUrl: string }>;

/**
*
* @param query - The process query.
* @param webUrl - The web url to execute the request against.
*/
processQuery(query: string, webUrl?: string): PromiseLike<void>;
}
}

Expand Down Expand Up @@ -6864,16 +6871,29 @@ declare module 'gd-sprest/helper/methods/createContentType' {
import { ContentType } from "gd-sprest-def/lib/SP/entitytypes";

/**
* Creates a content type in a web or specified list.
* @category Helper
* @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.
*/
* Creates a content type in a web or specified list.
* @category Helper
* @param props - The content type creation properties.
*/
export const createContentType: IcreateContentType;
export interface IcreateContentType {
(ctInfo: ContentTypeCreationInformation, parentInfo: { Id: string, Url?: string }, webUrl?: string, listName?: string): PromiseLike<ContentType>;
(props: IcreateContentTypeProps): PromiseLike<ContentType>;
}

/**
* The create content type properties
* @category Helper
* @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.
*/
export interface IcreateContentTypeProps {
ctInfo: ContentTypeCreationInformation;
listName?: string;
parentContentTypeId?: string;
parentContentTypeUrl?: string;
webUrl?: string;
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/gd-sprest.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/gd-sprest.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
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",
Expand Down
161 changes: 118 additions & 43 deletions src/helper/methods/createContentType.ts
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>`;
}
59 changes: 57 additions & 2 deletions src/lib/web.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IWeb } from "../../@types/lib";
import { Base, Request } from "../utils";
import { ContextInfo } from "./contextInfo";
import { Base, Request, RequestType } from "../utils";

export const Web: IWeb = ((url?, targetInfo?) => {
let web = new Base(targetInfo);
Expand Down Expand Up @@ -39,4 +40,58 @@ Web.getWebUrlFromPageUrl = ((pageUrl: string) => {
endpoint: "SP.Web.GetWebUrlFromPageUrl(@v)?@v='" + pageUrl + "'",
method: "POST"
});
}) as any;
}) as any;

// Static method to execute a process query request
Web.processQuery = (query: string, webUrl?: string) => {
// Minify the xml query
let data = query.replace(/\r?\n/g, '').replace(/ /g, '');

// Return a promise
return new Promise((resolve, reject) => {
let processResponse = (resp) => {
// See if there was an error
if (resp[0] && resp[0].ErrorInfo) {
// Reject the request
console.error("[Error] " + resp[0].ErrorInfo.ErrorMessage, resp[0].ErrorInfo);
reject(resp[0].ErrorInfo);
} else {
// Resolve the request
resolve(resp);
}
}

// See if the web url exist
if (webUrl) {
// Get the context information for the web
ContextInfo.getWeb(webUrl).execute(
// Success
ctx => {
// Execute the request
(new Base({
data,
endpoint: "_vti_bin/client.svc/ProcessQuery",
method: "POST",
requestDigest: ctx.GetContextWebInformation.FormDigestValue,
requestType: RequestType.Post,
})).execute(processResponse, reject);
},

// Error
() => {
// Reject the request
reject("Error getting the context information for the web.");
}
);
} else {
// Execute the request
(new Base({
data,
endpoint: "_vti_bin/client.svc/ProcessQuery",
method: "POST",
requestDigest: ContextInfo.formDigestValue,
requestType: RequestType.Post,
})).execute(processResponse, reject);
}
});
}
2 changes: 1 addition & 1 deletion src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IREST } from "../@types";
* SharePoint REST Library
*/
export const $REST: IREST = {
__ver: 8.33,
__ver: 8.34,
AppContext: (siteUrl: string) => { return Lib.Site.getAppContext(siteUrl); },
Apps: Lib.Apps,
ContextInfo: Lib.ContextInfo,
Expand Down

0 comments on commit 660f2f7

Please sign in to comment.