BruceLee
DocumentDiscussion

导航

 

        #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

posted on 2008-09-16 20:48  Bruce Lee  阅读(938)  评论(1编辑  收藏  举报