top of page
Writer's pictureVijai Anand Ramalingam

How to restrict deletions on Lookup field in SharePoint 2010

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#:

a. Open Visual Studio 2010.

b. Create Console Application.

c. 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. namespace RestrictDeletions

  7. {

  8. class Program   

  9. {

  10. static void Main(string[] args)       

  11. {

  12. using (SPSite site = new SPSite("http://serverName:1111/SitePages/Home.aspx"))           

  13. {

  14. using (SPWeb web = site.RootWeb)               

  15. {

  16. SPList list = web.Lists.TryGetList("ChildList");

  17. SPField field=list.Fields["Department"];

  18. SPFieldLookup fieldLookup = (SPFieldLookup)field;                   

  19. fieldLookup.Indexed = true;                   

  20. fieldLookup.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Cascade;                   

  21. fieldLookup.Update();               

  22. }           

  23. }       

  24. }   

  25. }

  26. }


d. Hit F5.


Using PowerShell:

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

  2. $site=Get-SPSite $siteURL

  3. $web=$site.Rootweb

  4. $list=$web.Lists.TryGetList("ChildList")

  5. $field=$list.Fields["Department"]

  6. $fieldLookup=[Microsoft.SharePoint.SPFieldLookup]$field

  7. $fieldLookup.Indexed=$true

  8. $fieldLookup.RelationshipDeleteBehavior [Microsoft.SharePoint.SPRelationshipDeleteBehavior]::Cascade

  9. $fieldLookup.Update()

0 comments

Comments


bottom of page