Friday, February 13, 2015

Update list item with multiple attachment using server object model

SPList oList = oWeb.Lists.TryGetList(LISTNAME);
SPListItem oItem = oList.Items.Add();
oItem["Title"] = "Sample text";
oItem.Update();

HttpFileCollection hfc = HttpContext.Current.Request.Files;
for (int iCount = 0; iCount < hfc.Count; iCount++)
{
    {
        HttpPostedFile hpf = hfc[iCount];
        if (!String.IsNullOrEmpty(hpf.FileName))
        {
            Stream fStream = hpf.InputStream;
            byte[] contents = new byte[fStream.Length];
            fStream.Read(contents, 0, (int)fStream.Length);
            fStream.Close();
            fStream.Dispose();

            SPAttachmentCollection attachments = oItem.Attachments;
            string fileName = Path.GetFileName(hpf.FileName);
            attachments.Add(fileName, contents);
        }
    }
}
oItem.Update();

No comments:

Post a Comment

How to launch a document by clicking a link to the file?

Word: <a href='ms-word:ofe|u|path/to/web/word/document.docx'>Link to document</a> Excel: <a href='ms-excel:o...