Click here to Skip to main content
Click here to Skip to main content

List and AllowDelete Property

, 20 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to protect a list by disabling the Delete list option using Visual Studio and PowerShell.

Introduction

In this article we can explore how to protect a list from getting deleted by the user. In real world scenarios there are chances that the user can use the List Settings > Delete list option. 

We have to make sure that the above option is hidden for the user through the SharePoint user interface.

There are multiple ways to achieve this:

  • Use  Visual Studio to set the list property AllowDeletion to false.
  • Use PowerShell to set the list property AllowDeletion to false.
  • Create a new Permission with Delete Restriction on List and assign to the user.

Here we are using the Visual Studio and PowerShell approach to disable the Delete option.

Visual Studio Approach

Create a new list in your SharePoint site and name the list New List. Ensure that through the List Settings page you can see the Delete this list link.

Now create a new console application inside Visual Studio and name it AllowDeletionFalse.

Inside the code modify the Main method as shown below:

static void Main(string[] args)
{
    SPSite site = new SPSite("http://localhost");
    SPWeb web = site.OpenWeb();

    SPList list = web.Lists["New List"];
    list.AllowDeletion = false;

    list.Update();
}

The code above connects to the URL specified, gets the instance of the list named New List and modifies the property AllowDeletion to false.

Build and execute the application. If it is executed without errors the list now has the Delete link hidden. You can verify this by going back to the List Settings page inside SharePoint.

From the Permissions and Management group you can see that the Delete this list link is missing. This makes our list protected from unwanted delete activity by a user.

PowerShell Approach

Now we can try to reverse the AllowDeletion property using PowerShell script.  Please note that for these type of scenarios on client locations, PowerShell would be more handy. (Visual Studio might not be installed in the customer premises.)

You can start the PowerShell Editor aka ISE (Integrated Script Editor) from Start > Programs > Accessories > Windows Power Shell menu group.

The editor is shown below. Save the default file as AllowDeletionFalse.ps1. (ps1 is the extension for PowerShell scripts).

Following is shown above in the .ps1 file.

If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 
{ 
   Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell 
}

$web = Get-SPWeb -Identity "http://localhost" -AssignmentCollection $assignment
$list = $web.lists["New List"]
$list.AllowDeletion = $true
$list.Update()

Code Explained

The first If block makes sure that the Microsoft.SharePoint.PowerShell snap-in is loaded into memory. (It is similar to Add Reference in Visual Studio.)

The Get-SPWeb cmdlet retrieves the reference to the specified web application. web.lists returns the reference to our New List. After setting AllowDeletion to true the list is updated. You can use the Run command of the PowerShell editor to execute the code.

Once the code executes successfully our New List should have the Delete this list link visible in the List Settings. You can refresh the List Settings page to ensure that it is restored.

References

Summary

In this article we have seen how to protect a list by disabling the Delete list option using Visual Studio and PowerShell. The source code is contained in the attachment.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jean Paul V.A
Architect
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.
 
In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.
 
Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.
 
You can find some of his work over here. He blogs at http://jeanpaulva.com
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 20 Aug 2012
Article Copyright 2012 by Jean Paul V.A
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid