{"id":15,"date":"2007-08-23T23:57:00","date_gmt":"2007-08-24T04:57:00","guid":{"rendered":"http:\/\/blogs.devhorizon.com\/reza\/?p=15"},"modified":"2007-11-21T17:36:29","modified_gmt":"2007-11-21T22:36:29","slug":"on-the-fly-creation-of-attachments-for-list-items","status":"publish","type":"post","link":"https:\/\/blogs.devhorizon.com\/reza\/2007\/08\/23\/on-the-fly-creation-of-attachments-for-list-items\/","title":{"rendered":"On-the-fly creation of attachments for list items"},"content":{"rendered":"<p>Here is the code to add an item to a list (task list in this example) programmatically and create and attach a file on the fly. You basically need to encode a set of characters into a sequence of bytes (byte array) and call SPAttachmentCollection.Add method to add the binary representation of your attachment to the list item.<\/p>\n<blockquote><p>StringBuilder sbLog = new StringBuilder();<\/p>\n<p> sbLog.Append(&#8220;Accessing root web of the site collection&#8230;\\n&#8221;);<br \/>\n SPWeb web = new SPSite(&#8220;http:\/\/moss:21165&#8221;).OpenWeb();<\/p>\n<p> sbLog.Append(&#8220;Accessing Task list in the root web&#8230;\\n&#8221;);<br \/>\n SPList taskList = web.Lists[&#8220;Tasks&#8221;];<\/p>\n<p> sbLog.Append(&#8220;Adding a new task item&#8230;\\n&#8221;);<br \/>\n SPListItem newTask = taskList.Items.Add();<\/p>\n<p> sbLog.Append(&#8220;Populating the fields&#8230;\\n&#8221;);<br \/>\n newTask[&#8220;Title&#8221;] = &#8220;Work hard ,but play harder&#8221;;<br \/>\n newTask[&#8220;Due Date&#8221;] = DateTime.Now;<br \/>\n newTask[&#8220;Priority&#8221;] = &#8220;(1) High&#8221;;<\/p>\n<p> sbLog.Append(&#8220;Creating the attachment&#8230;\\n&#8221;);<br \/>\n ASCIIEncoding encoder = new ASCIIEncoding();<br \/>\n byte[] bytFile = encoder.GetBytes(sbLog.ToString());<br \/>\n newTask.Attachments.Add(&#8220;log.txt&#8221;, bytFile);<br \/>\n newTask.Update();<\/p><\/blockquote>\n<p> <img loading=\"lazy\" decoding=\"async\" width=\"1\" src=\"\/\/blogs.devhorizon.com\/aggbug.aspx?PostID=497\" height=\"1\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is the code to add an item to a list (task list in this example) programmatically and create and attach a file on the fly. You basically need to encode a set of characters into a sequence of bytes (byte array) and call SPAttachmentCollection.Add method to add the binary representation of your attachment to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[3],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-moss-2007","tag-moss-2007"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>On-the-fly creation of attachments for list items - Reza Alirezaei&#039;s Blog %<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blogs.devhorizon.com\/reza\/2007\/08\/23\/on-the-fly-creation-of-attachments-for-list-items\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Reza Alirezaei\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/08\\\/23\\\/on-the-fly-creation-of-attachments-for-list-items\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/08\\\/23\\\/on-the-fly-creation-of-attachments-for-list-items\\\/\"},\"author\":{\"name\":\"Reza Alirezaei\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"headline\":\"On-the-fly creation of attachments for list items\",\"datePublished\":\"2007-08-24T04:57:00+00:00\",\"dateModified\":\"2007-11-21T22:36:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/08\\\/23\\\/on-the-fly-creation-of-attachments-for-list-items\\\/\"},\"wordCount\":160,\"commentCount\":0,\"keywords\":[\"MOSS 2007\"],\"articleSection\":[\"MOSS 2007\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/08\\\/23\\\/on-the-fly-creation-of-attachments-for-list-items\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/08\\\/23\\\/on-the-fly-creation-of-attachments-for-list-items\\\/\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/08\\\/23\\\/on-the-fly-creation-of-attachments-for-list-items\\\/\",\"name\":\"On-the-fly creation of attachments for list items - Reza Alirezaei's Blog %\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\"},\"datePublished\":\"2007-08-24T04:57:00+00:00\",\"dateModified\":\"2007-11-21T22:36:29+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/08\\\/23\\\/on-the-fly-creation-of-attachments-for-list-items\\\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/\",\"name\":\"Reza Alirezaei's Blog\",\"description\":\"Blogging from the field!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\",\"name\":\"Reza Alirezaei\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"caption\":\"Reza Alirezaei\"},\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/author\\\/rezaa\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"On-the-fly creation of attachments for list items - Reza Alirezaei's Blog %","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blogs.devhorizon.com\/reza\/2007\/08\/23\/on-the-fly-creation-of-attachments-for-list-items\/","twitter_misc":{"Written by":"Reza Alirezaei","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/08\/23\/on-the-fly-creation-of-attachments-for-list-items\/#article","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/08\/23\/on-the-fly-creation-of-attachments-for-list-items\/"},"author":{"name":"Reza Alirezaei","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"headline":"On-the-fly creation of attachments for list items","datePublished":"2007-08-24T04:57:00+00:00","dateModified":"2007-11-21T22:36:29+00:00","mainEntityOfPage":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/08\/23\/on-the-fly-creation-of-attachments-for-list-items\/"},"wordCount":160,"commentCount":0,"keywords":["MOSS 2007"],"articleSection":["MOSS 2007"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blogs.devhorizon.com\/reza\/2007\/08\/23\/on-the-fly-creation-of-attachments-for-list-items\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/08\/23\/on-the-fly-creation-of-attachments-for-list-items\/","url":"https:\/\/blogs.devhorizon.com\/reza\/2007\/08\/23\/on-the-fly-creation-of-attachments-for-list-items\/","name":"On-the-fly creation of attachments for list items - Reza Alirezaei's Blog %","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#website"},"datePublished":"2007-08-24T04:57:00+00:00","dateModified":"2007-11-21T22:36:29+00:00","author":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blogs.devhorizon.com\/reza\/2007\/08\/23\/on-the-fly-creation-of-attachments-for-list-items\/"]}]},{"@type":"WebSite","@id":"https:\/\/blogs.devhorizon.com\/reza\/#website","url":"https:\/\/blogs.devhorizon.com\/reza\/","name":"Reza Alirezaei's Blog","description":"Blogging from the field!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blogs.devhorizon.com\/reza\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938","name":"Reza Alirezaei","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","caption":"Reza Alirezaei"},"url":"https:\/\/blogs.devhorizon.com\/reza\/author\/rezaa\/"}]}},"_links":{"self":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":0,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}