Add project parameter in SSIS dtproj

Vernou 86 Reputation points
2024-10-11T14:53:54.63+00:00

I am developing an extension to help with SSIS package development. I need to set project parameter.

I do this code :

private void SetProjectParameters(EnvDTE.Project project, string parameterName, string value)
{
    ThreadHelper.ThrowIfNotOnUIThread();

    foreach(EnvDTE.ProjectItem item in project.ProjectItems)
    {
        if (itemType.FullName == "Microsoft.DataWarehouse.VsIntegration.Shell.Project.Extensibility.ProjectItemExt")
        {
            var objectGetter = itemType.GetProperty("Object");
            var obj = objectGetter.GetValue(item);

            if(obj is Microsoft.SqlServer.Dts.Runtime.Parameters parameters)
            {
                if(!parameters.Contains(parameterName))
                {
                    var parameter = parameters.Add(parameterName, TypeCode.String);
                    parameter.Value = parameterName;
                }
            }
        }
    }
}

But it don't work. However it's a good start, because if I add a second parameter by the Visual Studio UI and save the modification, the programmatically added parameter is saved in the file Project.params. And after unload/reload the dtproj, the programmatically added parameter is visible.

Sound like it miss the save and refresh.

How to add a project parameter to a dtproj from a Visual Studio Extension?

Developer technologies | Visual Studio | Extensions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Omkara Varshitha Kunapalli (INFOSYS LIMITED) 1,825 Reputation points Microsoft External Staff
    2025-11-20T04:46:56.59+00:00

    Hello thanks for reaching out!

    Work on the UI thread:

    • Requirement: Ensure calls touching DTE/SSIS objects run on the UI thread (use ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync).
    • Get the SSIS project root item:
      • Action: Locate the .dtproj EnvDTE.ProjectItem (the one representing the SSIS project) in project.ProjectItems. Retrieve the underlying SSIS object model:
      • Action: From the SSIS project item, use ProjectItem.Object to get the SSIS project object.
      • Goal: Access Microsoft.SqlServer.Dts.Runtime.Project and then its Project.Parameters collection Add or update the parameter:
           - **Action:** If parameters.Contains(name) is false, call parameters.Add(name, TypeCode.String) and set parameter.Value = value.
        
                    - **Optional:** Set **Sensitive**, **Required**, and **Description** to match your needs.
        
        • Mark the project as dirty:
          • Action: Set the project’s dirty flag so VS knows it needs saving (e.g., via IVsHierarchy/IVsPersistHierarchyItem2 or by editing Project.params and marking it dirty).
          • **
            Simple fallback:** Call project.Save or DTE.Solution.SaveAll if available. Persist to disk (force save):
            • Action: Explicitly save both the dtproj and Project.params:
              • Save the SSIS project (EnvDTE.Project.Save).
              • If you edited the Parameters collection, ensure Project.params is saved; if needed, open it as a ProjectItem, edit, and call Document.Save. Trigger a refresh so the UI shows the change:
    • Action: Reload the SSIS project in VS:
    • Use VS shell APIs to unload/reload the project (e.g., IVsSolution to reopen), or
      • Close and reopen the Project.params document, or
        • Collapse/expand the project node to force re-read Validate visibility in designer:
          - **Action:** Open the SSIS Project Parameters window; confirm the parameter appears and matches the value.
          
                - **Check:** Build the project to ensure no parameter schema issues. **Handle edge cases (net effect you observed):**
          
                         - **If parameter appears only after manual save/reload:** Ensure steps 5–7 are executed; the SSIS designer caches parameters, so a programmatic add must be followed by explicit save and refresh.
          
                               - **Package into your extension command:**
          
                                     - **Action:** Wrap the logic into a command that: 
          
                                              - Switches to UI thread
          
                                                       - Adds/updates parameter
          
                                                                - Saves dtproj and Project.params
          
                                                                         - Reloads project or refreshes items
          
                                                                                  - Reports success/failure to the user. 
          

     

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.