Skip to content

Instantly share code, notes, and snippets.

@phillipharding
Last active October 24, 2017 12:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipharding/f3071cbd0eef4fbcb0fc210390eea268 to your computer and use it in GitHub Desktop.
Save phillipharding/f3071cbd0eef4fbcb0fc210390eea268 to your computer and use it in GitHub Desktop.
Creates an Office Document in a SharePoint Document Library using the supplied Office Document Template, then opens the edit properties dialog of the new document.
/** Creates an Office Document in a SharePoint Document Library using the supplied Office Document Template
* see: https://msdn.microsoft.com/en-us/library/dd958478(v=office.12).aspx
* @param {} newDocumentName - empty or the new document filename
* @param {} listName - the target list (ID) in the form {A28BF06E-1374-4858-9D35-62C5904B8503}
* @param {} docType - Microsoft.SharePoint.Client.DocumentTemplateType
* - Invalid=0, Word=1, Excel=2, PowerPoint=3, OneNote=4, ExcelForm=5
* @param {} templateUrl - the absolute/server relative URL of the document template (can be template or document)
* @param {} waitUI - optional, the notification handle of a wiat dialog to close
* e.g.
* var waitUI = SP.UI.ModalDialog.showWaitScreenWithNoClose('Creating Document...', "Please wait, this shouldn't take long...", 100, 380);
* @return {} promise
*/
function createNewDocument(newDocumentName, listName, docType, templateUrl, waitUI) {
var d = new $.Deferred();
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getById(listName);
var folder = list.get_rootFolder();
var docName = newDocumentName && newDocumentName.length
? String.format("{0}", newDocumentName)
: String.format("New Document - {0}.docx", new Date().format("yyyy-mm-dd_hhMMss"));
/** replace illegal filename characters */
docName = docName.replace(/[~#!@£$%^&*()\{\}\\:,<>?/|"]+/gi, '-');
var docItem = templateUrl && templateUrl.length
? list.createDocumentFromTemplate(docName, folder, templateUrl)
: list.createDocument(docName, folder, docType || 1);
var wfUrl = docItem.getWOPIFrameUrl(SP.Utilities.SPWOPIFrameAction.edit);
var docFile = docItem.get_file();
ctx.load(list, "DefaultEditFormUrl");
ctx.load(docItem, "Id");
ctx.load(docFile, "ServerRelativeUrl", "CheckOutType");
ctx.executeQueryAsync(onCreateSuccess, onFail);
return d.promise();
function onCreateSuccess() {
var wopiUrl = wfUrl.get_value();
var docInfo = {
documentName: docName,
editDocumentPropertiesUrl: String.format("{0}?ID={1}&Source={2}", list.get_defaultEditFormUrl(), docItem.get_id(), _spPageContextInfo.webServerRelativeUrl),
fileServerRelativeUrl: docFile.get_serverRelativeUrl(),
wopiEditDocumentUrl: Boolean(wopiUrl) ? wopiUrl.replace(/wopiframe\.aspx/gi, "WOPIFrame2.aspx") : "",
};
console.info("Document: " + docInfo.documentName + " checked in.");
console.info("File: " + docInfo.fileServerRelativeUrl);
console.info("Edit properties url: " + docInfo.editDocumentPropertiesUrl);
console.info("WOPI edit document url: " + docInfo.wopiEditDocumentUrl);
var opts = {
url: docInfo.editDocumentPropertiesUrl,
args: null,
title: "Create New Document",
dialogReturnValueCallback: onDialogReturn.bind(docInfo),
};
if (waitUI) {
waitUI.close();
}
OpenPopUpPageWithDialogOptions(opts);
}
function onDialogReturn(returnValue, docObject) {
/**
docObject = {
isFolder: false,
newFileIcon: "icdocx.png",
newFileSize: 18420,
newFileUrl: "/sites/somesite/Documents/Document - 2017-43-17_030239.docx"
}
*/
console.info("returned from edit properties");
console.info(this);
console.info(returnValue);
console.info(docObject);
if (returnValue === SP.UI.DialogResult.OK) {
//GoToPage(this.wopiEditDocumentUrl);
}
var r = $.extend(docObject, this);
r.editPropertiesDialogReturn = returnValue;
d.resolve(r);
}
function onFail(c, b) {
console.error(b.get_message());
alert("Critical Error: " + b.get_message());
d.reject("Critical Error: " + b.get_message());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment