Hi All,
I am currently trying real hard on how to write a event handler to populate a person & group field with current user logged in when i upload or create a new file that requires to be checked in before i publish it. I would also need to populate the Single Text Field with the current user's department.
Many Thanks Ahead!
Loading

Sam HobbsRank #107 · 20.5K reputationPosted Oct 22, 2010, 8:24 PM EDT
Do you still need help? If so, then are you now asking for help with a event handler for the ItemUpdated Event?
Suthish NairRank #92 · 22.8K reputationPosted Oct 20, 2010, 1:41 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Oct 20, 2010, 2:36 PM EDT
Peterson Yap35 reputationPosted Oct 22, 2010, 7:06 PM EDT
Below is the code i wrote into a ItemAdded Event Handler, it is working fine now..
public override void ItemAdded(SPItemEventProperties properties)
{
try
{
SPWeb web = properties.OpenWeb();
SPListItem doc = properties.ListItem;
doc["Document Owner"] = web.CurrentUser;
doc["Document Author"] = web.CurrentUser;
//if the field is people and group, can load name to show department too
// doc["DocDepartment"] = web.CurrentUser;
//loads mainsite with the current page's site URL
SPSite mainSite = new SPSite(properties.WebUrl);
//loads the ProfileManager of current site
UserProfileManager mngr = new UserProfileManager(ServerContext.GetContext(mainSite));
//Get the profile of the user that is retrieved above
UserProfile up = mngr.GetUserProfile(web.CurrentUser.LoginName);
//stores the department value of the above user
string departmentVal = up["Department"].Value as string;
doc["Document Owner Department"] = departmentVal;
doc.Update();
}
catch (Exception ex)
{
Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Item Added Exception : {0}", ex.StackTrace);
}
}
Peterson Yap35 reputationPosted Oct 28, 2010, 12:09 AM EDT
Cheers everyone!