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

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

Syntax

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

Arguments

    relationshipId: The id of the relationship. Needs to be unique in the set of relationships from this part to other parts.

    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);
// Adding the relationship to a part.
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."]
};