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

Sunday, 1 June 2014

Upload Attachements to SharePoint List Programmatically

in aspx:
<asp:UpdatePanel ID="upfuAttachment" runat="server" UpdateMode="conditional">
                <ContentTemplate>
                    <asp:FileUpload ID="fuAttachment" runat="server" />
                </ContentTemplate>
                <Triggers>
                    <asp:PostBackTrigger ControlID="btnSave" />
                </Triggers>
            </asp:UpdatePanel>
in Code behind:

SPWeb oWeb = SPContext.Current.Web;
            try
            {
                oWeb.AllowUnsafeUpdates = true;
                if (fuAttachment.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;
            }
            catch (Exception ex)
            {
               //throw ex;
            }

No comments:

Post a Comment

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