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):
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="MyCustomAction"
RegistrationType="List"
GroupId="ActionsMenu"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="1000"
ControlAssembly="[Fully qualified assembly name]"
ControlClass="MyNamespace.MyCustomAction">
</CustomAction>
</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:
public class MyCustomAction : WebControl
{
protected override void CreateChildControls()
{
// Do some stuff
}
}
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:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction Id="ContentTypeRibbonCustomization" RegistrationId="10005" RegistrationType="List" Location="CommandUI.Ribbon.ListView" Sequence="95" Title="Run Custom Code">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.List.Settings.Controls._children">
<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"/>
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler Command="ContentTypeCommand" CommandAction="/MyWeb/_layouts/CustomPages/MyCustomApplicationPage.aspx" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</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:
<%@ Assembly Name="MyCustomAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e246903334b3e97b" %>
<%@ Page Language="C#" AutoEventWireup="true" Inherits="MyCustomNamespace.MyCustomControl"
Debug="true" %>
And finally, below is the code behind for the aspx page:
namespace MyCustomNamespace
{
public class MyCustomControl: UserControl
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// run some custom code
// then redirect the user back to the original page
}
}
}
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).
Hi, your first code works fine on SP2010, you just need to add your WebControls to the safecontrols section of your web.config.
It can be done by editing your project manifest template, for example:
<Assembly Location=".dll” DeploymentTarget=”GlobalAssemblyCache” >
<SafeControl Assembly="$SharePoint.Project.AssemblyFullName$" Namespace="” TypeName=”*” Safe=”True” />
Should look Better..
<Solution xmlns="http://schemas.microsoft.com/sharepoint/"> <Assemblies> <Assembly Location="OddoAM.WebSite.dll" DeploymentTarget="GlobalAssemblyCache" > <SafeControls> <SafeControl Assembly="$SharePoint.Project.AssemblyFullName$" Namespace="OddoAM.WebSite.WebControls" TypeName="*" Safe="True" /> <SafeControl Assembly="$SharePoint.Project.AssemblyFullName$" Namespace="OddoAM.WebSite.WebParts" TypeName="*" Safe="True" /> </SafeControls> </Assembly> </Assemblies> </Solution>Hi Mathieu & Jalil,
Really Thanks to provide this blog and clear understanding.
But now I have one small query… as Jalil says I have added one custom button on view item page and on the click on that i just want to fire one custom function but don’t want to open any other page or such like popup. just want to fire one custom function.
Now as Mathieu said in this reply this may possible through above (First Passage) Code. But can mathieu would you please tell me will it open the new aspx page or just execute directly ? and if it execute directly then should i create new class file under that vs2010 project and compile that ? don’t know the process to achieve that if you can help.