The SharePoint development tools in Microsoft Visual Studio 2010 make it easier to create business solutions, by using SharePoint project items. This Microsoft SharePoint 2010 Visual How To demonstrates how to add SharePoint project items to create a new list with event handlers that enforce custom business rules.
Start by creating a new SharePoint site by using the Blank site template. You will use this site as a test site. Next, launch Microsoft Visual Studio 2010 and create a new SharePoint project named Movies by using the Empty SharePoint Project template. When you are prompted by the SharePoint Configuration Wizard, add the URL of a local test site and select Deploy as a sandboxed solution. Now, build the project using the following steps.
To build the SharePoint project and add SharePoint project items
- In Solution Explorer, right-click the Movies project node, click Add, and then click New Item. In the Add New Item dialog box, create a new List Instance project item named Movies. When you create a new list instance, you are prompted for information by the SharePoint Configuration Wizard. Give the new list a display name of Movies. Instantiate the new list as a Custom List. Provide a relative URL of Lists/Movies for the list, and then click Finish.
- In Solution Explorer, notice that there is a new project item named Movies and a new feature in the Features node named Feature1. Right-click the node for Feature1, and then rename this feature MainSite.
- Open MainSite in the Feature Designer. Change the Title of the feature to Movies and change the Scope of the feature from Web to Site. This change configures the feature to activate at the level of the site collection instead of at the site level.
- In Solution Explorer, select the new project item named Movies, and examine its properties in the Visual Studio property sheet. Also examine the Elements.xml file.
- Ensure that the Output Window is showing in Visual Studio 2010. If you cannot see the Output Window, on the Visual Studio 2010 View menu, click Output. Or, use the keyboard shortcut CTRL+ALT+O.
- In Solution Explorer, right-click the Movies project node, and then execute the Deploy command. Watch the progress of the Deploy command in the Output Window.
- After the Deploy command completes, return to the test site in the browser and refresh the home page. After the page refreshes, you should see a link to the new Movies list in the Quick Launch list.
- In the browser, navigate to the Movies list, and then add a new list item.
- Return to Visual Studio 2010, and rerun the Deploy command. A Deployment Conflicts dialog box appears that informs you that the target test site already contains a list with a title of Movies. Click the Resolve Automatically button, which deletes the existing Movies list and re-creates a new one. After the Deploy command completes, return to the browser and refresh the page. Verify that the Movies list no longer contains the item that you created earlier. This is because the list was automatically deleted and re-created by the deployment conflict resolution capability that is built into the SharePoint development tools in Microsoft Visual Studio 2010.
- In Solution Explorer, select the new project item named Movies, and examine its properties in the Visual Studio property sheet. Locate the Deployment Conflict Resolution property and change its value to Automatic. Now rerun the Deploy command. The SharePoint development tools in Microsoft Visual Studio 2010 should now delete and then re-create the Movies list without prompting you each time that you run the Deploy command.
- Now, you will add a new project item for an event receiver. In Solution Explorer, right-click the Movies project node, select Add, and then click New Item. In the Add New Item dialog box, create a new Event Receiver project item named MoviesListEvents. The SharePoint Configuration Wizard prompts you for more information to create the new project item. Select List Item Events for the type of event receiver that you want to create. For the event source, select Custom List. For events to handle, select an item is being added and an item was added, and then click Finish.
- Open the
Elements.xml file in the MoviesListEvents project item folder. Modify the opening Receiver tag by removing the ListTemplateId attribute and adding the ListUrl attribute, so that your event handler is bound to a single list instance instead of being bound to all the lists of a specific type.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListUrl="Lists/Movies" >
...
- You must now update the Receiver element for the after event named ItemAdded, to make it a synchronous event. To do this, add a Synchronization element that contains a value of Asynchronous, as shown in the following example.
<Receiver>
<Name>MoviesEventListItemAdded</Name>
<Type>ItemAdded</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>Movies.MoviesEventList.MoviesEventList</Class>
<SequenceNumber>10000</SequenceNumber>
<Synchronization>Asynchronous</Synchronization></Receiver>
- Now you can update your event handlers. Implement the ItemAdding event handler with a validation rule to prohibit the user from adding an ampersand (&) in movie titles, as shown in the following example.
public override void ItemAdding(SPItemEventProperties properties) {
string val = properties.AfterProperties["Title"].ToString();
// Prohibit user from adding ampersand (&) in movie title.
if(val.Contains("&")){
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.Cancel = true;
properties.ErrorMessage = "Ttitle cannot contain ampersand (&)";
}
}
- Implement the after event handler named ItemAdded to convert the text in every movie title to uppercase letters.
public override void ItemAdded(SPItemEventProperties properties) {
string val = properties.ListItem["Title"].ToString();
properties.ListItem["Title"] = val.ToUpper();
properties.ListItem.UpdateOverwriteVersion();
}
- Test your work by pressing the F5 key. When the browser opens, play the part of a user and add a few new movie titles into the Movies list. Verify that you receive an error when you attempt to add a new movie title that contains an ampersand. Also, verify that all movie title text is converted to uppercase letters automatically.
|
Watch the video
Length: 00:18:41
About the Author
Ted Pattison is an author, instructor, and co-founder of Critical Path Training, a company dedicated to education about SharePoint technologies. As a Microsoft SharePoint Most Valuable Professional (MVP), Ted frequently works with the Microsoft Developer Platform Evangelism group to research and author SharePoint training material for developers early in the product life cycle, while the product is in its alpha and beta stages. Ted is also co-author of Inside Microsoft SharePoint 2010. |
No comments:
Post a Comment