Thursday, May 8, 2008

Add Colleague programatically

Colleague is one of the social networking feauture of MOSS 2007. MOSS 2007 provides an easy way to create and discover your network of friends and colleagues automatically. MOS 2007 automatically detects your friends from your Outlook and Windows IM contacts. It also detects your colleagues from Exchange distribution list and AD groups.
UserProfile object provides interface to add new colleague to your profile programatically. Following code shows how to add Colleague:

//Open SPSite
using (SPSite site = new SPSite("http://ServerName/Sitename"))
{
//Get Server context
ServerContext context = ServerContext.GetContext(site);
//Get UserProfileManager for the site cocntext
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile userProfile;
if (profileManager.UserExists("DomainName\\UserAccountName"))
{
userProfile = profileManager.GetUserProfile("DomainName\\UserAccountName");
//Check whether the colleague profile exists or not
if (profileManager.UserExists("DomainName\\ColleagueAccountName"))
{
ColleagueManager colleagueManager;
UserProfile colleagueProfile;
colleagueProfile = profileManager.GetUserProfile("DomainName\\ColleagueAccountName");
colleagueManager = userProfile.Colleagues;
//Adds collegue with group type peer. This property is marked as public
colleagueManager.Create(colleagueProfile, ColleagueGroupType.Peer, "", false, Privacy.Public);
}
}
}

1 comments:

sandhya said...

How can we add colleagues to some other profile .
I used SPSecurity.RunWithElevatedPrivileges but it didnt help me.
can you please suggest me how can we add colleagues programmatically to some other user instead of self profile.