深入浅出SharePoint——计算列如何使用Item的ID

问题描述:

 

 

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
 
namespace NCR
{
    class NCRListEventHandler: SPItemEventReceiver
    {
        /// <summary>
        /// Update NCRPrint calculated column so ID column is not blank. Without this new items have ID empty in NCRPrint column
        /// </summary>
        /// <param name="properties"></param>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            UpdateNCRPrintField(properties);
        }
 
        /// <summary>
        /// Without this the ID in the calculated column after update becomes 0 or empty
        /// </summary>
        /// <param name="properties"></param>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            UpdateNCRPrintField(properties);
        }
 
        private void UpdateNCRPrintField(SPItemEventProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(properties.SiteId))
                {
                    using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
                    {
                        SPField fldNCRPrint = web.Lists.GetList(properties.ListId, false).Fields.GetFieldByInternalName(Helper.FieldNames.NCRPrint.ToString());
                        fldNCRPrint.Update(true);
                    }
                }
            });
        }
    }
}

 

posted @ 2013-01-23 15:10  风影极光  阅读(588)  评论(0编辑  收藏  举报