views:

23

answers:

1

Hi, I am generating entity wrappers in VS2010 based on dynamic objects in a CRM system. In addition to the entity code, I want to add an EntityBase of which all entities inherit from. If the file exists in the project from before, it should not be added. I am using an IWizard implementation to give the generator the object names etc.

Is it possible in the IWizard implementation to determine whether or not to add an item if it exists in the project from before? How do I get a hold of the project handle and its items in or before the ShouldAddProjectItem method?

My code so far (not completed):

public class EntityWizardImplementation : IWizard
{
    public void BeforeOpeningFile(ProjectItem projectItem)
    {
        //Note: Nothing here.
    }

    public void ProjectFinishedGenerating(Project project)
    {
        //Note: Nothing here.
    }

    public void ProjectItemFinishedGenerating(ProjectItem projectItem)
    {
        //Note: Nothing here.
    }

    public void RunFinished()
    {
        //Note: Nothing here.
    }

    public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
    {
       try
        {
            var window = new WizardWindow();

            // Replace parameters gathered from the wizard
            replacementsDictionary.Add("$crmEntity$", window.CrmEntity);
            replacementsDictionary.Add("$crmOrganization$", window.CrmOrganization);
            replacementsDictionary.Add("$crmMetadataServiceUrl$", window.CrmMetadataUrl);

            window.Close();
        }
        catch (SoapException se)
        {
            MessageBox.Show(se.ToString());
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

    public bool ShouldAddProjectItem(string filePath)
    {
        // This is where I assume it is correct to handle the preexisting file.
        return true;
    }
}
+1  A: 

The automationObject in the RunStarted method represents the Visual Studio environment or context. It is castable to a DTE object, and you can access the solution, the projects etc. from the object. This is true provided that you launch this as an item template or project template wizard and not programatically. In that case, accessing the object will most likely fail.

public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
    if (automationObject is DTE)
    {
        DTE dte = (DTE)automationObject;
        Array activeProjects = (Array)dte.ActiveSolutionProjects;

        if (activeProjects.Length > 0)
        {
            Project activeProj = (Project)activeProjects.GetValue(0);

            foreach (ProjectItem pi in activeProj.ProjectItems)
            {
                // Do something for the project items like filename checks etc.
            }
        }
    }
}
Henrik