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 HobbsPosted Oct 23, 2010, 12:24 AM
Do you still need help? If so, then are you now asking for help with a event handler for the ItemUpdated Event?
Peterson YapPosted Oct 28, 2010, 4:09 AM
Cheers everyone!
Peterson YapPosted Oct 22, 2010, 11:06 PM
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);
}
}
Sam HobbsPosted Oct 20, 2010, 6:36 PM
Suthish NairPosted Oct 20, 2010, 5:41 AM