views:

652

answers:

2

I need to add a custom menu action to a custom content type programmatically in c#. This is because I will not know the URL I need to link to beforehand. The URL to link to will be pulled from configuration when the feature is activated. I have tried the following:

Added the CustomAction in my Element.xml file as:

<CustomAction
      Id="MyID"
      RegistrationType="ContentType" 
      RegistrationId="0x010100ef19b15f43e64355b39431399657766e"
      Location="EditControlBlock"
      Sequence="1000"
      Title="My Menu Item">
  <UrlAction Url="" />
</CustomAction>

In my feature receiver FeatureActivated method, I have:

SPElementDefinitionCollection eleCollection = 
    properties.Feature.Definition.GetElementDefinitions(
        new System.Globalization.CultureInfo(1));

foreach (SPElementDefinition ele in eleCollection)
{
    if (ele.Id == "MyID")
    {
        System.Xml.XmlNode node = ele.XmlDefinition.FirstChild;
        node.Attributes[0].Value = "MY URL";
        ele.FeatureDefinition.Update(true);
    }
}

I would expect this code to update the UrlAction Url with "MY URL" but it does not. If I hard code a URL in the XML it works but I must be able to do it programmatically.

+1  A: 

Depending on what you want to achieve, you can use some javascript;

<UrlAction Url="JavaScript:window.location='{SiteUrl}/_layouts/CustomListAction.aspx?ID={ListId}'"/>

the ~site and ~siteCollection also works:

<UrlAction Url="~site/_layouts/Page.aspx?ID={ListId}"/>
F.Aquino
It's a shame that the ECB custom doesn't support a ControlClass, which would give more flexibility than the old {SiteUrl} macros.
kbrimington
A: 

I don't think the WSS schema definition allows for an empty Url attribute in the UrlAction element. Maybe try putting a "default" url in the xml that you overwrite later?

Junx