Deleting Data Using LINQ Share Point 2010

Although we couldn’t use LINQ to add document items to a document library, we can use LINQ to delete such items. Delete works because LINQ provides a DeleteOnSubmit method that makes use of the unique identifier for the entity object to delete the corresponding object from the data store.

Deleting Sample Data
Add a new button to the sample application and name it Delete Sample Data.

Deleting Sample Data

It should be apparent from the comments that this code will delete all the sample data that we created in the preceding code examples. It uses three nested loops to ensure that referential integrity is maintained, and all associated child data is deleted before the parent element is deleted.

Ensuring Referential Integrity

Since we defined relationships for which participation was mandatory—that is, each child object must have a parent object—you may wonder why the child objects were not automatically deleted when the parent was removed, because removing the parent object while leaving the child would clearly leave the data set in an invalid state. In relational database terms, this is a fundamental requirement to ensure referential integrity. In SharePoint, applying referential integrity constraints on a lookup field is optional. Where no constraints are applied, it is possible to delete parent objects without cascading deletes to child objects or even raising an error to indicate that such a delete would invalidate the data structure. At the time of writing, when using SharePoint Designer2010 Beta 2 to create lookup columns, there is no option to configure referential integrity options. Of course, this issue may be resolved by the time this book goes to print.

Programmatically Configure Referential Integrity Options
For the moment, we can set referential integrity options using only the SharePoint user interface or programmatically. Let’s consider theprogrammatic option first. Add another button to the sample application and name it Enforce Integrity Checks. In the on-click event handler, add the following code:

This code updates the lookup column properties to enforce referential integrity by throwing an exception if an attempt is made to delete a parent object while child objects still exist. The other option for ensuring referential integrity is to cascade deletes automatically. In that case, when a parent object is deleted, any child objects are also automatically deleted. To see the effects of this change, review the code for the Delete Sample Data button. Either comment out or delete the middle loop so that the code reads as follows:

Run the sample application, re-creating the sample data first if you’ve deleted it. This time, before clicking the Delete Sample Data button, click the Enforce Integrity Checks button to apply our constraints. When you click the Delete Sample Data button, an exception will be thrown, as illustrated. Furthermore, if you stop execution of the sample application and check your data in the SharePoint user interface, you’ll find that no changes have been made.

spexception was handled

Configure Referential Integrity Options Using the User Interface
You’ve seen how to enforce constraints on lookup fields programmatically. Let’s now look at how this can be done using the SharePoint user interface. Navigate to the Asset Notes list using Internet Explorer. From the ribbon, under List Tools, select List | List Settings. In the page that appears, find the Columns section, and then select Asset Reference. You should now see the Change Column page:

Configure Referential Integrity Options Using the User Interface

At the bottom of the page is a Relationship section with option buttons for restricting and cascading delete and a checkbox to enable or disable referential integrity. The options are currently set as specified by the code in our sample application; however, so that our Delete Sample Data function works properly, we need to change the selected option from Restrict Delete to Cascade Delete. Make the change and click OK to save it. Run the sample application. As before, click the Delete Sample Data button. This time, the function completes without error, and a quick check of the lists using Internet Explorer will confirm that all data has been deleted as expected.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Share Point 2010 Topics