Develop custom button MS Project Add-in in the Ribbon

Hello Readers,

I’m going to discuss about developing a custom button under the Ribbon of MS Project application using c#. We can extend the Ribbon functionality and develop our own custom buttons and associate custom code with the button event. Lets look at a sample code to create a new button and associate custom code to create a new task in the project.

Steps: As I discussed in my previous post ( MS Project addin development) https://sharepointprojectserver.com/ms-project-add-in-development/ create a new project addin solution then follow the below steps to extend the Ribbon.

  1. Right click on your Solution and Add a new Item ( Ribbon.xml).
  2. Your solution folder structure will be as follows

3.  Open Ribbon.xml file and add the below code

Group Name : My Group , Button Lable: Insert Task  , ButtonAction : OnTextButton (function of your custom code)


<tabs>
      <tab idMso="TabAddIns">
        <group id="MyGroup" label="My Group">
          <button id="textButton" label="Insert Task" screentip="tip text" onAction="OnTextButton" supertip="This button inserts the task"  />          
        </group>
      </tab>
    </tabs>

4. Open Ribbon.cs file add the following code


//Add the below DLLs

using Office = Microsoft.Office.Core;
using MsProject = Microsoft.Office.Interop.MSProject;</pre>
public void OnTextButton(Office.IRibbonControl control)
{
MsProject.Task newTask = Globals.ThisAddIn.Application.ActiveProject.Tasks.Add("This is a new Tasks created from Custom Button action",    System.Type.Missing);
newTask.Start = DateTime.Today;
newTask.Duration = "5";
newTask.ResourceNames = "Res1,Res2";
System.Windows.Forms.MessageBox.Show("You request for a new task to be created.","New Task     Alert",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information,System.Windows.Forms.MessageBoxDefaultButton.Button1);

}

That’s it .. Now It’s time test our add-in.  Build it and run the code.

Now you can see a button under the “Add-in” Ribbon. Click on it, it will create a new task in the schedule. Below is the output screenshot.

Now, to uninstall the add-in from the MS Project application, Righ-Click on the Solution in Visual studio and click on “Clean” option.

That’s it. Thanks for reading the post.

-Rajkumar Allepu.