Thursday, May 16, 2013

Add Users to SharePoint Group using PowerShell

$USER_TO_ADD = $null;
 $web = Get-SPWeb -Identity "http://yoursite.com"
$manageGroup = $web.SiteGroups["GroupName"]
 try {
 $adUsername = "$USER_TO_ADD";
 $spUser = $web.Site.RootWeb.EnsureUser($adUsername.Split('\',1));    $manageGroup.AddUser($spUser);
 $manageGroup.Update();
 write-host "User: "
 $spUser.Name "was added to group" }
 catch
 {
    $_ Write-Host $spUser " does not exists"
 }
 } $web.Update(); $web.Dispose();

Thursday, May 9, 2013

Sharepoint 2010 disable event firing using Powershell script



Here is a code:


#Add SharePoint PowerShell SnapIn if not already added
 if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$web = Get-SPWeb "http://mysharepoint.com/mypage/"


$list = $web.Lists.TryGetList("<ListName>")

$item = $list.Items.GetItemById("<ItemID>")

   try
   {

Write-Host "Deleting" $item.Title;
$myAss = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
    $type = $myAss.GetType("Microsoft.SharePoint.SPEventManager");
    $prop = $type.GetProperty([string]"EventFiringDisabled",[System.Reflection.BindingFlags] ([System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Static));

$prop.SetValue($null, $true, $null);
$prop.GetValue($null,$null)
$item.Title;
     $item.Delete()
    Write-Host  $item.Title "was deleted";

$prop.SetValue($null, $false, $null);
$prop.GetValue($null,$null);
$prop.GetValue(
}
catch
{
     
}