CHAPTER 7 Custom Development with Visual Studio
So far in this app, we’ve discussed two customization tools for SharePoint Online: a web browser (see Chapter 4) and SharePoint Designer 2010 (see Chapter 5). But there’s another tool—much more powerful and flexible than the first two—that will be the subject of this chapter: Visual Studio 2010.
If you have a background in software development and have developed solutions using the Microsoft .NET framework, you’re probably no stranger to Visual Studio. It’s the development tool of choice for most .NET developers and has an extensive array of features built in to aid you in developing for the Microsoft platform.
Up until the 2010 version, Visual Studio didn’t have much to offer for SharePoint development (even with add-ons such as VSeWSS, the experience was pretty marginal), but thankfully those days are behind us now. Visual Studio 2010 introduces a wealth of new capabilities and templates that specifically target SharePoint development. While many of them were created with SharePoint 2010 in mind (rather than SharePoint Online), most of them still apply in the Office 365 world and will help us in developing
In this chapter, we’ll cover the following topics:
• When to Use Visual Studio for customization
• Preparing Visual Studio for SharePoint Online development
• Creating a project
• Restrictions of the sandboxed environment
• Creating a feature and adding a feature receiver
• Creating a web part
• Creating a module to deploy files
• Creating an event receiver
• Creating a content type
• Creating a list definition
• Creating a custom action
• Packaging and deploying your solution
• Tips and recommendations
When to Use Visual Studio for Customization
On the customization spectrum, Visual Studio is definitely at the advanced end. While a site administrator can customize SharePoint Online with a browser and a power user or web designer can customize SharePoint Online with SharePoint Designer 2010, the use of Visual Studio requires full- fledged software development skills. It also requires knowledge of SharePoint as a development platform (i.e., understanding that SharePoint is built on top of ASP.NET and knowing what its extension points are).
So when is it appropriate to make the jump to Visual Studio for your customizations? There’s no one simple answer to this question, but here are some questions you can ask yourself that will help make that determination. Think of them as guidelines.
Have You Exceeded What You Can Do in the Browser and SharePoint Designer?
This is an easy one. If the customization you want to make goes beyond what can be done in the browser or SharePoint Designer, Visual Studio is the only other choice. Of course, even that consideration assumes that the customization is possible in SharePoint Online in the first place. If the customization you want to make requires coding and deploying a custom timer job to the farm, for example, you’re out of luck whether you’ve got Visual Studio or not (since that goes beyond the limits of what you’re allowed to do in SharePoint Online).
Who Will Be Doing the Work?
Visual Studio is definitely a tool aimed squarely at developers. Power users and designers generally aren’t familiar with it and don’t use it. If a developer or group of developers will be making customizations for SharePoint Online, Visual Studio will likely be the tool of choice. However, that’s not to say developers may not also use other tools like SharePoint Designer to accomplish some of the customizations. It all comes down to what needs to be done and what the best tool is for the job. (For example, even developers will find SharePoint Designer useful for creating custom workflows for SharePoint Online.)
How Reusable Do the Customizations Need to Be?
If you’re developing customizations that will be used over and over again in SharePoint Online (for example, to be deployed to multiple sites or site collections that may even reside in multiple tenancies), Visual Studio is a great tool for packaging up those customizations and making them reusable. While you can certainly accomplish some of that without Visual Studio (e.g., creating a list template from a list and using it in multiple sites), Visual Studio offers more options and flexibility for creating truly reusable customizations that can even include custom code if desired.
Preparing Visual Studio for SharePoint Online Development
Setting Up Your Development Environment
Before you can use Visual Studio to customize SharePoint Online, you’ll need a development environment you can develop and test against. If you don’t already have a development environment set up, see Chapter 3 for instructions on how to do so.
Extending Visual Studio
One extremely useful extension that will help you develop solutions for SharePoint Online is the CKS Development Tools for Visual Studio extension for Visual Studio 2010. The extension comes in two flavors (one targeting SharePoint 2010 Foundation and one targeting SharePoint 2010 Server) and is available from Microsoft’s online Visual Studio Gallery.
To get the extension, open the Visual Studio extension gallery (either by clicking Tools > Extension Manager or by navigating to visualstudiogallery.msdn.microsoft.com) and search for “cks dev.” Both flavors should come back in the search results. Download the one that matches the edition of SharePoint you’re using in your development environment. The extension adds several new features and capabilities to Visual Studio, including:
• Several new project item templates (e.g., a Custom Action template for creating custom actions)
• Additional debugging and deployment options
• A “SharePoint” tab in the Add References dialog that lets you easily add SharePoint DLL references to your project without having to go browse the SharePoint root/hive folder and find them
• A Sandboxed Visual Web Part project item template
• Compile-time checking of sandboxed code to ensure that you’re not using types or members that aren’t allowed in sandboxed solutions
Creating a Project
If you don’t already have a project created for your customizations (for example, that you’re grabbing from a source control system), that’s the first thing you’ll need to do.
To create a new SharePoint project, follow these steps:
1. Open the File menu in Visual Studio, and choose New Project.
2. On the New Project dialog, expand the SharePoint node on the left and choose 2010. Select the Empty SharePoint Project template, give it a name, and click OK. See Figure 7-1 for an illustration of this step.
Figure 7-1. Creating a new SharePoint project in Visual Studio 2010
Tip If you’re creating a new solution and you think you may end up housing multiple projects, we recommend checking the Create directory for solution box, as shown in Figure 7-1. This will create a parent folder (which we’ve named “Chapter7.SharePoint”) for the solution that can house all your projects.
3. Figure 7-2 shows the SharePoint Customization Wizard. Choose a local site for development/debugging and select the Deploy as a sandboxed solution option (which is the only valid choice when developing for SharePoint Online even though Visual Studio doesn’t specifically call that out). Click the Finish button to create the project.
Figure 7-2. SharePoint Customization Wizard