Thursday, March 31, 2011

Sharepoint 2010 Creating Colleagues (bidirectional) by code

This is a sample how impersonating the System Account a  two way colleagues relationship can be established by code:

Thanks to the replies of Tony for  error-when-trying-to-add-a-colleague-to-a-user-profile
Problem was that I actually got an error if I run his code with elevetad privilege, so slightly modified his solution.



using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;

namespace SharePointProject1.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void AddUser_Click(object sender, EventArgs e)
        {
                using (SPSite siteCol = new SPSite("MySiteUrl"))
                {
//Open the Site with the SystemAccount
                    using (SPSite siteAdmin = new SPSite(siteCol.ID,siteCol.SystemAccount.UserToken))
                    {
                        try
                        {
//This is really needed, else you get an exception
                           siteAdmin.AllowUnsafeUpdates = true;
                            using (SPWeb spwebAdmin = siteAdmin.OpenWeb())
                            {
                               //I didn't test if this 3 line are really needed
                                SPUser inviter = spwebAdmin.EnsureUser(@"Domain\User1");
                                SPUser invited = spwebAdmin.EnsureUser(@"Domain\User2");
                                spwebAdmin.Update();
                                SPServiceContext ctx = SPServiceContext.GetContext(siteAdmin);

                                //Get the user'sprofilemanager from the current context and ignore privacy
                                UserProfileManager upcm = new UserProfileManager(ctx, true);
                                //Gettheuserprofile
                                UserProfile userInvitee = upcm.GetUserProfile(@"Domain \User1");
                                UserProfile userInviter = upcm.GetUserProfile(@"Domain \User2");
                                //updateinviter
                                if (!userInviter.Colleagues.IsColleague(userInvitee.ID))
                                {

                                    Colleague newColleague = userInviter.Colleagues.Create(userInvitee,ColleagueGroupType.General,"General", false,
Privacy.Public);
                                    newColleague.Commit();
                                    userInviter.Commit();
                                }
                                //updateinvitee
                                if (!userInvitee.Colleagues.IsColleague(userInviter.ID))
                                {
                                    Colleague newColleague = userInvitee.Colleagues.Create(userInviter,ColleagueGroupType.General,"General", false,Privacy.Public);
                                    newColleague.Commit();
                                    userInvitee.Commit();
                                }

                            }
                        }
                        finally
                        {
                            siteAdmin.AllowUnsafeUpdates = false;
                        }
                    }
                }
        }

    }
}


.Hope it works for you guys too