Is there a way to select a project using ENVDTE? - c#

I want to add a Service reference to a project using ENVDTE. The only way is by the following command which pops up the Add service Reference window:
_applicationObject.ExecuteCommand("Project.AddServiceReference", string.Empty);
But this command will work only on currently selected project. Is there a way to select a particular project where the service reference is to be added?
DTE2.SelectedProjects has no way to select a project, it only helps to retrieve selected projects.

You have to navigate through the solution tree like this :
var se = _applicationObject.ToolWindows.SolutionExplorer;
var proj = se.GetItem("PathToYourProject");
proj.Select(vsUISelectionType.vsUISelectionTypeSelect);
After you launch your command.
PathToYourProject is a "pseudo" XPath. If you're workging in a project called "Project" in a solution called "Solution" your path will be : "Solution\Project"
Take care if your project is in a solution folder. It's a little bit more tricky. You have to expand solution folder like this :
var solutionFolder = se.GetItem("PathToYourSolutionFolder");
if (!solutionFolder .UIHierarchyItems.Expanded)
{
solutionFolder .UIHierarchyItems.Expanded = true;
}
Then you retrieve and select your project by this way :
var proj = solutionFolder .UIHierarchyItems.Item("ProjectName");
proj.Select(vsUISelectionType.vsUISelectionTypeSelect);
Finally, I'm not sure, but you may have to select the "Service References" node of your project before to launch the command.

Related

Add ClassLibrary Project programmatically

I have a VS 2017 extension and have tried using my own custom Project Template, by adding it programmatically, but things aren't going so well.
In my endeavour to find the mistake, I would like to see whether it is my custom Project Template causing the problem or not. Therefore I want to programmatically add any other existing built-in VS project such as a ClassLibrary type project template.
It seems to be located here:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ProjectTemplates\CSharp\Windows\1033\
But there isn't a zip folder and I can't create one in that directory according to Windows.
I will be using something similar to the following code:
Solution2 soln = (Solution2)visualStudioInstance.Solution;
string templatePath = soln.GetProjectTemplate("ClassLibrary.zip", "CSharp");
soln.AddFromTemplate(templatePath, projPath, "MyProjectName", false);
Am I on the right track?
I've tried it and I got an exception, but perhaps it was just because the zip folder doesn't exist.
UPDATE
The exception I get is:
"Object reference not set to an instance of an object."
I get it in the last line of the following code, when I try to add a reference to calcEngineProject. calcEngineProject is null even though it enters the if-statement and should be assigned the value of projCS.Object as VSProject2.
The code is as follows:
templatePath = soln.GetProjectTemplate("ClassLibrary.zip", "CSharp");
soln.AddFromTemplate(templatePath, prjPath, "ClassLibrary1", false);
foreach (Project p in soln.Projects)
{
if (String.Compare(p.Name, "ClassLibrary1") == 0)
{
projCS = p;
break;
}
}
if (projCS != null)
{
calcEngineProject = projCS.Object as VSProject2;
}
calcEngineProject.References.Add(Path.Combine(Config.Engine.EngineBinPath, "Engines.Calculation.dll"));
Also, I saw that templatePath is this:
"C:\PROGRAM FILES (X86)\MICROSOFT VISUAL
STUDIO\2017\PROFESSIONAL\COMMON7\IDE\EXTENSIONS\EYXTAMKA.FB4\ProjectTemplates\CSharp\.NET
Standard\1033\ClassLibrary\ClassLibrary.vstemplate"
and not
\%USERPROFILE%\Documents\My Exported Templates\
as mentioned in Upgrading custom project template

Roslyn - determine project excluded from build configuration

I am trying to figure out which project is enabled/disabled in respective build configuration/platform setup. Where could I find this "project.BuildsInCurrentConfiguration" information please?
var properties = new Dictionary<string, string>
{
{ "Configuration", "Debug" },
{ "Platform", "x86"}
};
MSBuildWorkspace workspace = MSBuildWorkspace.Create(properties);
workspace.LoadMetadataForReferencedProjects = true;
Solution solution = workspace.OpenSolutionAsync("someSolution.sln").Result;
foreach (Project project in solution.Projects)
Console.Out.WriteLine($"{project.OutputFilePath} is enabled in this build setup: {project.BuildsInCurrentConfiguration}");
workspace.CloseSolution();
I would have thought I wouldn't be offered the projects that are not part of the picked configuration/platform, but solution.Projects shows me all of them regardless build setup.
I don't think Roslyn really has most of that information right now (I'm not sure if it ever would; but I would hope it would). I don't see anything related to a "configuration" for a project with the Roslyn APIs for example. That seems to be delegated to the DTE interfaces. You can get at platform type in a Roslyn project, so conceptually you could only get projects that would apply to a given type of build:
var rolsynProjects = solution.Projects
.Where(p => p.CompilationOptions.Platform == Platform.X86);
but, things like "DEBUG" configuration seem to only be available via DTE--which isn't that hard to get at. e.g.
var project = DTE.Solution.Projects
.Where(p=>p.FullName == rolsynProjects.First().FilePath).FirstOrDefault();
And from that VS project, you can get at its ConfigurationManager

ReportingService2010 could not be found

I have:
private readonly ReportingService2010 _rs = new ReportingService2010();
Error:
The type or namespace name 'ReportingService2010' could not be found (are you missing a using directive or an assembly reference?)
I setup a reference to the SSRS service. The reference does not give me access to ReportingService2010 as I expect. The closest thing is:
MySsrsServiceNamespace.ReportingService2010SoapClient
How am I supposed to use the ReportingService2010 class? MSDN lists this class vaguely.
Please note I tried using ReportingService2010SoapClient. This class does not match the documentation for ReportingService2010. For example, ListChildren() only accepts 4 parameters and the Url property does not exist.
Just ran into the exact same issue. ReportingService2010SoapClient class was available, but the ReportingService2010 class was not. Was driving me nuts. I had added it as a "Service References", but you have to add it as a "Web References", like so:
Delete your old Service Reference
Right click on References. The "Add Service Reference" dialog comes up.
Do not enter the WSDL URL now, instead: Click on the "Advanced" button at the bottom left.
The "Service Reference Settings" dialog comes up.
At the bottom left, click the "Add Web Reference" button.
Now enter the URL for the WSDL. (for me that was servername/ReportServer/ReportService2010.asmx)
Click the small arrow on the right, it will take its sweet time to load.
Name the web reference, I used "ReportingService2010WebReference", but ReportingService2010" probably works just as well.
Click "Add Reference"
In your code, update your using statements to "using .ReportingService2010WebReference (or whatever name you picked)
Code:
private MySol.ReportService2010WebReference.ReportingService2010 rsClient;
rsClient = new ReportingService2010();
rsClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
CatalogItem[] items = null;
items = rsClient.ListChildren("/", false);
foreach (var item in items)
{
tr.ErrorMessage += (item.Path + " " + item.CreatedBy);
}
Worked on the first try. Web.config file wasn't touched.
Either create a proxy class and include it in your application or add a web reference to ReportingService. The tutorial is available there:
http://technet.microsoft.com/en-us/library/ms155134.aspx
Note that if you are going for proxy class and you are using more than one endpoint (ReportExecution, ReportingService) you should generate proxy classes on different namespaces, otherwise you will get clashes.
Did you do it by web reference? If so, try using WSDL at the command line. Command line syntax:
wsdl /language:CS /n:"Microsoft.SqlServer.ReportingServices2010" http://serverName/reportserver/ReportService2010.asmx?wsdl
Do not add a Webreference
Follow the following steps and it would work just fine.
1) Make sure you have .netframework >= 4.6.1
2) Run command prompt as administrator
3) cd C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools
4) Generate class: wsdl /proxyusername:Username /proxypassword:Password -out:Reportingservice2010.cs http://Servername/Reportserver/ReportService2010.asmx?wsdl
Additional) Run wsdl /? for help Files will output in: C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools
5) Add the .cs file to your project
Change this;
private readonly ReportingService2010 _rs = new ReportingService2010()
to
private readonly ReportingService2010SoapClient _rs = new ReportingService2010SoapClient()
You are attempting to create an instance to a class that does not exists and adding the reference creates a corresponding *Client class for you to instantiate.

Resolving .vcxproj dependencies with c# project class doesn't return included folders

I'm trying to write a tool which will resolve dependencies between c++ and c# projects.
When I try to get folders included in vcxproj, it returns everything but included folders.
I searched, where I would expect to find them:
ItemDefinitions -> ClCompile -> AdditionalIncludeDirectories
But they are not there, or anywhere else.
Project proj = new Project(projectFileName);
ProjectItemDefinition tp = proj.ItemDefinitions.First(pid =>
String.Compare(pid.Key,"ClCompile", true) == 0).Value;
ProjectMetadata tpPMD = tp.Metadata.First(pmd =>
String.Compare(pmd.Name,"AdditionalIncludeDirectories", true) == 0);
As I know, C# project and C++ project have different object. If you want to get the VC++ project you should reference VCProject and VCProjectEngine.
Here is an example about how to get the VC project you can refer to:
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vcprojectengine.vcproject.aspx

Visual Studio Addin "Exclude From Build" Property

I am currently trying to create an addin for Visual Studio 2008 that will list all files which are not excluded from the current build configuration.
I currently have test C++ console application that has 10 files, 2 of which are "Excluded From Build". This is a property that will allow a specific file to be excluded from a specific configuration (i.e. Debug or Release). This property is located when you right click on a file in the solution explorer and select Properties->Configuration Properties->General->Excluded From Build
At the moment I have the following code that will loop though all project files and get the properties for each file.
foreach (Project theProject in _applicationObject.Solution.Projects)
{
getFiles(theProject.ProjectItems);
}
private void getFiles(ProjectItems theItems)
{
foreach (ProjectItem theItem in theItems)
{
string theItemName = theItem.Name;
foreach (Property theProp in theItem.Properties)
{
string thePropName = theProp.Name;
}
getFiles(theItem.ProjectItems);
}
}
The issue I am having is that I cant seem to find the "Excluded From Build" property. I cannot find very good documentation on what properties are listed where. Where is this Excluded From Build property located within the _applicationObject object?
I'm not familiar with the Visual Studio object model, but in the documentation for VS2005 the following objects have an ExcludedFromBuild property:
VCFileConfiguration
VCFileConfigurationProperties
VCPreBuildEventTool
VCPreLinkEventTool
VCPostBuildEventTool
VCWebDeploymentTool
Hopefully this will lead you down the right path.

Categories