Add custom webpart to a page using power shell

by Soufiane.BenYOUSSEF 1. December 2011 18:34
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"

Tags: ,

PowerShell | SharePoint

Deactivate user & mysite strategies

by Soufiane.BenYOUSSEF 13. November 2011 00:10

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 powershell with elevated privileges

Run powershell with elevated privileges

by Soufiane.BenYOUSSEF 13. September 2011 00:06

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

Tags: ,

PowerShell

A propos de DCube

DCube est une société de services spécialisée dans les plateformes Microsoft.

Microsoft Gold Certified Partner depuis 2007, nous comptons une trentaine de collaborateurs certifiés ayant à cœur de mettre leur passion pour la technologie au service de leurs clients.

Visitez notre site : www.d-cube.fr