SharePoint Use Confirmation for Site Owners
A problem in SharePoint is to keep content up to date. Therefore SharePoint offers the possibility to create use confirmations. The only problem is that the confirmation can only be approved by the site collection administrators. Not in all companies the site collection admins are also the site owners. This can be due to permission restrictions or other actions that a normal site owner should not do.
In this post I'll show you how you can create your own confirmation rules and pages without using visual studio or SharePoint solution files.
You can configure it in the Central Administration under "Application Management". There you'll find the link "Confirm site use and deletion".
Here you can configure after how many days a confirmation mail should be send to the sitecollection owner. Secondly you can also configure after how many times without response the sitecollection should be deleted.
As I am a SharePoint Admin in a company with sites that contain important information, I'm of course not a fan of workflows which automatically delete sites. I'm more interested in which sites are being used permanently and which not.
The script is relatively simple. It just loops through all site collections and checks their "CertificationDate" property. This property contains the last date the site has been confirmed.
Within a site collection it loops through the SharePoint groups to find the site owner group. Then max. 2 site random site owners are requested to confirm the site use by an email. Additionally the SharePoint administrator is informed if the confirmation time exceeds a period you predefined. With that you have the full control over the use process.
Here comes the script. Adapt the variables in the "Variables which can be changed" section. Change the URLs, SMTP Server and Files Paths. Also adapt the name of your owners group in line 76 if you have other group names for your owners.
Create a new folder in the task schedule library. Name it SharePoint.
Now create a new task.
Name it "Site Use Confirmation". In the "Action" tab choose :
In the "Triggers" tab create a new trigger and set the schedule you want. Save the task by clicking "OK"
This code fragment ensures that everyone can confirm the site use. Now adapt the link in the powershell script to your new confirmation page.
In this post I'll show you how you can create your own confirmation rules and pages without using visual studio or SharePoint solution files.
Standard Site Use Confirmation
SharePoint offers an out of the box site confirmation mechanism. It send an email to the site collection owner and updates a counter of how many times a mail is sent without receiving a confirmation. The mechanism does not check the real use of the site. It simply send an email after a period of time to check the use.You can configure it in the Central Administration under "Application Management". There you'll find the link "Confirm site use and deletion".
Here you can configure after how many days a confirmation mail should be send to the sitecollection owner. Secondly you can also configure after how many times without response the sitecollection should be deleted.
As I am a SharePoint Admin in a company with sites that contain important information, I'm of course not a fan of workflows which automatically delete sites. I'm more interested in which sites are being used permanently and which not.
PowerShell Script
For me as an administrator the easiest way to manipulate things in SharePoint is to create PowerShell Scripts. Because of that I created a simple script which sends confirmation mails to users.The script is relatively simple. It just loops through all site collections and checks their "CertificationDate" property. This property contains the last date the site has been confirmed.
Within a site collection it loops through the SharePoint groups to find the site owner group. Then max. 2 site random site owners are requested to confirm the site use by an email. Additionally the SharePoint administrator is informed if the confirmation time exceeds a period you predefined. With that you have the full control over the use process.
Here comes the script. Adapt the variables in the "Variables which can be changed" section. Change the URLs, SMTP Server and Files Paths. Also adapt the name of your owners group in line 76 if you have other group names for your owners.
#############################################
# Loading Microsoft.SharePoint.PowerShell #
#############################################
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
# >> Get today
$date = Get-Date
# >> Format of the date for the log file
$formatteddate = Get-Date -uformat "%Y_%m_%d_%H_%M"
#############################################
# Variables which can be changed #
#############################################
# >> Confirm mail send after 90 days of last confirmation
$confirmAfterDays = 90
# >> After this amount of days of tolerance the admin is informed
$toleranceDays = 30
# >> Name of the log file
$log = "E:\Appl\SP2010\Scripts\ConfirmUsage_$formatteddate.log"
# >> Name of the log file
$webapplication = "http://sharepoint"
# >> Email of sender
$emailFrom = "sharepoint@mycompany.ch"
# >> Email of admin
$emailAdmin = "sharepoint@mycompany.ch"
# >> Location of the useconfirmation page
$pathPage = "/_layouts/MyCompany.UseConfirmation/useconfirmation.aspx"
# >> SMTP Server
$smtp = new-object Net.Mail.SmtpClient("mail.mycompany.ch")
#############################################
# Start Process #
#############################################
# >> Logging!!
Start-Transcript -path $log
# >> Create the stopwatch
[System.Diagnostics.Stopwatch] $sw;
$sw = New-Object System.Diagnostics.StopWatch
$sw.Start()
Write-Host "Starting User Confirmation Check."
Write-Host "------------------------------------------"
Write-Host ""
try {
$webapp = Get-SPWebApplication $webapplication
$webapp.Sites | foreach {
Write-Host "Checking Site: " $_.Url
$diffDate = ($date - $_.CertificationDate).Days
if ($diffDate -gt $confirmAfterDays ) {
Write-Host " >> Site: " $_.Url " - Last use was for $diffDate days."
$users = @()
#############################################
# Get Site Owners from groups #
#############################################
foreach($group in $_.RootWeb.SiteGroups){
if ( $group.Name -like "* Owners") ) {
foreach( $user in $group.Users){
$users += $user.Email
}
}
}
#############################################
# Select Users #
#############################################
$countUser = $users.Length
$emailTo = ""
if ($countUser -gt 2){
# By more than 2 site owners select random 2
$i1 = Get-Random -minimum 0 -maximum $countUser
$i2 = Get-Random -minimum 0 -maximum $countUser
while ($i1 -eq $i2) { $i2 = Get-Random -minimum 0 -maximum $countUser }
$emailTo = $users[$i1] + ";" + $users[$i2]
}else {
$emailTo = $users -join ";"
}
#############################################
# Send E-Mail to Users #
#############################################
if ($emailTo -ne ""){
$subject = "Confirm SharePoint Web site in use"
$body = "Dear Site Owner, `n"
$body += "Please click on the link " + $_.Url + $pathPage +" to confirm that your site is still in use."
$body += @"
If the site is not being used, you can archive or delete it. Kontakt your SharePoint-Administrator for further process: sharepoint@mycompany.ch
You will receive reminders of this until you confirm the site is in use, or delete it.
Your Sharepoint Team
"@
Write-Host " >> Sending Email To: " $emailTo
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
#############################################
# Send E-Mail to Admin if site use is not #
# confirmed for x days #
#############################################
if ( ($diffDate - $confirmAfterDays) -gt $toleranceDays ) {
Write-Host " >> Sending Notice To: $emailAdmin that site use is not confirmed since $toleranceDays days."
$subject = "Site Use is not confirmed since $toleranceDays days."
$body = "The site use of '" + $_.Url + "' is not confirmed since $toleranceDays days. Please contact the site owner or archive the site"
$smtp.Send($emailFrom, $emailAdmin, $subject, $body)
}
}
}
}
catch [System.Exception] {
$_.Exception.ToString();
Write-Host "Error while sending confirmation usage mails."
}
finally {
$sw.Stop()
Write-Host "Time Elapsed: " $sw.Elapsed.ToString()
Stop-Transcript
}
Create Task Schedule
The next step is to create a task schedule which activates the script every 2 days. Therefore go to your Application Server and open the Task Scheduler. Be sure that you are logged as Farm Account on the server.Create a new folder in the task schedule library. Name it SharePoint.
Creat new folder in Task Scheduler |
Now create a new task.
Create a new Task Scheduler Task |
Name it "Site Use Confirmation". In the "Action" tab choose :
- Action: Start a program
- Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
- Add argument: -command "C:\Scripts\SiteUseConfirmation.ps1"
In the "Triggers" tab create a new trigger and set the schedule you want. Save the task by clicking "OK"
Confirmation Page
Finally we need the confirmation page. The standard confirmation page of SharePoint cannot be used because only a sitecollection admin can confirm the use. But we want that everbody can do that.
So I copied the standard confirmation page and modified it for my purposes. Create a new "MyCompany.UseConfirmation" folder within the /14/TEMPLATE/LAYOUTS/ folder. Copy the useconfirmation.aspx file into the new folder.
We have to make some modifications so the new confirmation page works.
1.) Replace the DynamicMasterPageFile and Inherits attributes by MasterPageFile="/_layouts/v4.master".
2.) Create a Page Load event. Add this piece of code above the first asp:Content element.
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using(SPSite site = new SPSite(SPContext.Current.Site.ID))
{
site.ConfirmUsage();
Label_UseConfirmation.Text = Label_UseConfirmation.Text.Replace("%1", site.RootWeb.Title);
}
});
}
</script>
This code fragment ensures that everyone can confirm the site use. Now adapt the link in the powershell script to your new confirmation page.
Deployment
You have to copy the confirmation page to all of your frontend servers into the /14/Templates/Layouts/MyCompany.UseConfirmation/ folder.
Comments