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

vendredi 24 décembre 2010

Parcours site web en anonyme

Lorsque que vous souhaiter parcourir les web du site courrant en mode anonyme vous ne pourrez pas utiliser de foreach sur la propriété AllWebs de l'objet SPSite. En effet, lorsque vous utiliserez cette propriété en mode anonyme vous aurez une popup d'authentification qui s'ouvrira. Pour éviter ce problème il faudrait plutôt utilisé la méthode:

SPContext.Current.Web.GetSubwebsForCurrentUser();

Utilisation template de mail sous WSS

L'envoi d'un mail par code peut-être une chose fastidieuse quand vous construisez le code html de votre mail dans une de vos fonctions. Le problème de cette méthode c'est que votre code devient vite illisible et si vous(ou un collaborateur utilisant WSS/MOSS) voulez changez le format du mail, vous serez obligez de repasser dans votre code.

Donc pour éviter de vous perdre rapidement dans votre fonction qui écrit votre mail et évitez de repasser dans votre code, utilisez plutôt un template de mail au format html que vous uploaderez dans une doclib (au top level site par exemple).











Du coté code, vous n'avez qu'a ouvrir votre template de mail de la manière suivante:
SPFolder emailTemplatesFolder = rootWeb.GetFolder(rootWeb.Url + "/" + "EmailTemplates");
SPFileCollection fileCollection = emailTemplatesFolder.Files;
SPFile senderMailBodySPFile = fileCollection["monTemplateDeMail.html"];
byte[] senderMailBodyBinary = senderMailBodySPFile.OpenBinary();
string senderMailBody = Encoding.UTF8.GetString(senderMailBodyBinary);



Un petit truc bien sympa aussi, vous pouvez mettre des variables dans votre mail que vous remplacerez dans votre méthode.Pour ma part, j'ai rajoutez des variables dans mon mail qui sont de la forme @@nomVariable@@
Et pour finir, il vous suffit de remplacer vos variables de la manière suivante:

string userName = "dcubesolutions";
senderMailBody = senderMailBody.Replace("@@userName@@", userName );