Wednesday, November 28, 2012

Disable Eamil Notification When Adding Colleague in SharePoint 2010

By default SharePoint 2010 an email notification will be sent out when a user has added the other user as a colleague. This may not be a good idea for a big company. Consider the case that thousands of employees may add the CEO as colleague so they could follow what CEO's news feed. That would be a huge spam for the CEO. Of course anyone can go to his/her mysite and change that email notification option:



IT forks have no problem to figure out all that, but most business people in a company are not technical guys, they are busy for other stuff, and have no time or no willing to explore tons of options available in SharePoint settings. Then you may get a request to disable the email notification for all employees.

The easiest way to do that is use Powershell script to update each user's profile. Create a script called "CreateMysites.ps1":
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
#[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")

$site = Get-SPSite "http://mysites.company.com";
$ServerContext = Get-SPServiceContext $site;
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServerContext);
$ProfileEnumerators = $ProfileManager.GetEnumerator();

#Go through each user profile and update its setting
foreach ($profile in $ProfileEnumerators)
{
    try {
        $AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value
        if ($AccountName.ToLower().StartsWith("mydomain\")) 
        {
            #Three values map to three options shown in above screen-shot: 0 is ON and 1 is OFF
            $profile["SPS-EmailOptin"].Value = 010;
            $profile.Commit();
        }
    }
    catch [Exception] {
      write-host $_.Exception.Message;
    }
} 
$site.Dispose();
The script can be executed in SharePoint 2010 Management Shell directly. The powershell script can also be run by Windows Scheduled task. You just need to create a batch file "CreateMysites.bat" to start the powershell:
PowerShell.exe -command C:\Schedule\CreateMysites.ps1
To setup a schedule to run the script in Windows, go to SharePoint server => Administrative Tools => Task Scheduler, click Create Task action from the right-hand side panel, give a task name, set the schedule as daily, weekly or whatever you need in the Triggers tab, and then add a new Action in the Actions tab: