ARTICLE

How to create a new hold using PowerShell and programmatically in SharePoint 2010

Posted by Vijai Anand Articles | SharePoint 2010 March 15, 2011
In this article we will be seeing how to create a new hold in SharePoint 2010 using PowerShell and C#.
Reader Level:

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

Refer http://www.c-sharpcorner.com/UploadFile/anavijai/5103/  to create a hold and add or remove the hold to the item through UI.

In this article

  • Create a new hold using c#
  • Create a new hold using PowerShell

Create a new hold using c#:

  • Open Visual Studio 2010.
  • Create a new console application.
  • Add the following references.
     
    • Microssoft.Office.Policy.dll
    • Microsoft.SharePoint.dll
  • Add the following namespaces.
     
    • using Microsoft.SharePoint;
    • 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)
                {
                    //Create a new Hold in Holds list

                    SPList list = web.Lists["Holds"];
                    SPListItem item = list.Items.Add();
                    item["Title"] = "HR";
                    item["Description"] = "HR-Hold";
                    item["Managed By"] = web.CurrentUser;
                    item.Update();
                }
            }
        }
    }
}


HoldShare1.gif

Create a new hold using PowerShell:

$siteURL="http://serverName:1111/"
$site=Get-SPSite $siteURL
$web=$site.RootWeb
$list = $web.Lists["Holds"]
$item=$list.Items.Add()
$item["Title"]="Finance"
$item["Description"] = "Finance-Hold";
$item["Managed By"] = $web.CurrentUser;
$item.Update();
$web.Dispose()
$site.Dispose()

Login to add your contents and source code to this article
comments
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts