SharePoint 2010: Custom action that executes custom code
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):
04 | RegistrationType="List" |
06 | Location="Microsoft.SharePoint.StandardMenu" |
08 | ControlAssembly="[Fully qualified assembly name]" |
09 | ControlClass="MyNamespace.MyCustomAction"> |
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:
1 | public class MyCustomAction : WebControl |
3 | protected override void CreateChildControls() |
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"> |
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> |
11 | <CommandUIHandler Command="ContentTypeCommand" CommandAction="/MyWeb/_layouts/CustomPages/MyCustomApplicationPage.aspx" /> |
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" |
And finally, below is the code behind for the aspx page:
01 | namespace MyCustomNamespace |
03 | public class MyCustomControl: UserControl |
05 | protected override void OnLoad(EventArgs e) |
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