basquang™ on clouds

July 3, 2009

Custom Current User Field

Filed under: SharePoint — basquang @ 3:57 AM

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;
            }
        }        
    }

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: