0

We wanted to unset the manager changes activity feed for all the users are in the SharePoint 2010 farm, have used the below code

using System;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.Office.Server.ActivityFeed;

namespace MysiteActivityOFF
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
           {
               string currentSite = "http://weburl";
               using (SPSite aSite = new SPSite(currentSite))
               {
                   SPServiceContext currentContext = SPServiceContext.GetContext(aSite);
                   UserProfileManager userProfMan = new UserProfileManager(currentContext);
                   ActivityType activityType = null;
                   string userName = Environment.UserDomainName + "\\" + Environment.UserName;
                   UserProfile currentUser = userProfMan.GetUserProfile(userName);
                   ActivityManager activityMan = new ActivityManager(currentUser, currentContext);
                   bool rights = activityMan.PrepareToAllowSchemaChanges();<br/>
                   if (rights)
                   {
                       try
                       {
                           activityType = activityMan.ActivityTypes["ManagerChange"];
                           activityType.IsPublished = false;
                           activityType.Commit();
                           activityType.Refresh(true);
                           Console.WriteLine("Manager change activity successfully disabled for user : " + currentUser.DisplayName);
                       }
                       catch (Exception ex)
                       {
                           Console.WriteLine(ex.StackTrace);
                       }
                   }
                   Console.ReadKey(true);
               }
           });
        }
    }
}

however it throws the below error:

ERROR :
=======
In the "activityType.Commit();" line the exception occurs at :

Message :
ActivityManager.ActivityFeedSchemaMapping Exception
Inner Exception :
Procedure or function activityFeed_UpdateActivityTypes has too many arguments specified. StackTrace :
at
Microsoft.Office.Server.ActivityFeed.ActivityFeedSchemaMapping.InsertOrUpdateOne(ActivityFeedSchemaMapping[] mapping, Object obj, String sproc, SqlSession sqlSession, Boolean isInsert)
at Microsoft.Office.Server.ActivityFeed.ActivityType.Commit()

Powershell:<br/>
$loginname = "domain\account";<br/>
$site = get-spsite https://url<br/>
$context = Get-SPServiceContext $site;<br/>
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context);<br/>
$profile = $upm.GetUserProfile([string]$loginname);<br/>
$am = New-Object Microsoft.Office.Server.ActivityFeed.ActivityManager($profile, $context);<br/>
$am.PrepareToAllowSchemaChanges();<br/>
$mgrChange = $am.ActivityTypes["ManagerChange"];<br/>
$mgrChange.IsPublished = $false<br/>
$mgrChange.Commit()<br/>

Error:

Exception calling "Commit" with "0" argument(s): "ActivityManager.ActivityFeedSchemaMapping Exception" At line:1 char:18 + $mgrChange.Commit <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

Update-2:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System") 
$loginname = "domain\user"
$site = Get-SPSite "URL"

$context = Get-SPServiceContext $site;
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context);
$profile = $upm.GetUserProfile([string]$loginname);

$am = New-Object Microsoft.Office.Server.ActivityFeed.ActivityManager($profile, $context)
$apptList = New-Object System.Collections.Generic.List[Microsoft.Office.Server.ActivityFeed.ActivityPreferencePerType]

$am.ActivityTypes | ForEach-Object {
  $appt = New-Object Microsoft.Office.Server.ActivityFeed.ActivityPreferencePerType
  $appt.ActivityType = $_
  $appt.IsSet = $false
  $apptList.Add($appt)
}
$am.ActivityPreferences.SetActivityPreferencesPerType($apptList)
$am.ActivityPreferences.Commit()
$am.ActivityPreferences.Refresh()

Irrespective of the user, it updates the service account profile(on which logged into the server). what could be the issue?

1 Answer 1

2
+25

The class is ActivityPreference. ActivityManager class accepts userprofile object but it has to match the current user (see this). If not it seems to retrive from httpcontext or windows identity. Also it looks like it cannot be impersonated with system account. To update the propety under user context use something like below.

        string siteUrl = "http://site/";
        string managerType = "ManagerChange";


        using (SPSite site = new SPSite(siteUrl))
        {
            SPServiceContext serviceContext = SPServiceContext.GetContext(site);
            UserProfileManager profileManager = new UserProfileManager(serviceContext);
            UserProfile profile = profileManager.GetUserProfile("domain\\username");

            ActivityManager activityManager = new ActivityManager(profile, serviceContext);
            List<ActivityPreferencePerType> apptc = new List<ActivityPreferencePerType>();

            foreach (ActivityType type in activityManager.ActivityTypes)
            {
                ActivityPreferencePerType appt = new ActivityPreferencePerType();
                appt.ActivityType = type;
                appt.IsSet = false;

                if (type.ActivityTypeName.ToLower() == managerType.ToLower())
                {
                    appt.IsSet = false;
                }
                else
                {
                    appt.IsSet = true;
                }
                apptc.Add(appt);
            }
            ActivityPreferencesCollection coll = activityManager.ActivityPreferences;

            coll.SetActivityPreferencesPerType(apptc);
            coll.Commit();
            coll.Refresh();

In PowerShell (using reflection to set preference for another user).

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System")

$loginname = "domain\testuser"
$site = Get-SPSite "http://site"

$context = Get-SPServiceContext $site;
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context);
#$myprofile = $upm.GetUserProfile("domain\myusername")
$profile1 = $upm.GetUserProfile([string]$loginname)

$am = New-Object Microsoft.Office.Server.ActivityFeed.ActivityManager($myprofile1, $context)

$type = $am.GetType()
$methodInfo = $type.GetMethod("CopyBasicUserInfo", [reflection.bindingflags]"nonpublic,instance", $null, $profile1.GetType(), $null)
$methodInfo.Invoke($am, $profile1)

$apptList = New-Object System.Collections.Generic.List[Microsoft.Office.Server.ActivityFeed.ActivityPreferencePerType]

$am.ActivityApplications["UserProfileChange_Gatherer"].ActivityTypes | ForEach-Object {
  $appt = New-Object Microsoft.Office.Server.ActivityFeed.ActivityPreferencePerType

  $appt.ActivityType = $_

  if($_.ActivityTypeName -eq "ManagerChange") {
    $appt.IsSet = $false
  }
  else {
    $appt.IsSet = $true
  }
  $apptList.Add($appt)
}
$am.ActivityPreferences.SetActivityPreferencesPerType($apptList)
$am.ActivityPreferences.Commit()
$am.ActivityPreferences.Refresh()

This blog talks about it in more detail.

9
  • Great help..I wounder if we could permanently unset the Manger change activity feed and allow the users to set on demand. any suggestions? Mar 22, 2012 at 7:28
  • Irrespective of the user, it updates the service account profile(on which logged into the server). what could be the issue? Mar 22, 2012 at 12:34
  • See edited response.
    – sssreddy
    Mar 22, 2012 at 23:24
  • I had tried the same code as above but it is not impersonating for any user .its still updating the service account only .Can you please help me with this
    – Anil
    Apr 11, 2012 at 16:23
  • You cannot impersonate activitymanager, although you could use reflection to set the target user infomation (see ps script in the ans)
    – sssreddy
    Apr 11, 2012 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.