{"id":4626,"date":"2024-08-31T07:16:39","date_gmt":"2024-08-31T07:16:39","guid":{"rendered":"https:\/\/himbap.com\/blog\/?p=4626"},"modified":"2024-08-31T07:16:39","modified_gmt":"2024-08-31T07:16:39","slug":"add-attachment-from-multipartcontent-stream","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=4626","title":{"rendered":"Add attachment from MultipartContent Stream"},"content":{"rendered":"\n<p><strong>Requirement<\/strong><\/p>\n\n\n\n<p>Create attachment in Dynamics 365 CE from the stream which will be available in MultipartContent response.<\/p>\n\n\n\n<p><strong>Details<\/strong><\/p>\n\n\n\n<p>Let&#8217;s say are getting MultipartContent in HttpResponse. This response contains stream which we want to use to create and attach pdf file in notes. We can use following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    if (httReqresponse != null &amp;&amp; httReqresponse.IsSuccessStatusCode)\n                    {\n  var multipartContent = await httReqresponse.Content.ReadAsMultipartAsync();\nif (multipartContent != null &amp;&amp; multipartContent.Contents.Count > 1)\n                                {\n                          HttpContent secondPart = multipartContent.Contents&#91;1];\n  var streamContent = await secondPart.ReadAsStreamAsync();\n byte&#91;] bytesArrary;\n            using (MemoryStream ms = new MemoryStream())\n            {\n                streamContent.CopyTo(ms);\n                bytesArrary= ms.ToArray();\n            }\n            \n                Entity notes = new Entity(\"annotation\");\n                notes&#91;\"objectid\"] = new EntityReference(\"&lt;&lt;Parenentitylookupname>>\", &lt;&lt;guid>>);\n                notes&#91;\"objecttypecode\"] = \"&lt;&lt;Name of parent entity>>\";\n                notes&#91;\"subject\"] = \"Notes Subject\";\n                notes&#91;\"documentbody\"] = Convert.ToBase64String(bytesArrary);\n\n                \/\/set mimetpe\n                notes&#91;\"mimetype\"] = @\"application\/pdf\";\n\n                notes&#91;\"filename\"] = \"FileName.pdf\";\n                service.Create(notes);\n            }\n       \n}\n}\n\n<\/code><\/pre>\n\n\n\n<p>The code first checks if the HTTP response is not null and if the response status code indicates success. The response content is expected to be in a multipart format which we are reading asynchronously. Once we have contents, we can read stream and create notes attachment.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Hope it will help someone !!<br><strong>Keep learning and Keep Sharing !!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Requirement Create attachment in Dynamics 365 CE from the stream which will be available in MultipartContent response. Details Let&#8217;s say are getting MultipartContent in HttpResponse. This response contains stream which we want to use to create and attach pdf file in notes. We can use following code: The code first checks if the HTTP response is not null and if&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=4626\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,402,6,296],"tags":[1095,1096,1094],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4626"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4626"}],"version-history":[{"count":2,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4626\/revisions"}],"predecessor-version":[{"id":4628,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4626\/revisions\/4628"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}