{"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"],"_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}]}}