In this exercise, you create a custom list definition in Microsoft SharePoint 2010 and then create an event receiver that is triggered when the list is used. We will be following the Microsoft guide found here:
http://msdn.microsoft.com/en-us/library/ff728096.aspx
To create the SharePoint project
- To start Visual Studio 2010, click the Start Menu, click All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.
- On the File menu, point to New, and then click Project.
- In the New Project dialog window, in the Installed Templates section, click Visual C#, clickSharePoint, and then click 2010.
- Click List Definition from the project items.
- In the Name box, type Bugs and then click OK.
- In the SharePoint Customization Wizard, type the local Web site that you want to use for this exercise (Mine was http://neronguyen/sites/SharePointDevelopment).
- For the trust level, select Deploy as a farm solution and then click Next.
- In the What is the display name of the list definition? Box, type Bugs.
- In the What is the type of the list definition? drop-down list, select Custom List.
- Select the Add a list instance for this list definition check box and then click Finish.
Customize the SharePoint List Definition
This task guides you through customizing the List Definition project template by changing the list name, the content type it stores, and the fields that are displayed in the new form, edit form, and display form.
To customize the list definition
- In Solution Explorer, expand ListInstance1 and open the Elements.xml file.
- In Solution Explorer, before Schema.xml, open Elements.xml.
- Insert the following XML into the top of the Elements.xml file above the ListTemplate line. This XML describes the Bug Item content type the list stores.
<!-- Describes content types. --> <ContentType ID="0x010089E3E6DB8C9B4B3FBB980447E313CE94" Name="Bug Item" Group="Custom Content Types" Description="Bug item content type." Version="0"> <FieldRefs> <FieldRef ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" /> <FieldRef ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" /> <FieldRef ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" /> </FieldRefs> </ContentType> <!-- End describes content types. -->
6. Insert the following XML above what you just entered in Step 5 in the Elements.xml file. This XML describes the fields the Bug Item content type uses.
<!-- Describes fields in bug list. --> <Field Type="Note" DisplayName="Description" Required="FALSE" NumLines="6" RichText="FALSE" Sortable="FALSE" ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" StaticName="BugDescription" Name="BugDescription" Group="Custom Columns" /> <Field Type="Text" DisplayName="Project" Required="FALSE" MaxLength="255" ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" StaticName="BugProject" Name="BugProject" Group="Custom Columns" /> <Field Type="Text" DisplayName="Assigned To" Required="FALSE" MaxLength="255" ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" StaticName="BugAssignedTo" Name="BugAssignedTo" Group="Custom Columns" /> <!-- End describes fields in bug list. -->
7. In Solution Explorer, open Schema.xml.
8. Add the
EnableContentTypes=”TRUE”
attribute to the List element inside the Schema.xml file.
9. Insert the following XML right after the <ContentTypes> element in the schema.xml file. This XML describes the Bug Item content type this list stores.
<!-- Start Add. --> <ContentTypeRef ID="0x010089E3E6DB8C9B4B3FBB980447E313CE94" /> <!-- End Add. -->
10. Insert the following XML right after the <Fields> element. This XML describes the fields the list stores. These are directly related to the fields in the content type that you added in the previous step.
<!-- Start Add. --> <Field Type="Note" DisplayName="Description" Required="FALSE" NumLines="6" RichText="FALSE" Sortable="FALSE" ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" StaticName="BugDescription" Name="BugDescription" Group="Custom Columns" /> <Field Type="Text" DisplayName="Project" Required="FALSE" MaxLength="255" ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" StaticName="BugProject" Name="BugProject" Group="Custom Columns" /> <Field Type="Text" DisplayName="Assigned To" Required="FALSE" MaxLength="255" ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" StaticName="BugAssignedTo" Name="BugAssignedTo" Group="Custom Columns" /> <!-- End Add. -->
11. Insert the following XML into the <ViewFields> element in the second view, where
BaseViewID="1"
. This XML describes which fields should be visible in this particular view. BaseView 1 is set as the default view. This is the view the newly created fields should display in.<!-- Start Add. --> <FieldRef Name="BugDescription"></FieldRef> <FieldRef Name="BugProject"></FieldRef> <FieldRef Name="BugAssignedTo"></FieldRef> <!-- End Add. -->
12. In Solution Explorer, right-click the Bugs node, and then click Deploy.
Test the Solution
In this task, you test the solution by adding a bug to the Bugs list.
To add an item to the Bugs list
- Open the website that you specified previously.
- On the Home page, click the Bugs list in the left navigation pane.
- On the List Tools tab, click Items, and then click Bug Item on the New Item drop-down list.
5. Type Who is going to track this bug? in the Description box, type Writing developer articles in theProject box, and then click Save.
Add the Event Receiver Code
In this task, you create and attach an event receiver to the list definition. This event receiver prevents the deletion of bug items and displays a message.
To add the event receiver to the list definition
- In Solution Explorer, right-click the Bugs node, point to Add, and then click New Item.
- In the New Project dialog window, in the Installed Templates section, click Visual C#, clickSharePoint, and then click 2010.
- From the project items, click Event Receiver.
- Type BugListItemEvent in the Name box, and then click Add.
- In the SharePoint Customization Wizard, select the An item is being deleted option, and then clickFinish.
- In Solution Explorer, expand BugListItemEvent, and then open the Elements.xml file.
- In Solution Explorer, expand BugListItemEvent and then open BugListItemEvent.cs. You should see the overridden ItemDeleting method.
- Insert the following code into the body of ItemDeleting method after the
base.ItemDeleting(properties);
statement. This code prevents the deletion of an item and display an error message.
try { properties.Cancel = true; properties.ErrorMessage = "Bugs can only be resolved not deleted!"; } catch (Exception ex) { return; } finally { this.EventFiringEnabled = true; }
10. In Solution Explorer, right-click the Bugs node and then click Deploy. If you get a deployment error message, click Resolve Automatically.
11. Open the website you specified previously.
12. On the Home page, in the left navigation pane, click the Bugs list.
13. On the List Tools tab, click Items, and then on the New Item drop-down list, click Bug Item. The Bugs – New Item screen is displayed.
14. To create a bug, type information into the applicable boxes, and then click Save.
15. Next, try to delete the item. You should receive a message similar to the one shown:
Congratulations! You’ve successfully created a custom List definition and created an event receiver for any deletions.
No comments:
Post a Comment