[SharePoint 2010] Create a Simple Custom Action Using Visual Studio 2010

Visual Studio 2010 has added a multitude of new features to make custom development on SharePoint 2010 much simpler that it was previously. Though Visual Studio 2010 does not include a simple wizard or other simpler process for creating Custom Actions, we can still take advantage of the simple packaging, debugging, and deployment features available for SharePoint 2010. In addition, Custom Actions point to custom application pages or other custom functions contained in the same Visual Studio solution.

You will need a development environment with SharePoint Foundation 2010 or SharePoint Server 2010 as well as Visual Studio 2010. Developing for SharePoint 2010 in Visual Studio 2010 requires you to run them both on the same machine (not your production server however). For information on setting up your development environment, see http://msdn.microsoft.com/en-us/library/ee554869.aspx.

To get started:

  • Create an “Empty SharePoint Project” in Visual Studio 2010:

    • Be sure to set the target framework to “.NET Framework 3.5″. SharePoint 2010 doesn’t support.NET Framework 4.0.
    • You have your choice of Visual Basic or Visual C#. I prefer C#, so all of my code examples will be in C#.
  • Specify the local site you are using for debugging (the one running on the local machine) and whether the solution will be sandboxed or a farm solution:

    • Custom Actions are available in Sandboxed Solutions, however, the available functionality will be limited.
  • Visual Studio does not have a template for Custom Actions. Instead you will have to create an Empty Element:
    • In the Solution Explorer, right click your solution name and Add > New Item:

  • Select Empty Element and provide your action with a name. The name is simply the file name, not how it will actually show up in SharePoint.

Now you have an empty Elements.xml file to create your Custom Action with:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
</Elements>

For this example, paste the following in place of the existing content:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="SiteActionsToolbar" GroupId="SiteActions" Location="Microsoft.SharePoint.StandardMenu" Sequence="1000" Title="Bing">
    <UrlAction Url="http://www.bing.com"/>
  </CustomAction>
</Elements>

This simple Custom Action will add a link to Bing on the Site Actions menu. Now deploy your solution to SharePoint to test it:

  • Right click the Solution Name in the Solution Explorer and select Deploy:

  • Visual Studio will indicate when it has completed building and deploying the solution to your local SharePoint site:

  • Now navigate to your SharePoint site in a web browser. Under the Site Actions you will now have a link to Bing:

That is how a simple Custom Action is created. In upcoming articles I will demonstrate how to add Custom Actions to different parts of the SharePoint Interface, such as the Ribbon and Site Settings.

John Chapman

Hello, I'm John Chapman. I am a SharePoint Developer for Sitrion (formerly NewsGator) living in Denver, Colorado. I develop solutions using SharePoint and .NET, and I thrive on the challenge of writing code to overcome the impossible, annoying, or otherwise difficult obstacles.

More Posts - Website - Twitter - LinkedIn - Google Plus

  • Muhammad Kazim

    Hi,

    Very nice share, Thanks.

    Would it be working on WSS / MOSS ? Or its just SharePoint 2010 specific ?

    • http://www.sharepointjohn.com John Chapman

      Unfortunately doing this in Visual Studio 2010 only works for SharePoint 2010. With that said, the elements.xml file for the Custom Action is nearly identical for 2007 vs 2010. Here is a good place to start for WSS3/MOSS Custom Actions: http://msdn.microsoft.com/en-us/library/ms473643%28v=office.12%29.aspx

  • Nitin

    Thanks ! it worked for me after some more work on establishing sharepoint web application first.

    Thanks again for clear cut steps!

  • vasundra

    I need to create a custom action under site action menu using Sandbox solution.
    In code , we have some other requirement so we are trying to override the createchildcontrol() method.
    MenuItem template is not supported in Sandbox .searching for some replacement for Menuitem template class.

    Regards,
    Vasundra

  • Purnima Swamy

    I would like to know how to get the URL for New Site in SiteAction. I would like to create a custom button on click should navigate to New Site window