SIGN UP MEMBER LOGIN:    
ARTICLE

How to restrict deletions on Lookup field in SharePoint 2010

Posted by Vijai Anand Articles | SharePoint April 01, 2011
In this article we will be seeing how to restrict deletions on Lookup field in SharePoint 2010 using C# and power shell.
Reader Level:

In this article we will be seeing how to restrict deletions on Lookup field in SharePoint 2010 using C# and power shell.

When you create a lookup column in SharePoint 2010 you can see "Relationship" where you can maintain referential integrity enforcing the relationships defined by lookup columns. Just like foreign key constraints in a relational database, in SharePoint 2010 you can configure restrict delete and cascade delete rules on lookup column relationships:

  • Cascade delete rules: Delete items that reference a record when you delete that record.
  • Restrict delete rules: Will prevent you from deleting a record that is referenced by a lookup column in another list.

Using C#:

  • Open Visual Studio 2010.
  • Create Console Application.
  • Replace the code with the following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace RestrictDeletions
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://serverName:1111/SitePages/Home.aspx"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList list = web.Lists.TryGetList("ChildList");
                    SPField field=list.Fields["Department"];
                    SPFieldLookup fieldLookup = (SPFieldLookup)field;
                    fieldLookup.Indexed = true;
                    fieldLookup.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Cascade;
                    fieldLookup.Update();
                }
            }
        }
    }
}

  • Hit F5.

Using PowerShell:

$siteURL="http://serverName:1111/"
$site=Get-SPSite $siteURL
$web=$site.Rootweb
$list=$web.Lists.TryGetList("ChildList")
$field=$list.Fields["Department"]
$fieldLookup=[Microsoft.SharePoint.SPFieldLookup]$field
$fieldLookup.Indexed=$true
$fieldLookup.RelationshipDeleteBehavior=[Microsoft.SharePoint.SPRelationshipDeleteBehavior]::Cascade
$fieldLookup.Update()
 

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor