Monday, March 21, 2016

Powershell to remove all alerts for a user in a Web Application

This script will delete all alerts for a specific user on a Web App.  It's useful when a user is deleted, but the MySite cleanup job did not remove their collection level alerts.

Credit on this one to:  http://www.sharepointdiary.com/2011/11/managing-alerts-using-powershell.html - Salaudeen Rajack. 

##### Remove all alerts for specific user from a Web Application #####
$SPwebApp = Get-SPWebApplication "http://somesharepointsite.com"
$SpecificUser = "AD\someuser"
foreach ($SPsite in $SPwebApp.Sites)
{ # get the collection of webs
foreach($SPweb in $SPsite.AllWebs)
{ $alerts = $SPweb.Alerts
# if 1 or more alerts for a particular user, Make a note of them by copying their ID to an Array
if ($alerts.Count -gt 0)
{ $myalerts = @()
foreach ($alert in $alerts)
{ if ($alert.User -like $SpecificUser)
{ $myalerts += $alert
}
}
### now we have alerts for this site, we can delete them
foreach ($alertdel in $myalerts)
{ $alerts.Delete($alertdel.ID)
write-host $alertdel.ID
}
}
}
}

Friday, March 18, 2016

Powershell to approve all items in a SharePoint list


This script will set all items in the list to Approved.  Handy if you've bulk updated a bunch of items than need to get them published.

add-pssnapin microsoft.sharepoint.powershell


#$site = new-object Microsoft.SharePoint.SPSite("http://somesite.com/news")

$web = get-spweb "http://somesite.com/news/2016/"
$list = $web.Lists["Pages to be Changed"]
$items = $list.Items
foreach ($item in $items)

{

$item["_ModerationStatus"] = 0
$item.Update()

}

Remove WWW prefix from SharePoint URLs

It's not every day you are tasked with doing something, with Microsoft in the room, and the Microsoft rep goes "Wow, we should totally put that on TechNet."

This is one of those things.

If you want to remove the www prefix from a url for all SharePoint sites, here's how:

1.  Download and install the URL Rewrite Module on all web front ends.  Get it here:  http://www.iis.net/downloads/microsoft/url-rewrite

2.  Configure Alternate Access Mappings in SharePoint to allow the www url.  We added an Internal URL for our WWW entry, and mapped it to the public url.  Our AAM settings looked like this when we were done:



3.  Add a binding in IIS for the additional WWW url.  Our bindings looked like this:



4.  Configure the URL Rewrite Module for the new address.  Do that by:
- Adding a Blank Rule
- Enter the pattern http://www.somesite.com/*
- Choose Rewrite for the Action Type
- Enter the pattern http://somesite.com/*
- Save it.

Why this works:

SharePoint needs an Alternate Access Mapping for the www url.  Once that's in place, IIS needs a binding to recognize the request from SharePoint.  Once THAT is entered, the rewrite module will see the www request come in, pick it up, and reroute it. 

The users will see the site automatically replace the URL to http://somesite.com.

Wednesday, March 16, 2016

Delete all users from a SharePoint group with Powershell

Add-PSSnapin Microsoft.SharePoint.Powershell

###########################
# "Enter the site URL here"
$SITEURL = "http://somesite.com"

# "Name of Site group from which users have to be removed"
$SITEGROUP = "Members"

###########################

$site = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$web = $site.OpenWeb()
"Web is : " + $web.Title

$oSiteGroup = $web.SiteGroups[$SITEGROUP];

"Site Group is :" + $oSiteGroup.Name
$oUsers = $oSiteGroup.Users

foreach ($oUser in $oUsers)
{
    "Removing user : " + $oUser.Name
    $oSiteGroup.RemoveUser($oUser)
}