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

Run powershell with elevated privileges

Sometimes,it depends on what you need to do, but you'll need to run power shell with elevated privileges.

You can run power shell with elevated privileges with this command line:
runas /noprofile /user:YOURUSER "C:\windows\system32\windowspowershell\v1.0\powershell.exe -NoExit & 'c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\SharePoint.ps1

Add custom webpart to a page using power shell

In one of my client, I had to import a webpart in the webpart gallery and add this webpart to the welcome page using Power Shell.
Here is the script I've made.Hope this will help.

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell" ) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Publishing")
[System.Reflection.Assembly]::LoadWithPartialName("System")

$site = new-object Microsoft.SharePoint.SPSite("http://mysite");
$web = $site.OpenWeb()
$fileDWP = $saveFolder + "myCustom.dwp"
$errorMsg = ""

[Microsoft.SharePoint.SPList]$wpList = $site.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::WebPartCatalog)
$fileStream = ([System.IO.FileInfo](Get-Item $fileDWP)).OpenRead()

[Microsoft.SharePoint.SPFolder]$wpFolder = $wpList.RootFolder
[Microsoft.SharePoint.SPFile]$wpFile = $wpFolder.Files.Add("myCustom.webpart", $fileStream, $true)

$wpFile.Update();

[System.Xml.XmlReader]$xmlReader = [System.Xml.XmlReader]::Create($wpFile.OpenBinaryStream())
[Microsoft.SharePoint.Publishing.PublishingWeb]$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web);
[Microsoft.SharePoint.SPFile]$defaultPage = $pubWeb.DefaultPage;
[Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager]$wpManager = $defaultPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$myCustomWP = $wpManager.ImportWebPart($xmlReader,[ref]$errorMsg)
$wpManager.AddWebPart($infoWp, "Right", 1);
$fileSream.Close()
$xmlReader.Close()
$pubWeb.Close()
$web.Dispose()
$site.Dispose()

write-host "Done"

mercredi 16 février 2011

Debug Sharepoint 2010

Si vous n'arrivez pas à débugger avec sharepoint 2010.Voici une petite astuce qui pourras surement vous aider.Pour ce faire,aller dans la base de registre et trouver cette valeur

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0\SharePointTools], changer la valeur DWORD de EnableDiagnostics en la mettant à 1. Si le DWORD n'existe pas, il faut le créer en choisissant"Nouveau DWORD"

vendredi 7 janvier 2011

Dcube présent aux Techdays de février 2011

Dcube sera présent aux Techdays de février 2011 pour présenter le cloud computing grâce à un scénario de développement en 3 écrans (windows phone 7, azure et silverlight).

Pour plus d'information : Techdays février 2011