OpenXmlPackage.addRelationship(relationshipId, relationshipType, target, internalOrExternal)

Return to the
Open XML SDK for JavaScript
Developer Center
Adds the specified relationship to the package.

Syntax

    openXmlDoc.addRelationship(relationshipId, relationshipType, target, internalOrExternal)

Arguments

    relationshipId: The id of the relationship. Needs to be unique in the set of relationships from the package to parts.
    Typically there are only three relationships from the package to parts:
    1. To the main document part, the workbook part, or the presentation part.
    2. To the core file properties part
    3. To the extended file properties part.
    
    relationshipType: The relationship type of the new relationship. openXml.relationshipTypes contains a list
        of most commonly used relationship types.
    
    target: A string that contains either a relative or absolute path to the target part.
    
    internalOrExternal: A string that contains either "Internal" or "External" depending on the type of relationship.

Usage

    var newCommentPart = doc.addPart("/comments.xml", openXml.contentTypes.wordprocessingComments,
        "xml", comments);
    mainPart.addRelationship("rId9999",
        openXml.relationshipTypes.wordprocessingComments,
        "../comments.xml", "Internal");

Example

var doc = new openXml.OpenXmlPackage(blankDocument_base64);
var mainPart = doc.mainDocumentPart();
var xDoc = mainPart.getXDocument();
var firstPara = xDoc.descendants(W.p).firstOrDefault();
var newFirstPara = new XElement(W.p,
    firstPara.attributes(),
    new XElement(W.commentRangeStart,
        new XAttribute(W.id, "1")),
    new XElement(W.r,
        new XElement(W.t, "This is a paragraph that contains a comment.")),
    new XElement(W.r,
        new XElement(W.commentReference,
            new XAttribute(W.id, "1"))),
    new XElement(W.commentRangeEnd,
        new XAttribute(W.id, "1")));
firstPara.replaceWith(newFirstPara);
// Create the content for the new comments part.
var comments = new XElement(W.comments,
    new XAttribute(XNamespace.xmlns + "w", wNs.namespaceName),
    new XElement(W.comment,
        new XAttribute(W.id, "1"),
        new XElement(W.p,
            new XElement(W.r,
                new XElement(W.t, "A comment")))));
// Create the new comments part.
var newCommentPart = doc.addPart("/comments.xml",
    openXml.contentTypes.wordprocessingComments,
    "xml", comments);
// This example shows adding a relationship to a part.  Adding a
// relationship to the package follows the exact same pattern.
mainPart.addRelationship("rId9999",
    openXml.relationshipTypes.wordprocessingComments,
    "../comments.xml", "Internal");
return {
    returnedDocument: doc.saveToBase64(),
    defaultDocumentName: "DocumentWithComment.docx",
    output: ["This example added a comment to the document.",
        "Click the 'Save' button to save the modified document to a local drive.  If you do not see a 'Save' button, you do not have Flash available, and can't save the document."]
};