Monthly Archives: January 2013

Empty SharePoint Recycle Bin

In this post, I would like to show you how to empty your SharePoint Recycle Bin in one go. This is apply to site recycle bin and site collection recycle bin.
1. Go to your SharePoint site Recycle Bin, like: http://sitename/_layouts/RecycleBin.aspx
2. In your internet browser URL address bar, replace above URL to: javascript:emptyItems();

Empty Recycle Bin

Empty Recycle Bin

Enjoy … this is very convenient if you have thousands of items in your SharePoint Recycle Bin.

Advertisements

Empty Recycle Bin in All SharePoint Sites

Continuing from my previous post to reduce SharePoint Content Database for test environment by deleting version histories for all libraries in SharePoint Site Collection.

We could also reducing SharePoint Content Database by Emptying all Recycle Bin. Below is the powershell script to do it

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
# get site
$site = new-object Microsoft.SharePoint.SPSite(<a href="http://sitename/">http://sitename</a>)
# loop through webs
foreach ($web in $site.AllWebs)
{
   $web.RecycleBin.MoveAllToSecondStage();
   write-host $web.title ":" $web.URL
}
$site.RecycleBin.DeleteAll();
write-host $site.RootWeb.title ":" $site.RootWeb.URL

$web.Dispose();
$site.Dispose();

NOTE: DO NOT forget to set your PowerShell execution policy to Remotely Signed

Set-ExecutionPolicy RemoteSigned

Delete Version History in SharePoint Document Library

In this post, I would like to show how to delete version history in SharePoint Document Library. For my scenario, I need to do it to reduce the content database size and who cares about file version history 5 years ago.

There are few ways to achieve it. One of the manual way and involving to action for each document library is:

  1. to set the maximum version in document library settings, let say to 2.
  2. Next document you’ve uploaded will delete all version history of all existing files except the latest 2 versions.

The other way you could do is through PowerShell script. Below is the script to delete all version history for all items in Document Library.

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
# get site
$site = new-object Microsoft.SharePoint.SPSite("http://sitename")
# loop through webs
foreach ($web in $site.AllWebs)
{
   write-host $web.url
   # loop through all lists in web
   foreach ($list in $web.Lists)
   {
      # examine if BaseType of list is NOT a Document Library
      if (($list.BaseType -eq "DocumentLibrary") -and ($list.EnableVersioning))
      {
         # Keep latest 2 versions
         $list.MajorVersionLimit = 2
         $list.Update()
         foreach($item in $list.Items)
         {
            $item.SystemUpdate($false)
         }
      }
   }
}
$web.Dispose();
$site.Dispose();

If you like to delete all version history you could replace line 14-20 above to:

         # Delete all version history
         foreach ($item in $list.Items)
         {
            # work with the file object as we're in a document library
            $file = $item.File
            # delete all versions
            $file.Versions.DeleteAll()
         }

NOTE: DO NOT forget to set your PowerShell execution policy to Remotely Signed

Set-ExecutionPolicy RemoteSigned

This could also be done in .NET managed code as well found here.

Another thing that you could do to reduce SharePoint Content Database is by emptying all recycle bin in site collection.

Add, Update and Delete item in SharePoint List

I remembered add, update and delete item in SharePoint list is one of the first thing that I’ve learned in SharePoint. I know its quite common task that you have to deal within SharePoint and hence I reiterate it in this article.

The code below will Add new item into SharePoint list, Update the description of item added and Delete the item at the end.

using (SPSite site = new SPSite("http://sitename"))
// Or SPContext.Current.Site.Url
{
   using (SPWeb Web = site.OpenWeb())
   {
      Web.AllowUnsafeUpdates = true;

      // Open List
      SPList list = Web.Lists["MyList"];

      // Add new item in List
      SPListItem item = list.Items.Add();
      item["Title"] = “Test Title”;
      item["Description"] = “Test Description”;
      item.Update();

      // Get Item ID
      int listItemId = item.ID;

      // Update the List item by ID
      SPListItem itemUpdated = list.GetItemById(listItemId);
      itemUpdated["Description"] = “Update Description”;
      itemUpdated.Update();

      // Delete item
      SPListItem itemDeleted = list.GetItemById(listItemId);
      itemDeleted.Delete();

      Web.AllowUnsafeUpdates = false;
   }
}

Export All Custom Solutions from SharePoint Farm

When I’m in process of cloning of production environment to test environment. I’ve come across nice trick to export all wsp files from your SharePoint farm.

It is done by PowerShell script and I will list all steps required below:

  • Run Windows PowerShell
  • Load SharePoint snap in to PowerShell: [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
  • Get the actual Farm into variable: $cf = [Microsoft.SharePoint.Administration.SPFarm]::Local
  • Check whether SharePoint Farm variable loaded: $cf

Image

  • Get all solutions of the farm into a farm variable: $sols = $cf.Solutions
  • Check solutions variable: $sols
  • Export solutions variable into temp folder: $sols | ForEach-Object {Write-Host $_.name -foregroundcolor yellow; $_.SolutionFile.SaveAs(“c:\temp\wsp\” + $_.name)}

Image

  • Now, all of your custom solutions will be in C:\temp\wsp folder
%d bloggers like this: