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.

i) Microsoft.Office.Policy.dill ii) Microsoft.SharePoint.dill

  • Add the following namespaces

i) using Microsoft.Sharepont; ii) using Microsoft.Office.RecordsManagement.Holds;

  • Replace the code with the following.

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using Microsoft.Office.RecordsManagement.Holds;

  7. namespace Holds

  8. {

  9. class Program   

  10. {

  11. static void Main(string[] args)       

  12. {

  13. using (SPSite site = new SPSite("http://serverName:1111/"))           

  14. {

  15. using (SPWeb web = site.RootWeb)               

  16. {

  17. // Sets the item on the specified hold.

  18. SPList list = web.Lists["Shared Documents"];                   

  19. SPListItem item = list.Items[0];                   

  20. SPList listHolds = web.Lists["Holds"];                   

  21. SPListItem itemHold = listHolds.Items[1];                   

  22. Hold.SetHold(item, itemHold, "Hold added"); 

  23. // Sets the items on the specified hold.                   

  24. SPListItemCollection items = list.Items;                   

  25. Hold.SetHold(items, itemHold, "Hold added to all the items");                

  26. }           

  27. }       

  28. }   

  29. }

  30. }

Add to a hold using PowerShell

Sets the item on the specified hold:

  1. $siteURL="http://serverName:1111/"

  2. $site=Get-SPSite $siteURL

  3. $web=$site.RootWeb

  4. $list = $web.Lists["Shared Documents"];

  5. $item = $list.Items[0];

  6. $listHOlds = $web.Lists["Holds"]

  7. $itemHold=$listHOlds.Items[0];

  8. [Microsoft.Office.RecordsManagement.Holds.Hold]::SetHold($item,$itemHold,"Hold added")

  9. $web.Dispose()

  10. $site.Dispose()


Sets the items on the specified hold:

  1. $siteURL="http://serverName:1111/"

  2. $site=Get-SPSite $siteURL

  3. $web=$site.RootWeb

  4. $list = $web.Lists["Shared Documents"];

  5. $items = $list.Items;

  6. $listHOlds = $web.Lists["Holds"]

  7. $itemHold=$listHOlds.Items[0]

  8. [Microsoft.Office.RecordsManagement.Holds.Hold]::SetHold($items,$itemHold,"Hold added to all the items")

  9. $web.Dispose()

  10. $site.Dispose()