How to add to a hold using Powershell and Programmatically in SharePoint 2010


In this article we will be seeing how to add to a hold in SharePoint 2010 using PowerShell and C#.

In this article

  • Add to a hold using c#
  • Add to a hold using PowerShell

Add to a hold using C#

  • Open Visual Studio 2010.
  • Create a new console application.
  • Add the following references.

    1. Microsoft.Office.Policy.dill
    2. Microsoft.SharePoint.dill
     
  • Add the following namespaces.

    1. using Microsoft.Sharepont;
    2. using Microsoft.Office.RecordsManagement.Holds;
     
  • Replace the code with the following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.RecordsManagement.Holds;
namespace Holds
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://serverName:1111/"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    // Sets the item on the specified hold.
 
                    SPList list = web.Lists["Shared Documents"];
                    SPListItem item = list.Items[0];
                    SPList listHolds = web.Lists["Holds"];
                    SPListItem itemHold = listHolds.Items[1];
                    Hold.SetHold(item, itemHold, "Hold added"); 

                    // Sets the items on the specified hold.
                    SPListItemCollection items = list.Items;
                    Hold.SetHold(items, itemHold, "Hold added to all the items"); 
                }
            }
        }
    }
}


Add to a hold using PowerShell

Sets the item on the specified hold:

$siteURL="http://serverName:1111/"
$site=Get-SPSite $siteURL
$web=$site.RootWeb
$list = $web.Lists["Shared Documents"];
$item = $list.Items[0];
$listHOlds = $web.Lists["Holds"]
$itemHold=$listHOlds.Items[0];
[Microsoft.Office.RecordsManagement.Holds.Hold]::SetHold($item,$itemHold,"Hold added")
$web.Dispose()
$site.Dispose()

Sets the items on the specified hold:

$siteURL="http://serverName:1111/"
$site=Get-SPSite $siteURL
$web=$site.RootWeb
$list = $web.Lists["Shared Documents"];
$items = $list.Items;
$listHOlds = $web.Lists["Holds"]
$itemHold=$listHOlds.Items[0];
[Microsoft.Office.RecordsManagement.Holds.Hold]::SetHold($items,$itemHold,"Hold added to all the items")
$web.Dispose()
$site.Dispose()

Up Next
    Ebook Download
    View all
    Learn
    View all