Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Friday, 13 June 2014

Upload Attachments To SharePoint List Programmatically

  private void UploadFileToList(SPListItem itemToAdd)
        {
            using (SPSite oSite = new SPSite(siteURL))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                   
                        oWeb.AllowUnsafeUpdates = true;
                        if (fuOption.HasFile)
                        {
                            HttpFileCollection uploads = HttpContext.Current.Request.Files;
                            SPAttachmentCollection attachments;
                            for (int i = 0; i < uploads.Count; i++)
                            {
                                Stream fs = uploads[i].InputStream;
                                byte[] fileContents = new byte[fs.Length];
                                fs.Read(fileContents, 0, (int)fs.Length);
                                fs.Close();
                                attachments = itemToAdd.Attachments;
                                string fileName = Path.GetFileName(uploads[i].FileName);
                                attachments.Add(fileName, fileContents);
                            }
                        }
                        oWeb.AllowUnsafeUpdates = false;
                  
                }
            }
        }

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.