现在的位置: 首页 > 综合 > 正文

Manage ListItem attachment,include add attachment in listitem,list all attachments.(给ListItem增加附件)

2012年11月22日 ⁄ 综合 ⁄ 共 2435字 ⁄ 字号 评论关闭

        #region add a attachment into listitem.
        /// <summary>
        /// Author:李曦光(Bruce Lee)
        /// Created Time:2008-9-16
        /// Description:add a attachment into listitem.
        /// Mender:
        /// Modify Time:
        /// Modify Description:
        /// </summary>
        /// <param name="spWeb">a SPWeb object</param>
        /// <param name="spListItem">SPList name</param>
        /// <param name="intSPListItemId">SPListItem id</param>
        /// <param name="strAttachmentName">save attachment name</param>
        /// <param name="byteContent">byte attachment</param>
        /// <returns></returns>
        public bool AddAttachmentsInListItem(SPWeb spWeb, string strSPListName, int intSPListItemId, string strAttachmentName, byte[] byteContent)
        {
            bool boolReturn = false;
            try
            {
                SPList spList = spWeb.Lists[strSPListName];
                SPListItem spListItem = spList.GetItemById(intSPListItemId);
                if (spListItem.Attachments != null)
                {
                    spListItem.Attachments.Add(strAttachmentName, byteContent);
                    spListItem.Update();
                    boolReturn = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                spWeb.Dispose();
            }
            return boolReturn;
        }
        #endregion
        #region get attachments in listitem.
        /// <summary>
        /// Author:李曦光(Bruce Lee)
        /// Created Time:2008-9-16
        /// Description:get attachments in listitem.
        /// Mender:
        /// Modify Time:
        /// Modify Description:
        /// </summary>
        /// <param name="spWeb">a SPWeb object</param>
        /// <param name="spListItem">SPList name</param>
        /// <param name="intSPListItemId">SPListItem id</param>
        /// <returns></returns>
        public SPAttachmentCollection GetAttachmentsInListItem(SPWeb spWeb, string strSPListName, int intSPListItemId)
        {
            SPAttachmentCollection attach = null;
            try
            {
                SPList spList = spWeb.Lists[strSPListName];
                SPListItem spListItem = spList.GetItemById(intSPListItemId);
                if (spListItem.Attachments != null)
                {
                    attach = spListItem.Attachments;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                spWeb.Dispose();
            }
            return attach;
        }
        #endregion

抱歉!评论已关闭.