title: How do you add custom menu actions programmatically in SharePoint?
body: <p>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:</p>
  
  <p>Added the CustomAction in my Element.xml file as:</p>
  
  <pre><code>&lt;CustomAction
        Id="MyID"
        RegistrationType="ContentType" 
        RegistrationId="0x010100ef19b15f43e64355b39431399657766e"
        Location="EditControlBlock"
        Sequence="1000"
        Title="My Menu Item"&gt;
    &lt;UrlAction Url="" /&gt;
  &lt;/CustomAction&gt;
  </code></pre>
  
  <p>In my feature receiver FeatureActivated method, I have:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>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.</p>