Tuesday, December 24, 2013

Changing social e-mail notification settings in SharePoint 2013 using the SharePoint server object model

I recently got the question whether it was possible to change the e-mail notification settings from the newsfeed functionality in SharePoint 2013 for all users – for those of you who don’t know this functionality check out Get email notifications about newsfeed items. Out of the box you will get a lot of e-mails for different actions on your newsfeed such as when someone has started following you, mentions, replies to conversations or community discusion posts, etc …

It actually is a UserProfile property called EmailOptin which controls this behavior but unfortunately there is not a lot of documentation about this property. The property contains an integer value that you need to set – the logic seems to be a bit strange but you need to set the value of the bit to 1 if you want the checkbox to be unchecked – the next example shows if you only want to

  Integer value to uncheck
Someone has started following me 2
Suggestions for people and keywords I might be interested in 4
Someone has mentioned me 8
Someone has replied to a conversation that I started 16
Somone replied to a conversation that I replied to 32
Someone replied to my community discussion post 64

This means that if you only want to uncheck the first option (Someone has started following me) you need to set it SPS-EmailOptin to 2. Listed below is a code sample for a console app which runs on the SharePoint server and which sets the SPS-EmailOptin property.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Threading.Tasks;
   6:  using Microsoft.SharePoint;
   7:  using Microsoft.Office.Server.UserProfiles;
   9:  namespace UserProfilesServerOM
  10:  {
  11:      class Program
  12:      {
  13:          static string strUrl = "http://portal.hpw.local";
  14:          static string strAccount = @"hpw\joris";
  16:          static void Main(string[] args)
  17:          {
  19:              // Get SPSite and service context from string 
  20:              SPSite site = new SPSite(strUrl);
  21:              SPServiceContext serviceContext = SPServiceContext.GetContext(site);
  23:              // Initialize user profile config manager object 
  24:              UserProfileManager upm = new UserProfileManager(serviceContext);
  27:              //To set prop values on user profile 
  28:              UserProfile u = upm.GetUserProfile(strAccount);
  29:              string sPropName = "SPS-EmailOptin";
  30:              //126 unchecks all options
  31:              u[sPropName].Value = 2;
  32:              u.Commit();
  34:              Console.WriteLine("Hit any key to continue...");
  35:              Console.ReadLine();
  36:          }
  37:      }
  38:  }

No comments: