Office Template Create Document
As described in Office Template Engine MatchPoint provides a mechanism that allows the evaluation of expressions on a Microsoft Word document. This can be achieved by creating a new document based on a selected Document template.
Creating Office Documents with the Expression Engine
A new document can be created based on a selected Document template by using the following Expression:
//Creates a document based on a given document template in the default Shared Document library. Returns a string for validation errors or a success message."
MPUtility.CreateDocument(<TargetWebUrl>, <TemplateUrl>, <TargetExtension>, <DocumentTitle>, <OpenDocument>)
//Creates a document based on a given document template. Returns a string for validation errors or a success message.
MPUtility.CreateDocument(<TargetWebUrl>, <WebRelativeListOrFolderUrl>, <TemplateUrl>, <TargetExtension>, <DocumentTitle>, <OpenDocument>)
TargetWebUrl
specifies the URL that points to the SPWeb where the new document should be created.WebRelativeListOrFolderUrl
specifies the document library or folder, within the specified SPWeb, that should be the location where the document is created. e.g. "Shared Documents\Folder1"TemplateUrl
specifies the full path URL that points to the Office document used as a template.TargetExtension
specifies the extension of the document. Not mandatory so it can be set as an empty string "".DocumentTitle
specifies the Name of the document that is about to be created.OpenDocument
is a boolean field that indicates if the document should be directly opened after creation or not.
Return values:
- On failure:
- Document name not specified
- Document Template \ Folder Url not specified
- Target Document Library not specified
- Target Folder not found
On Success:
- Html that contains the Success message, the open document hyperlink element and the open containing document library hyperlink element.
Your document '<DocumentTitle>' has been created successfully. <a href="javascript:void(0)" onclick="javascript:$$.Opener.OpenDocument(this, '<DocumentUrl>', 'SharePoint.OpenDocuments', 'ms-word')">Open</a> '<DocumentTitle>' or go to the <a href="<WebRelativeListUrl>"><WebRelativeList></a>.
- If OpenDocument is set as true, the document will be automatically opened by appending the following code:
<script type="text/javascript">javascript:$$.Opener.OpenDocument(this, '<DocumentUrl>', 'SharePoint.OpenDocuments', 'ms-word')</script>
Returned messages need to be appended to an html element. The following example describes a way to do this. Check for ```html mp$(".mp-information").html(r.Value); ```
The following example shows how an office template can be created, i.e. within a Composite Web Part:
<?xml version="1.0" encoding="utf-8"?>
<CompositeConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title><![CDATA[MP Create Document]]></Title>
<DataProvider xsi:type="ListDataProvider">
<ListUrlExpression><![CDATA["http://demo.mp.local/sites/test/Shared Documents"]]></ListUrlExpression>
<Scope>Default</Scope>
<RunAsUser CredentialId="" />
<RowLimit>100</RowLimit>
<CacheKey>658a1999-2aed-421e-a9eb-dbd0a526080a</CacheKey>
</DataProvider>
<SelectionMode>None</SelectionMode>
<LoadingMode>OnRender</LoadingMode>
<ShowProgressIndicator>true</ShowProgressIndicator>
<DisableAjaxPaging>false</DisableAjaxPaging>
<Transformer xsi:type="PatternTransformer">
<HeaderTemplate><![CDATA[<span style="display:none">Search</span><div class="mp-templateRow">
<script type="text/javascript">
function CreateDocument(element)
{{
var rowElement = mp$(element).closest(".mp-document-info-form");
var templateUrl = document.getElementById("docTemplateUrl").value;
var documentTitle = document.getElementById("documentTitle").value;
var documentLibrary = document.getElementById("documentLibrary").value;
var webUrl = document.getElementById("webUrl").value;
$$.Invoke("MPUtility.CreateDocument", webUrl, documentLibrary, templateUrl, "", documentTitle, true, function (r)
{{
mp$(".mp-information")[0].style.display = "block";
mp$(".mp-information").html(r.Value);
}});
}}
function openForm(docTemplateUrl, webUrl) {{
document.getElementById("myForm").style.display = "block";
document.getElementById("docTemplateUrl").value = docTemplateUrl;
document.getElementById("webUrl").value = webUrl;
}}
function closeForm() {{
document.getElementById("myForm").style.display = "none";
}}
</script>
<div class="form-popup" id="myForm">
<form class="mp-document-info-form">
<h2>Create document</h2>
<input type="hidden" name="docTemplateUrl" id="docTemplateUrl">
<input type="hidden" name="webUrl" id="webUrl">
<label for="title"><b>Title</b></label>
<input type="text" placeholder="Document title" id="documentTitle" name="title" required>
<label for="doclib"><b>Document Library</b></label>
<input type="text" value="Shared Documents" id="documentLibrary" placeholder="Specify document library" name="doclib">
<button type="button" onclick="CreateDocument(this)" class="btn">Create</button>
<button type="button" class="btn cancel" onclick="closeForm()">Close</button>
</form>
<div class="mp-information">
</div>
</div>]]></HeaderTemplate>
<FooterTemplate><![CDATA[</div>
<style>
{{box-sizing: border-box;}}
.mp-information {{
display: none;
}}
/* The popup form - hidden by default */
.form-popup {{
display: none;
bottom: 0;
right: 15px;
max-width: 500px;
border: 3px solid #f1f1f1;
z-index: 9;
}}
/* Add styles to the form container */
.mp-document-info-form {{
max-width: 300px;
padding: 10px;
background-color: white;
}}
/* Full-width input fields */
.mp-document-info-form input[type=text] {{
width: 100%;
padding: 15px;
margin: 5px 0 22px 0;
border: none;
background: #f1f1f1;
}}
/* When the inputs get focus, do something */
.mp-document-info-form input[type=text]:focus {{
background-color: #ddd;
outline: none;
}}
/* Set a style for the submit button */
.mp-document-info-form .btn {{
background-color: #4CAF50;
color: white;
padding: 16px 20px;
border: none;
cursor: pointer;
width: 100%;
margin-bottom:10px;
opacity: 0.8;
}}
/* Add some hover effects to buttons */
.mp-document-info-form .btn:hover, .open-button:hover {{
opacity: 1;
}}
</style>]]></FooterTemplate>
<RowTemplate><![CDATA[<button type="button" onclick="openForm('{DataItem.Url}', '{Web.Url}')" class="mp-templateItem" data-TemplateUrl="{DataItem.Url}">{DataItem.Name}</button>
<br/>]]></RowTemplate>
<EmptyDataTemplate><![CDATA[No Items found]]></EmptyDataTemplate>
</Transformer>
<PageSizeExpression><![CDATA[0]]></PageSizeExpression>
</CompositeConfiguration>