{"id":2074,"date":"2016-02-04T04:12:09","date_gmt":"2016-02-04T04:12:09","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=2074"},"modified":"2016-02-04T04:35:12","modified_gmt":"2016-02-04T04:35:12","slug":"openxmlpackage_addrelationship","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/openxmlpackage_addrelationship\/","title":{"rendered":"OpenXmlPackage.addRelationship(relationshipId, relationshipType, target, internalOrExternal)"},"content":{"rendered":"<p><span class=\"Back\"><a class=\"Back\" href=\"https:\/\/www.ericwhite.com\/blog\/open-xml-sdk-for-javascript\/\">Return to the<br \/>Open XML SDK for JavaScript<br \/>Developer Center<\/a><\/span>Adds the specified relationship to the package.<\/p>\n<h1>Syntax<\/h1>\n<pre>\r\n    openXmlDoc.addRelationship(relationshipId, relationshipType, target, internalOrExternal)\r\n<\/pre>\n<h1>Arguments<\/h1>\n<pre>\r\n    relationshipId: The id of the relationship. Needs to be unique in the set of relationships from the package to parts.\r\n    Typically there are only three relationships from the package to parts:\r\n    1. To the main document part, the workbook part, or the presentation part.\r\n    2. To the core file properties part\r\n    3. To the extended file properties part.\r\n    \r\n    relationshipType: The relationship type of the new relationship. openXml.relationshipTypes contains a list\r\n        of most commonly used relationship types.\r\n    \r\n    target: A string that contains either a relative or absolute path to the target part.\r\n    \r\n    internalOrExternal: A string that contains either \"Internal\" or \"External\" depending on the type of relationship.\r\n<\/pre>\n<h1>Usage<\/h1>\n<pre>\r\n    var newCommentPart = doc.addPart(\"\/comments.xml\", openXml.contentTypes.wordprocessingComments,\r\n        \"xml\", comments);\r\n    mainPart.addRelationship(\"rId9999\",\r\n        openXml.relationshipTypes.wordprocessingComments,\r\n        \"..\/comments.xml\", \"Internal\");\r\n<\/pre>\n<h1>Example<\/h1>\n<pre>\r\nvar doc = new openXml.OpenXmlPackage(blankDocument_base64);\r\nvar mainPart = doc.mainDocumentPart();\r\nvar xDoc = mainPart.getXDocument();\r\nvar firstPara = xDoc.descendants(W.p).firstOrDefault();\r\nvar newFirstPara = new XElement(W.p,\r\n    firstPara.attributes(),\r\n    new XElement(W.commentRangeStart,\r\n        new XAttribute(W.id, \"1\")),\r\n    new XElement(W.r,\r\n        new XElement(W.t, \"This is a paragraph that contains a comment.\")),\r\n    new XElement(W.r,\r\n        new XElement(W.commentReference,\r\n            new XAttribute(W.id, \"1\"))),\r\n    new XElement(W.commentRangeEnd,\r\n        new XAttribute(W.id, \"1\")));\r\nfirstPara.replaceWith(newFirstPara);\r\n\/\/ Create the content for the new comments part.\r\nvar comments = new XElement(W.comments,\r\n    new XAttribute(XNamespace.xmlns + \"w\", wNs.namespaceName),\r\n    new XElement(W.comment,\r\n        new XAttribute(W.id, \"1\"),\r\n        new XElement(W.p,\r\n            new XElement(W.r,\r\n                new XElement(W.t, \"A comment\")))));\r\n\/\/ Create the new comments part.\r\nvar newCommentPart = doc.addPart(\"\/comments.xml\",\r\n    openXml.contentTypes.wordprocessingComments,\r\n    \"xml\", comments);\r\n\/\/ This example shows adding a relationship to a part.  Adding a\r\n\/\/ relationship to the package follows the exact same pattern.\r\nmainPart.addRelationship(\"rId9999\",\r\n    openXml.relationshipTypes.wordprocessingComments,\r\n    \"..\/comments.xml\", \"Internal\");\r\nreturn {\r\n    returnedDocument: doc.saveToBase64(),\r\n    defaultDocumentName: \"DocumentWithComment.docx\",\r\n    output: [\"This example added a comment to the document.\",\r\n        \"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.\"]\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Return to theOpen XML SDK for JavaScriptDeveloper CenterAdds 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_s2mail":"","footnotes":""},"class_list":["post-2074","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/comments?post=2074"}],"version-history":[{"count":3,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2074\/revisions"}],"predecessor-version":[{"id":2078,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2074\/revisions\/2078"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=2074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}