Monday, July 30, 2012

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.

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.

  1. #############################################
  2. # Loading Microsoft.SharePoint.PowerShell #
  3. #############################################
  4. $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
  5. if ($snapin -eq $null) {
  6. Add-PSSnapin "Microsoft.SharePoint.Powershell"
  7. }
  8. # >> Get today
  9. $date = Get-Date
  10. # >> Format of the date for the log file
  11. $formatteddate = Get-Date -uformat "%Y_%m_%d_%H_%M"
  12. #############################################
  13. # Variables which can be changed #
  14. #############################################
  15. # >> Confirm mail send after 90 days of last confirmation
  16. $confirmAfterDays = 90
  17. # >> After this amount of days of tolerance the admin is informed
  18. $toleranceDays = 30
  19. # >> Name of the log file
  20. $log = "E:\Appl\SP2010\Scripts\ConfirmUsage_$formatteddate.log"
  21. # >> Name of the log file
  22. $webapplication = "http://sharepoint"
  23. # >> Email of sender
  24. $emailFrom = "sharepoint@mycompany.ch"
  25. # >> Email of admin
  26. $emailAdmin = "sharepoint@mycompany.ch"
  27. # >> Location of the useconfirmation page
  28. $pathPage = "/_layouts/MyCompany.UseConfirmation/useconfirmation.aspx"
  29. # >> SMTP Server
  30. $smtp = new-object Net.Mail.SmtpClient("mail.mycompany.ch")
  31. #############################################
  32. # Start Process #
  33. #############################################
  34. # >> Logging!!
  35. Start-Transcript -path $log
  36. # >> Create the stopwatch
  37. [System.Diagnostics.Stopwatch] $sw;
  38. $sw = New-Object System.Diagnostics.StopWatch
  39. $sw.Start()
  40. Write-Host "Starting User Confirmation Check."
  41. Write-Host "------------------------------------------"
  42. Write-Host ""
  43. try {
  44. $webapp = Get-SPWebApplication $webapplication
  45. $webapp.Sites | foreach {
  46. Write-Host "Checking Site: " $_.Url
  47. $diffDate = ($date - $_.CertificationDate).Days
  48. if ($diffDate -gt $confirmAfterDays ) {
  49. Write-Host " >> Site: " $_.Url " - Last use was for $diffDate days."
  50. $users = @()
  51. #############################################
  52. # Get Site Owners from groups #
  53. #############################################
  54. foreach($group in $_.RootWeb.SiteGroups){
  55. if ( $group.Name -like "* Owners") ) {
  56. foreach( $user in $group.Users){
  57. $users += $user.Email
  58. }
  59. }
  60. }
  61. #############################################
  62. # Select Users #
  63. #############################################
  64. $countUser = $users.Length
  65. $emailTo = ""
  66. if ($countUser -gt 2){
  67. # By more than 2 site owners select random 2
  68. $i1 = Get-Random -minimum 0 -maximum $countUser
  69. $i2 = Get-Random -minimum 0 -maximum $countUser
  70. while ($i1 -eq $i2) { $i2 = Get-Random -minimum 0 -maximum $countUser }
  71. $emailTo = $users[$i1] + ";" + $users[$i2]
  72. }else {
  73. $emailTo = $users -join ";"
  74. }
  75. #############################################
  76. # Send E-Mail to Users #
  77. #############################################
  78. if ($emailTo -ne ""){
  79. $subject = "Confirm SharePoint Web site in use"
  80. $body = "Dear Site Owner, `n"
  81. $body += "Please click on the link " + $_.Url + $pathPage +" to confirm that your site is still in use."
  82. $body += @"
  83. If the site is not being used, you can archive or delete it. Kontakt your SharePoint-Administrator for further process: sharepoint@mycompany.ch
  84. You will receive reminders of this until you confirm the site is in use, or delete it.
  85. Your Sharepoint Team
  86. "@
  87. Write-Host " >> Sending Email To: " $emailTo
  88. $smtp.Send($emailFrom, $emailTo, $subject, $body)
  89. }
  90. #############################################
  91. # Send E-Mail to Admin if site use is not #
  92. # confirmed for x days #
  93. #############################################
  94. if ( ($diffDate - $confirmAfterDays) -gt $toleranceDays ) {
  95. Write-Host " >> Sending Notice To: $emailAdmin that site use is not confirmed since $toleranceDays days."
  96. $subject = "Site Use is not confirmed since $toleranceDays days."
  97. $body = "The site use of '" + $_.Url + "' is not confirmed since $toleranceDays days. Please contact the site owner or archive the site"
  98. $smtp.Send($emailFrom, $emailAdmin, $subject, $body)
  99. }
  100. }
  101. }
  102. }
  103. catch [System.Exception] {
  104. $_.Exception.ToString();
  105. Write-Host "Error while sending confirmation usage mails."
  106. }
  107. finally {
  108. $sw.Stop()
  109. Write-Host "Time Elapsed: " $sw.Elapsed.ToString()
  110. Stop-Transcript
  111. }


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.

  1. <script runat="server">
  2. void Page_Load(object sender, System.EventArgs e)
  3. {
  4. SPSecurity.RunWithElevatedPrivileges(delegate()
  5. {
  6. using(SPSite site = new SPSite(SPContext.Current.Site.ID))
  7. {
  8. site.ConfirmUsage();
  9. Label_UseConfirmation.Text = Label_UseConfirmation.Text.Replace("%1", site.RootWeb.Title);
  10. }
  11. });
  12. }
  13. </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.

No comments: