Visual Studio Development Bookmark and Share   
 index > Visual Studio Guidance Automation Toolkit > Project reference
 

Project reference

Hello everyone,

I am using software factory toolkit.

I want to get reference of one project into another

I called an action from CreateSolution.xml file

<Action Type="Test.Actions.AddProjectReference,Test" Name="Addref"></Action

And at AddProjectReference.cs file I do following

protected override void OnExecute()
{
string projectName="Portal.Test.Business";
string refProject="Portal.Test.Shared";


AddReference(projectName, refProject);

}

private void AddReference(string projectName,string refProject)
{
EnvDTE.DTE dte = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));
Project project = null;
Project referenceProject = null;

//*****************************************
foreach (Project proj in dte.Solution.Projects)
{
if (proj != null)
{
if (proj.Name.Equals(projectName))
project = proj;
else if (proj.Name.Equals(refProject))
referenceProject = proj;
}
}
//*************************************************
if (project != null && referenceProject != null)
{

VSLangProj.VSProject aVsproject = (VSLangProj.VSProject) project.Object;

aVsproject.References.AddProject(referenceProject);
}
}
Note:I want reference of Portal.Test.Shared projectinto Portal.Test.Business

Mynew package runs successfully,but at reference node ofPortal.Test.Business ,I did'nt found reference ofPortal.Test.Shared

Tell me where I am wrong

Thanks .

Monesh  Thursday, June 14, 2007 1:02 PM

Hi Monesh,

You should use the Project object instead its name. Here you've a simply snippet of how you can do that.

(here I'm assuming that you already have two input properties ReferringProject and ReferencedProject)

VSProject vsProject = referringProject.Object as VSProject;

if(vsProject != null)

{

if(DteHelper.IsWebProject(referencedProject))

{

VsWebSite.VSWebSite vsWebSite = (VsWebSite.VSWebSite)(referencedProject.Object);

foreach(VsWebSite.WebService webService in vsWebSite.WebServices)

{

vsProject.AddWebReference(webService.URL);

}

}

else

{

vsProject.References.AddProject(referencedProject);

}

}

hope it helps.

Jose Escrich  Thursday, June 14, 2007 4:05 PM
When iterate through the solution Projects collection, that projects could be a Project or a SolutionFolder, so you should to check the Name and also the Kind for each (you'll get the guid solution folder in ProjectKinds.vsProjectKindSolutionFolder). Another thing is that you should implement a recursive loop in order to find out projects inside SolutionFolder.

If you're using SFT in the Library you've a SolutionModelService which is an abstraction of a solution where you can find Projects in whole solution using predicates.

List<ProjectModel> FindAllProjects(Predicate<ProjectModel> match)

here you've an example

List<ProjectModel> projectModels = FindAllProjects(new Predicate<ProjectModel>(delegate(ProjectModel project)
{
return project.Project.Name.Equals("foo");
}));

Notice that ProjectModel is an abstraction of project and it has a Project property which you can use to add the references.
Jose Escrich  Tuesday, June 19, 2007 1:26 AM

Hi Monesh,

You should use the Project object instead its name. Here you've a simply snippet of how you can do that.

(here I'm assuming that you already have two input properties ReferringProject and ReferencedProject)

VSProject vsProject = referringProject.Object as VSProject;

if(vsProject != null)

{

if(DteHelper.IsWebProject(referencedProject))

{

VsWebSite.VSWebSite vsWebSite = (VsWebSite.VSWebSite)(referencedProject.Object);

foreach(VsWebSite.WebService webService in vsWebSite.WebServices)

{

vsProject.AddWebReference(webService.URL);

}

}

else

{

vsProject.References.AddProject(referencedProject);

}

}

hope it helps.

Jose Escrich  Thursday, June 14, 2007 4:05 PM

Thanks jose for replying my question.

My problem regarding project reference is still not solved.I am not using any recipe argument to refer referencing project and referenced project.I am using Software factork toolkit

I just use custom action thatI declare atCreateSolution.xml file

<Action Type="mypackage.Actions.AddProjectReference,mypackage" Name="Addref">

And at AddProjectReference.cs file I do

***********************************************************AddProjectReference.cs***********************************

protected override void OnExecute()

{
string projectName = "Portal.Test.Business";
string refProject = "Portal.Test.Shared";
AddReference(projectName, refProject);


}

private void AddReference(string projectName, string refProject)
{
EnvDTE.DTE dte = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));
Project project = null;
Project referenceProject = null;

foreach (Project proj in dte.Solution.Projects)
{


if (proj != null)
{
if (proj.Name.Equals(projectName))
{
project = proj;
}
else if (proj.Name.Equals(refProject))
{
referenceProject = proj;
}

}
}
if (project != null && referenceProject != null)
{
VSLangProj.VSProject aVsproject = (VSLangProj.VSProject)referenceProject.Object; aVsproject.References.AddProject(project);
}
}

************************************************************************************************************************************

I have an embedded structure of solution folders ,my solution.vstemplate file is:

*****************************************Solution.vstemplate*********************************************

<TemplateContent>
<ProjectCollection>
<SolutionFolder Name="Folder1">
<SolutionFolder Name="Folder2">
<SolutionFolder Name="Folder3">
<SolutionFolder Name="Folder4">
<SolutionFolder Name="$ProjectName$">
<ProjectTemplateLink ProjectName ="Portal.$ProjectName$.Shared">Projects\Folder1\Folder2\Folder3\Folder4\TEST\Portal.TEST.Shared\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="Portal.$ProjectName$.Business">Projects\Projects\Folder1\Folder2\Folder3\Folder4\TEST\Portal.Test.Business\MyTemplate.vstemplate</ProjectTemplateLink>
</SolutionFolder>
</SolutionFolder>
</SolutionFolder>
</SolutionFolder>
</SolutionFolder>
</ProjectCollection>
</TemplateContent>

*********************************************************************************************************************************************

This structure also contains 7 more projects and solutions folders.

Now in my AddProjectReference.cs file as I describe above,the foreach loops runs only ones,i.e it loop only to folder1 and exit.Thus I am not able to get value at project and referenceproject variable.

I am to assign project and referenceproject variables with referenced project and referencing project which can only be possible with foreach (Project proj in dte.Solution.Projects).

Tell me how can I achive this.

Thanks to everyone in this forums,


Monesh  Monday, June 18, 2007 9:11 AM
When iterate through the solution Projects collection, that projects could be a Project or a SolutionFolder, so you should to check the Name and also the Kind for each (you'll get the guid solution folder in ProjectKinds.vsProjectKindSolutionFolder). Another thing is that you should implement a recursive loop in order to find out projects inside SolutionFolder.

If you're using SFT in the Library you've a SolutionModelService which is an abstraction of a solution where you can find Projects in whole solution using predicates.

List<ProjectModel> FindAllProjects(Predicate<ProjectModel> match)

here you've an example

List<ProjectModel> projectModels = FindAllProjects(new Predicate<ProjectModel>(delegate(ProjectModel project)
{
return project.Project.Name.Equals("foo");
}));

Notice that ProjectModel is an abstraction of project and it has a Project property which you can use to add the references.
Jose Escrich  Tuesday, June 19, 2007 1:26 AM

You can use google to search for other answers

Custom Search

More Threads

• CreateProjectAction, TextTemplateAction and AddItemFromStringAction are not compatible in one recipe
• Including a custom assembly with GAT
• no menu entry in VSS 08 for guidance automation packages
• Support for the June or July CTP of VS .Net 2005?
• Is there any way to run an action when a project with my guidance package enabled is opened?
• Bug in Microsoft.Practices.RecipeFramework.Library.DteHelper.IsWebProject
• Guindance automation toolkit and extensions for Vs 2008 and Vs 2005
• Synchronizing - GAT Package Changes
• Could I add repeating code with this technology in my solution in C#.net?
• The Key combination (ctrl+R, ctrl+R) for word wrap not working