Thứ Sáu, 3 tháng 7, 2009

Custom Current User Field

In MOSS 2007 and WSS 3, we have “People and Group” field to specify User or Group. Some time we need to set default value for this. But It’s not default function of “People and Group”. You must create one custom field using Visual Studio 2008 Extensions for WSS 3 and Custom Field template. The code is very easy to write and understand:

public class CurrentUserFieldField : SPFieldUser
    {
        public CurrentUserFieldField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
        }
        
        public CurrentUserFieldField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
        }
        public override string DefaultValue
        {
            get
            {
                SPWeb web = SPContext.Current.Web;
                SPUser currentUser = web.CurrentUser;
                return string.Format("{0};#{1}", currentUser.ID.ToString(), currentUser.Name);
            }
            set
            {
                base.DefaultValue = value;
            }
        }        
    }

Không có nhận xét nào:

Đăng nhận xét