Skip to content

Commit

Permalink
Merge pull request #242 from gunjandatta/gdatta
Browse files Browse the repository at this point in the history
New Helper Methods
  • Loading branch information
gunjandatta authored Mar 28, 2020
2 parents 72a63ef + 913ec07 commit 3430ff6
Show file tree
Hide file tree
Showing 17 changed files with 353 additions and 244 deletions.
69 changes: 66 additions & 3 deletions @types/helper/methods.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { SP } from "gd-sprest-def";

/**
* Adds a content editor webpart to a page.
* @param url - The relative url of the page.
* @param wpProps - The webpart properties.
*/
export const addContentEditorWebPart: (url: string, wpProps: IContentEditorWebPart) => PromiseLike<void>;

/**
* Adds a script editor webpart to a page.
* @param url - The relative url of the page.
* @param wpProps - The webpart properties.
*/
export const addScriptEditorWebPart: (url: string, wpProps: IScriptEditorWebPart) => PromiseLike<void>;

/**
* Creates a content type in a web or specified list.
* @param ctInfo - The content type information.
Expand Down Expand Up @@ -33,6 +47,12 @@ export function parse<T = any>(jsonString: string): T;
*/
export const request: (props: IRequest) => PromiseLike<any>;

/**
* Sets the field links associated with a content type.
* @param ctInfo - The content type information
*/
export const setContentTypeFields: (ctInfo: { id: string, fields: Array<string>, listName?: string, webUrl?: string }) => PromiseLike<void>;

/**
* Request
*/
Expand All @@ -51,7 +71,50 @@ export interface IRequest {
}

/**
* Sets the field links associated with a content type.
* @param ctInfo - The content type information
* The content editor webpart properties
*/
export interface IContentEditorWebPart {
/** The webpart description. */
description?: string;

/** The webpart content. */
content?: string;

/** The webpart content link. */
contentLink?: string;

/** The webpart frame type. (BorderOnly, Default, None, Standard or TitleBarOnly) */
frameType?: string;

/** The webpart index. */
index?: number;

/** The webpart title. */
title?: string;

/** The webpart zone. */
zone?: string;
}

/**
* The script editor webpart properties
*/
export const setContentTypeFields: (ctInfo: { id: string, fields: Array<string>, listName?: string, webUrl?: string }) => PromiseLike<void>;
export interface IScriptEditorWebPart {
/** The webpart description. */
description?: string;

/** The webpart chrome type. (BorderOnly, Default, None, TitleAndBorder or TitleOnly) */
chromeType?: string;

/** The webpart content. */
content: string;

/** The webpart index. */
index?: number;

/** The webpart title. */
title?: string;

/** The webpart zone. */
zone?: string;
}
30 changes: 0 additions & 30 deletions @types/helper/webpart.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ export interface IWebPart {
*/
new(props: IWebPartProps);

/**
* Adds a script editor webpart to a page.
* @param url - The relative url of the page.
* @param wpProps - The webpart properties.
*/
addWebPartToPage(url: string, wpProps: IWebPartScriptEditor): PromiseLike<void>;

/**
* Creates an instance of a webpart.
* @param props - The webpart properties.
Expand Down Expand Up @@ -81,27 +74,4 @@ export interface IWebPartProps {

/** The target element id to render the webpart to */
elementId: string;
}

/**
* The script editor webpart properties
*/
export interface IWebPartScriptEditor {
/** The webpart description. */
description?: string;

/** The webpart chrome type. (BorderOnly, Default, None, TitleAndBorder or TitleOnly) */
chromeType?: string;

/** The webpart content. */
content: string;

/** The webpart index. */
index?: number;

/** The webpart title. */
title?: string;

/** The webpart zone. */
zone?: string;
}
39 changes: 39 additions & 0 deletions build/helper/methods/addContentEditorWebPart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Method to add a script editor webpart to the page
exports.addContentEditorWebPart = function (url, wpProps) {
// Return a promise
return new Promise(function (resolve, reject) {
// Get the current context
var context = SP.ClientContext.get_current();
// Get the webpart manager for the page
var page = context.get_web().getFileByServerRelativeUrl(url);
var wpMgr = page.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
// Create the webpart
var wp = wpMgr.importWebPart("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<WebPart xmlns=\"http://schemas.microsoft.com/WebPart/v2\">\n <Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>\n <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>\n <Title>[[Title]]</Title>\n <Description>[[Description]]</Description>\n <FrameType>[[FrameType]]</FrameType>\n <ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\">[[ContentLink]]</ContentLink>\n <Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\"><![CDATA[[[Content]]]]></Content>\n</WebPart>".replace(/\r?\n/g, '')
.replace(/\[\[FrameType\]\]/g, wpProps.frameType || "Default")
.replace(/\[\[Content\]\]/g, wpProps.content || "")
.replace(/\[\[ContentLink\]\]/g, wpProps.contentLink || "")
.replace(/\[\[Description\]\]/g, wpProps.description || "")
.replace(/\[\[Title\]\]/g, wpProps.title || "")).get_webPart();
// Add the webpart to the page
wpMgr.addWebPart(wp, wpProps.zone || "", wpProps.index || 0);
// Save the page
context.load(wp);
context.executeQueryAsync(
// Success
function () {
// Resolve the promise
resolve();
},
// Error
function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
// Reject the promise
reject(args[1] ? args[1].get_message() : "");
});
});
};
38 changes: 38 additions & 0 deletions build/helper/methods/addScriptEditorWebPart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Method to add a script editor webpart to the page
exports.addScriptEditorWebPart = function (url, wpProps) {
// Return a promise
return new Promise(function (resolve, reject) {
// Get the current context
var context = SP.ClientContext.get_current();
// Get the webpart manager for the page
var page = context.get_web().getFileByServerRelativeUrl(url);
var wpMgr = page.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
// Create the webpart
var wp = wpMgr.importWebPart("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<webParts>\n <webPart xmlns=\"http://schemas.microsoft.com/WebPart/v3\">\n <metaData>\n <type name=\"Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\" />\n <importErrorMessage>$Resources:core,ImportantErrorMessage;</importErrorMessage>\n </metaData>\n <data>\n <properties>\n <property name=\"Title\" type=\"string\">[[Title]]</property>\n <property name=\"Description\" type=\"string\">[[Description]]</property>\n <property name=\"ChromeType\" type=\"chrometype\">[[ChromeType]]</property>\n <property name=\"Content\" type=\"string\">[[Content]]</property>\n </properties>\n </data>\n </webPart>\n</webParts>".replace(/\r?\n/g, '')
.replace(/\[\[ChromeType\]\]/g, wpProps.chromeType || "TitleOnly")
.replace(/\[\[Content\]\]/g, wpProps.content.replace(/\</g, '&lt;').replace(/\>/g, '&gt;'))
.replace(/\[\[Description\]\]/g, wpProps.description || "")
.replace(/\[\[Title\]\]/g, wpProps.title || "")).get_webPart();
// Add the webpart to the page
wpMgr.addWebPart(wp, wpProps.zone || "", wpProps.index || 0);
// Save the page
context.load(wp);
context.executeQueryAsync(
// Success
function () {
// Resolve the promise
resolve();
},
// Error
function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
// Reject the promise
reject(args[1] ? args[1].get_message() : "");
});
});
};
2 changes: 2 additions & 0 deletions build/helper/methods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./addContentEditorWebPart"));
__export(require("./addScriptEditorWebPart"));
__export(require("./createContentType"));
__export(require("./createDocSet"));
__export(require("./hasPermissions"));
Expand Down
75 changes: 0 additions & 75 deletions build/helper/webpart.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var lib_1 = require("../lib");
/**
* Web Part
*/
Expand Down Expand Up @@ -36,44 +35,6 @@ var _WebPart = /** @class */ (function () {
}
}
};
/**
* Method to get the webpart
*/
this.getWebPart = function (wpId) {
// Return a promise
return new Promise(function (resolve, reject) {
// Get the current context
var context = SP.ClientContext.get_current();
// Get the webpart from the current page
var page = context.get_web().getFileByServerRelativeUrl(lib_1.ContextInfo.serverRequestPath);
var wpMgr = page.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var wpDef = wpMgr.get_webParts().getById(wpId);
var wp = wpDef.get_webPart();
context.load(wp, "Properties");
// Execute the request
context.executeQueryAsync(
// Success
function () {
// Resolve the promise
resolve({
Context: context,
Properties: wp.get_properties(),
WebPart: wp,
WebPartDefinition: wpDef,
WebPartId: wp.get_id()
});
},
// Error
function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
// Reject the promise
reject(args[1] ? args[1].get_message() : "");
});
});
};
/**
* Method to get the webpart id for a specified element
* @param el - The target element.
Expand Down Expand Up @@ -251,42 +212,6 @@ var _WebPart = /** @class */ (function () {
_this.render();
});
}
// Method to add a script editor webpart to the page
_WebPart.addWebPartToPage = function (url, wpProps) {
// Return a promise
return new Promise(function (resolve, reject) {
// Get the current context
var context = SP.ClientContext.get_current();
// Get the webpart manager for the page
var page = context.get_web().getFileByServerRelativeUrl(url);
var wpMgr = page.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
// Create the webpart
var wp = wpMgr.importWebPart("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<webParts>\n <webPart xmlns=\"http://schemas.microsoft.com/WebPart/v3\">\n <metaData>\n <type name=\"Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\" />\n <importErrorMessage>$Resources:core,ImportantErrorMessage;</importErrorMessage>\n </metaData>\n <data>\n <properties>\n <property name=\"Title\" type=\"string\">[[Title]]</property>\n <property name=\"Description\" type=\"string\">[[Description]]</property>\n <property name=\"ChromeType\" type=\"chrometype\">[[ChromeType]]</property>\n <property name=\"Content\" type=\"string\">[[Content]]</property>\n </properties>\n </data>\n </webPart>\n</webParts>".replace(/\r?\n/g, '')
.replace(/\[\[ChromeType\]\]/g, wpProps.chromeType || "TitleOnly")
.replace(/\[\[Content\]\]/g, wpProps.content.replace(/\</g, '&lt;').replace(/\>/g, '&gt;'))
.replace(/\[\[Description\]\]/g, wpProps.description || "")
.replace(/\[\[Title\]\]/g, wpProps.title || "")).get_webPart();
// Add the webpart to the page
wpMgr.addWebPart(wp, wpProps.zone || "", wpProps.index || 0);
// Save the page
context.load(wp);
context.executeQueryAsync(
// Success
function () {
// Resolve the promise
resolve();
},
// Error
function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
// Reject the promise
reject(args[1] ? args[1].get_message() : "");
});
});
};
// Method to create an instance of the webpart
_WebPart.create = function (props) {
// Return an instance of the webpart
Expand Down
2 changes: 1 addition & 1 deletion build/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var sptypes_1 = require("./sptypes");
* SharePoint REST Library
*/
exports.$REST = {
__ver: 5.36,
__ver: 5.37,
AppContext: function (siteUrl) { return Lib.Site.getAppContext(siteUrl); },
Apps: Lib.Apps,
ContextInfo: Lib.ContextInfo,
Expand Down
Loading

0 comments on commit 3430ff6

Please sign in to comment.