Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Tuesday, 20 March 2012

Notify Users Who Leave Documents Checked Out

We have a problem with some users, who check out documents but never check them back in. I wrote a Powershell script to loop through the libraries and look for files that had been checked out for longer than 1 day. 

All of our libraries have a view called "Checked Out" documents which shows all the checked out documents in the library. The script creates a link to this view but you can easily change that to create a link to the library or directly to the file.

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
    Write-Host "Loading SharePoint Powershell Snap-in"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
}

$site = Get-SPSite http://siteurl
$SmtpServer = "smtp.yourserver.com"
$Subject = "File Checked Out Longer Than 1 Day"
$from = "service.desk@yourcompany.com"
$date = Get-Date
$Warning = ((get-date).adddays(-1))

#Loop through the webs
foreach ($web in $site.AllWebs)
{
  # loop through all lists in web
  foreach ($list in $web.Lists)
  {
    # examine if BaseType of list is NOT a Document Library
    if ($list.BaseType -ne "DocumentLibrary")
    {
      # forget the rest and return to top
      continue
    }
    # loop through each item
    foreach ($item in $list.Items)
    {
      $file = $item.File   
      if(($file.CheckOutStatus -ne "None") -and ($file.CheckedOutDate -lt $warning))
      {
        $User = $file.CheckedOutByUser
        $Email = $file.CheckedOutByUser.email
        $body = "Hi " +$User + ",

You have had the following file checked out from Sharepoint for more than a day. Click on the link below to see checked out documents in this library.

" +$file.name + "     """ + $web.url + "/" + $list + "/Forms/Checked%20Out%20Documents.aspx" + """

Please check this document back in If you are no longer using it.

Thanks"
        $smtp = new-object Net.Mail.SmtpClient($SmtpServer)
        $smtp.Send($from, $Email, $subject, $body)          
      }
    }
  }
}

Tuesday, 3 January 2012

Retention Policy for Sharepoint Foundation 2010

There are no Retention Policies available for Sharepoint Foundation 2010 but you can get around this if you are a little bit creative. I have created a Powershell script that will delete files from a library that have not been modified within a specified time.
In our case we wanted to setup a temporary library for files that would be deleted if they had not been modified in the past three months.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$SPWeb = Get-SPWeb http://yourSiteURL/
$list=$SPWeb.Lists["Documents Library Name"]
$listItems = $list.Items
$listItemsTotal = $listItems.Count

$ThreeMonthsAgo = ((get-date).AddMonths(-3))

for ($x=$listItemsTotal-1;$x -ge 0; $x--)
{
    if([System.DateTime]$listItems[$x]["Modified"] -lt $ThreeMonthsAgo)
    {
        $listItems[$x].Delete()
    }
}
 
This script will delete the files but you could send them to the Recyle Bin instead using

$listItems[$x].Recycle()
 
You can change the expiry date using there properties to best fit your requirements.

 AddDays
 AddHours
 AddMilliseconds
 AddMinutes
 AddMonths
 AddSeconds
 AddTicks
 AddYears
 
After that I simply setup a Scheduled Task to run the Powershell script daily and I now have a Retention Policy on our temporary library.



Thursday, 15 December 2011

Open PDF in Browser Not Working

In Sharepoint 2010 you can change the Web Application settings so PDFs open in the browser. You can do this in Central Admin, Manage Web Applications. Select the Application and then click on General Settings. In the Browser File Handling section you can change the settings to be Permissive.
























Once this change is made it should work but you may still have a problem with sites that have been created from a template. You can run this script to update these sites to Permissive which will then fix all of the remaining sites.

$site = Get-SPSite(“http://siteurl")
foreach ($web in $site.AllWebs) {
    Write-Host "Inspecting " $web.Title
    foreach ($list in $web.Lists) {
        if($list.browserfilehandling -eq "Strict") {
            Write-Host "Changing " $list.Title
            $list.browserfilehandling = "Permissive";
            $list.update();
            $site.url,
            $list.title,
            $list.browserfilehandling
        }
    }
}