Insert headers and footers office.js api - ms-word

I am trying to insert header and footers into a document using the office js API in a task pane app. I cant seem to find a way to add headers and footers to the docs.

Headers and Footers inherit the Body class and can be accessed by initializing a Section. Here is sample from the Office-JS Snippet Explorer :
// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy sectionsCollection object.
var mySections = context.document.sections;
// Queue a commmand to load the sections.
context.load(mySections, 'body/style');
// Synchronize the document state by executing the queued-up commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
// Create a proxy object the primary header of the first section.
// Note that the header is a body object.
var myHeader = mySections.items[0].getHeader("primary");
// Queue a command to insert text at the end of the header.
myHeader.insertText("This is a header.", Word.InsertLocation.end);
// Queue a command to wrap the header in a content control.
myHeader.insertContentControl();
// Synchronize the document state by executing the queued-up commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
console.log("Added a header to the first section.");
});
});
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});

A question: how can you enable the word document to use the different header types? (First and Primary)? It seems I can set the contents of the First and Primary headers and footers, but, without manually changing the document it does not seem to use them.
I am not sure how to reword this (seems clear to me) :(, trying.
A word document uses the same header type for each page unless the document is changed to use different header types. ie, how do I replicate the attached image controls?
screen shot

Related

How to insert header for "FirstPage" of a Word document office.js api?

I use Word API to insert header into a document in Word 2016 (desktop version). Insert only works for primary header. The code runs without error for getHeader("firstPage") and getHeader("evenPages") but no result.
What can be wrong?
// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy sectionsCollection object.
var mySections = context.document.sections;
// Queue a commmand to load the sections.
context.load(mySections, 'body/style');
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
// Create a proxy object the primary header of the first section.
// Note that the header is a body object.
//var myHeader = mySections.items[0].getHeader("primary");
var myHeader = mySections.items[0].getHeader("firstpage");
//var myHeader = mySections.items[0].getHeader("evenpages");
// Queue a command to insert text at the end of the header.
myHeader.insertText("This is a header.", Word.InsertLocation.end);
// Queue a command to wrap the header in a content control.
myHeader.insertContentControl();
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
console.log("Added a header to the first section.");
});
});
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
When I try case with code line
var myHeader = mySections.items[0].getHeader("firstpage");
And I expect to get the Header on the first page only.

What is the equivalent method of Word.Body.getOoxml() in c# open xml sdk?

I need to read the contents of the header in a word document using c# and save it somewhere and later insert it to another word document using javascript wordapi.
I know that in "officejs wordapi" we can use insertOoxml method to insert into header for example: getHeader("Primary").insertOoxml(ooxmlStringFetchedFromServer, Word.InsertLocation.replace);
But how can i get this ooxml using c#?
Also i already tried OuterXml property of Header object but apparently its not an ooxml.
Update:
I figure out with this code we can get the right format to be used in javascript wordapi:
using (WordprocessingDocument word = WordprocessingDocument.Open(#"C:\sample.docx", false))
{
var ooxmlStringFetchedFromServer = word.MainDocumentPart.HeaderParts.FirstOrDefault().OpenXmlPackage.ToFlatOpcString();
}
So the correct format is "FlatOpc".
Now i can use the following javascript code to add this FlatOpc to the Word document:
async function run() {
await Word.run(async (context) => {
context.document.sections.getFirst().getHeader("Primary").insertOoxml(ooxmlStringFetchedFromServer, Word.InsertLocation.replace);
await context.sync();
});
}
But now i have another problem, which is the following code does not returning the FlatOpc of the Header section, instead it returns only the FlatOpc of the body section!
word.MainDocumentPart.HeaderParts.FirstOrDefault().OpenXmlPackage.ToFlatOpcString();

insert doc file into the current oppened document at cursor point (using insertFileFromBase64 )

Here I am trying to insert a doc file at cursor point from my Office Add-in to Word , but I could not find the proper solution.
The API has only three options :
bodyObject.insertFileFromBase64(base64File, insertLocation);
where insertLocation can be Start, End or Replace.
The options for Word.InsertLocation are:
Start: Prepend the inserted content before the existing content.
End: Append the inserted content after the existing contents.
Replace: Replace the existing content with the inserted content.
When you using bodyObject.insertFileFromBase64, you're scoping your call to the entire Body of the document. Calling this method will therefore not care about your cursor location.
I suspect that you really want here is rangeObject.insertFileFromBase64. This is scoped to a Range rather than the entire Body. You can fetch a range from the current selection (or if nothing is selected, your cursor location):
Word.run(function (context) {
// Queue a command to get the current selection and then
// create a proxy range object with the results.
var range = context.document.getSelection();
// Queue a commmand to insert base64 encoded .docx at the beginning of the range.
// You'll need to implement getBase64() to make this work.
range.insertFileFromBase64(getBase64(), Word.InsertLocation.start);
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('Added base64 encoded text to the beginning of the range.');
});
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});

how to capture the data sent by alloy ui io request in serveresource method?

Getting blank values for title and description in serveResource method.Is this the right way to send the parameters from io request?
After inserting blank values in database I have to reload the page to see the inserted values?So io-request is not ajax request?
<aui:script use="aui-base">
A.one('#<portlet:namespace/>save').on('click', function(event) {
var A = AUI();
var title=A.one('#<portlet:namespace/>title').val();
alert(title);
var description=A.one('#<portlet:namespace/>description');
var url = '<%= newJob.toString() %>';
A.io.request(
url,
{
method:'POST',
data: {
<portlet:namespace />title: title,
<portlet:namespace />description: description,
},
}
['aui-io-deprecated']
);
Liferay.Util.getOpener().<portlet:namespace/>closePopup('<portlet:namespace/>dialog');
});
AUI's io request is ajax request only.
You can get parameters in serveResource method using code below:
ParamUtil.get(resourceRequest, "NAMEOFPARAMETER");
Modify your javascript function and provide data attribute as below:
data: {
'<portlet:namespace />title': title,
'<portlet:namespace />description': description,
}
I assume both title and description are textfields. If so, description is missing a .val() call, or more appropriately, .get('value'). I didn't use a dialog/modal in my source, but the overall approach should be the same.
<script>
AUI().use('aui-base', 'aui-io-request', function(A){
A.one('#<portlet:namespace />save').on('click', function(event) {
var title= A.one('#<portlet:namespace />title').get('value');
var description=A.one('#<portlet:namespace />description').get('value');
var url = '<%=myResourceURL.toString()%>';
A.io.request(url,
{
method:'POST',
data: {
title: title,
description: description,
},
});
});
});
</script>
I'm still relatively new to Liferay and have had trouble with this as well. I've noticed that the data parameters are not in the parametersMap of the default ResourceRequest, as you have stated. Out of curiosity, I decided to use
UploadPortletRequest req = PortalUtil.getUploadPortletRequest(resourceRequest);
in the serveResource method and check it's parametersMap. The title and description parameters are available therein. I'm still learning where and how to access data from Liferay objects, but it would seem that for the UploadPortletRequest to have the data, it would be plucked from somewhere within the default ResourceRequest ... where still remains elusive to me.
After inserting blank values in database I have to reload the page to see the inserted values?
You have to reload the page because a resource action does not trigger a page refresh. If you are manipulating data that you want reflected in some other "view" you'll need to configure the appropriate communication or use one of the other available url types that does trigger the doView method of your other "view".

Web Api returns garbage for text files unless run from the browser bar

I am writing a file service using Asp.Net’s Web Api. The service retrieves files (Css, Excel, Csv, etc.) from SQL Server and serves them up in response to Get requests.
My first test case is for Css files. The issue is that, while I can see the correct data on the server side, when the browser retrieves/decodes it, the results are mangled. The issue appears to be related to the encodings.
Here are the request/response headers in FireFox:
When I click on the response tab in FireBug, here’s what it looks like:
The results look like ascii being displayed as utf8. This is the html view in FireBug:
The above example is an iFrame inside a Facebook application which is running ssl.
If I take the url and open it directly in the browser, it works and correctly displays my Css:
In summary, when I retrieve my Css file from a tag inside my Facebook app, I get garbage (encoding issue?). If I retrieve it straight from the browser, it works.
My CssFormatter MediaTypeFormatter code:
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
var taskSource = new TaskCompletionSource<object>();
try
{
var incomingFile = value as FileRestService.Entity.IFile;
var ms = new MemoryStream(incomingFile.DataBuffer);
ms.CopyTo(writeStream);
ms.Flush();
taskSource.SetResult(writeStream);
}
catch (Exception e)
{
taskSource.SetException(e);
}
return taskSource.Task;
}
Am I creating the response stream incorrectly? I noticed that the response headers do not specify the encoding. Is this an issue?
I find the easiest way to handle this is to write something along the lines of (here's the important details):
public class Formatter : MediaTypeFormatter {
// TODO override the constructor to add some mappings or some other way for this formatter to be picked up
// TODO override CanReadType and CanWriteType according to your rules
public override void SetDefaultContentHeaders(Type t, HttpContentHeaders headers, string mediaType) {
base.SetDefaultContentHeaders(t, headers, mediaType);
headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
FileName = "SomeName.ext"
};
}
public override Task WriteToStreamAsync(Type t, object value, Stream s, HttpContentHeaders headers, TransportContext context) {
return Task.Factory.StartNew(() => {
// TODO code to write to the output stream, flush it but don't explicitly close it
});
}
}