0

I'm developing Project Templates for Universal Windows Platform. In UWP projects we can add references in three ways which are Assemblies, NuGet and SDK. To add assemblies I simply use

VSProject.References.Add("MyAssembly.dll")

And I can install NuGet packages using NuGet.VisualStudio dll. But how can I add SDK into a UWP project programmatically like references and NuGet packages?

enter image description here

Mathivanan KP
  • 1,979
  • 16
  • 25

2 Answers2

1

You can use additem method of Microsoft.Build.Evaluation.Project to achieve it. Here is a sample demo for your reference.

DTE2 dte = this.ServiceProvider.GetService(typeof(DTE)) as DTE2;
EnvDTE.Project currentProject = dte.Solution.Projects.Item(1);
string projectPath = currentProject.FullName;
var project = new Microsoft.Build.Evaluation.Project(projectPath);

var proItem = project.GetItems("SDKReference").FirstOrDefault();

project.AddItem("SDKReference", "WindowsTeam, Version=10.0.14393.0", new[]
                        {
                            new KeyValuePair<string, string>("Name", "Windows Team Extensions for the UWP")
                        });

project.Save();

Update:

DTE2 dte = this.ServiceProvider.GetService(typeof(DTE)) as DTE2;
EnvDTE.Project currentProject = dte.Solution.Projects.Item(1);
string projectPath = currentProject.FullName;

Microsoft.Build.Evaluation.ProjectCollection projectCollection = new Microsoft.Build.Evaluation.ProjectCollection();

var project = projectCollection.LoadProject(projectPath);             projectCollection.UnloadProject(project);

project.AddItem("SDKReference", "WindowsTeam, Version=10.0.14393.0", new[]
                        {
                            new KeyValuePair<string, string>("Name", "Windows Team Extensions for the UWP")
                        });

project.Save();

projectCollection.LoadProject(projectPath);
Zhanglong Wu - MSFT
  • 1,652
  • 1
  • 7
  • 8
  • Thanks. Its working as expected. But after project created a dialog is showing, which states "Project modified out side the environment." (see [here](https://ibb.co/mvQZna) for screenshot) How can I avoid this? Please help. – Mathivanan KP May 23 '17 at 10:44
  • Please check this thread: https://stackoverflow.com/questions/44117270/visual-studio-extension-reload-project, unload project->modify csproj file -> reload project again. – Zhanglong Wu - MSFT May 24 '17 at 00:58
  • That thread's solution works for add ins only. Its not working for Project Templates. – Mathivanan KP May 24 '17 at 05:03
  • Same code not working for me. Still the dialog is showing. As a workaround I have added the sdk entry into the replacementdicationary and added a find string into the template's project file. Its working as expected. Thanks for your help. – Mathivanan KP May 25 '17 at 11:12
  • I am glad to know that you resolve the issue and provide your solution, please create a reply and mark it as answer, it will be beneficial to other communities who have the similar issue. – Zhanglong Wu - MSFT May 26 '17 at 05:59
0

I have added the sdk entry into the replacementdicationary like below and added a find string as $myuwpsdk$ into the template's project file. Its working as expected. It might be a incorrect method or a temporary work around. But it completes my requirement.

replacementsDictionary["$myuwpsdk$"] = "<ItemGroup><SDKReference Include = \"WindowsTeam, Version=10.0.14393.0\"> <Name>Windows Team Extensions for the UWP</Name> </SDKReference> </ItemGroup>";
Mathivanan KP
  • 1,979
  • 16
  • 25