Tuesday, October 6, 2015

AttachFile To SharePoint ListItem

AttachFile To SharePoint ListItem

  public void AttachFileToItem(SPListItem listItem)
        {
            SPAttachmentCollection attachmentCollection = listItem.Attachments;

            Stream attachmentStream;
            Byte[] attachmentContent;

            // Get the file from the fileupload control
            if (fileUplaod1.HasFile)
            {
                attachmentStream = fileUplaod1.PostedFile.InputStream;

                attachmentContent = new Byte[attachmentStream.Length];

                attachmentStream.Read(attachmentContent, 0, (int)attachmentStream.Length);

                attachmentStream.Close();
                attachmentStream.Dispose();

                // Add the file to the attachment collection
                attachmentCollection.Add(fileUplaod1.FileName, attachmentContent);
            }

            // Update the list item- SystemUpdate will not create a version
            listItem.SystemUpdate();
       
        }
    }

No comments:

Post a Comment