mardi 13 septembre 2011

Deactivate user & mysite strategies

User strategies are uses by service profile for user profil to activate or deactivate some functionnality for users and MySites.

Here is 3 ways to proceed:

Central Admin





C#

using (SPSite site = new SPSite("http://myAdminUrl"))
{
     using (SPWeb web = site.OpenWeb())
     {
        SPServiceContext serviceContext = SPServiceContext.GetContext(site);
        UserProfileManager usrProfil = new UserProfileManager(serviceContext);
        PrivacyPolicyManager prvPolicyMgr = usrProfil.GetPrivacyPolicy();
        PrivacyPolicyItem[] pPolicyItems = prvPolicyMgr.GetAllItems();

        foreach (PrivacyPolicyItem policyItem in pPolicyItems)
        {
          if (policyItem.Group.Equals("appartenances", StringComparison.InvariantCultureIgnoreCase))
          {
              //Check if the Privacy policy is deactivated
             if (policyItem.PrivacyPolicy == PrivacyPolicy.Disabled)
               continue;

             policyItem.PrivacyPolicy = PrivacyPolicy.Disabled;
             policyItem.Commit();
          }
       }
    }
}

PowerShell
if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
   Add-PSSnapin Microsoft.SharePoint.PowerShell
}
 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
 [System.Reflection.Assembly]::LoadWithPartialName("System")

$site = new-object Microsoft.SharePoint.SPSite("http://mySite")
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site)
$web = $site.OpenWeb()

$upm = [Microsoft.Office.Server.UserProfiles.UserProfileManager](Microsoft.Office.Server.ServerContext]::Default)

$prvPolicyMgr = $upm.GetPrivacyPolicy()
$pPolicyItems =  @($prvPolicyMgr.GetAllItems())

foreach($policyItem in $pPolicyItems)
{
  if($policyItem.Group.Equals("appartenances",[System.StringComparison]::InvariantCultureIgnoreCase -and $policyItem.Privacy -neq [Microsoft.Office.Server.UserProfiles.PrivacyPolicy]::Disabled)
  {
    $policyItem.PrivacyPolicy = [Microsoft.Office.Server.UserProfiles.PrivacyPolicy]::Disabled
    $policyItem.PrivacyPolicy.Commit()
  }
}

$web.Dispose()
$site.Dispose()
For this script, you'll need to run powershell with sufficient privileges=> run power shell with elevated privileges

Aucun commentaire:

Enregistrer un commentaire