Sunday, December 23, 2012

SharePoint 2010: Custom action that executes custom code

SharePoint 2010: Custom action that executes custom code

 
 
 
 
 
 
1 Vote

In 2007 it was possible to create a custom action and then link it to some code.
You did this by first declaring your custom action in an elements.xml file (which you would then deploy as part of a feature):
02  <CustomAction
03    Id="MyCustomAction"
04    RegistrationType="List"
05    GroupId="ActionsMenu"
06    Location="Microsoft.SharePoint.StandardMenu"
07    Sequence="1000"
08    ControlAssembly="[Fully qualified assembly name]"
09    ControlClass="MyNamespace.MyCustomAction">
10  </CustomAction>
11</Elements>
In the ControlAssembly and ControlClass attributes you specified your custom assembly and your custom class (a WebControl) that contained your custom code.
Then you created your custom WebControl:
1public class MyCustomAction : WebControl
2{
3    protected override void CreateChildControls()
4    {
5        // Do some stuff
6    }
7}
Using this way we were able able to execute some custom code when someone clicked our custom action.
However, it seems that we cannot use this method in SharePoint 2010 although there are a few workarounds to achieve the same result. Below I will show you a way I used.
To start off with, I created an elements.xml file with the following declaration:
01<?xml version="1.0" encoding="utf-8"?>
03  <CustomAction Id="ContentTypeRibbonCustomization" RegistrationId="10005" RegistrationType="List" Location="CommandUI.Ribbon.ListView" Sequence="95" Title="Run Custom Code">
04    <CommandUIExtension>
05      <CommandUIDefinitions>
06        <CommandUIDefinition Location="Ribbon.List.Settings.Controls._children">
07          <Button Id="ContentTypeTest.Button" Image16by16="/_layouts/images/LSTPEND.gif" Image32by32="/_layouts/images/centraladmin_configurationwizards_farmconfiguration_32x32.png" Command="ContentTypeCommand" CommandType="General" Description="Runs some custom Code" TemplateAlias="o2" Sequence="95" LabelText="Perform My Action"/>
08        </CommandUIDefinition>
09      </CommandUIDefinitions>
10      <CommandUIHandlers>
11        <CommandUIHandler Command="ContentTypeCommand" CommandAction="/MyWeb/_layouts/CustomPages/MyCustomApplicationPage.aspx" />
12      </CommandUIHandlers>
13    </CommandUIExtension>
14  </CustomAction>
15</Elements>
This specifies that my Custom Action will appear on my custom list with TemplateType of 10005 (specified in the RegistrationId attribute) in the list view section.
I then used a Feature to depoy my Custom Action.
All my Custom Action does is to send the user to a custom page that is located in the Layouts folder:
/MyWeb/_layouts/CustomPages/MyCustomApplicationPage.aspx
I then created my custom aspx page. Below is the mark up for the page:
1<%@ Assembly Name="MyCustomAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e246903334b3e97b" %>
2<%@ Page Language="C#" AutoEventWireup="true" Inherits="MyCustomNamespace.MyCustomControl"
3    Debug="true" %>
And finally, below is the code behind for the aspx page:
01namespace MyCustomNamespace
02{
03    public class MyCustomControl: UserControl
04    {   
05        protected override void OnLoad(EventArgs e)
06        {
07            base.OnLoad(e);
08            // run some custom code
09            // then redirect the user back to the original page
10        }
11    }
12}
Using this way I was able to link some custom code that would execute in response to someone clicking my custom action.
For everything you need to know about customising the SharePoin Ribbon please go here to Chris O’Brien’s blog post.
After spending a lot of time investigating how to customise the Ribbon to meet the requirements I had I can safely say this is by far the best and most comprehensive blog post I found on the net.
As I said earlier there are other ways you can use to achieve the same objective. You can easily link a custom action to execute some javascript function. I guess one option would be to link it to a javascript function that then invoked some server side code. I havent tried this myself so I cannot say for sure if this approach would work.
If you had a similar issue to the one I had above, I would be interested in hearing what approach you used to resolve it (if different to the approach I used).

No comments:

Post a Comment