{"id":2127,"date":"2016-02-04T05:44:49","date_gmt":"2016-02-04T05:44:49","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=2127"},"modified":"2016-02-04T06:22:05","modified_gmt":"2016-02-04T06:22:05","slug":"openxmlpart-addrelationship","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/openxmlpart-addrelationship\/","title":{"rendered":"OpenXmlPart.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 part.<\/p>\n<h1>Syntax<\/h1>\n<pre>\r\n    part.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 this part to other parts.\r\n\r\n    relationshipType: The relationship type of the new relationship. openXml.relationshipTypes contains a\r\n        list 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\nvar newCommentPart = doc.addPart(\"\/comments.xml\",\r\n    openXml.contentTypes.wordprocessingComments,\r\n    \"xml\", comments);\r\nmainPart.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\/\/ Adding the relationship to a part.\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 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 [&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-2127","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2127","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=2127"}],"version-history":[{"count":3,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2127\/revisions"}],"predecessor-version":[{"id":2173,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2127\/revisions\/2173"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=2127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}