locked
itemtemplate to add item under a new folder RRS feed

  • Question

  • HI,

    Is there a way to add the item under a new directory. For example, mproject, if I add a new Item (from my itemteplate) to this project with name "myfile.fileext", then the item should be added under project as myproject\myFile\myfile.fileext. Here  a  new directory  was created as myfile(directory name is same as the file name entered in add item dialog) and then the new item was created under that directory.

     


    singhhome
    Monday, December 27, 2010 6:44 PM

Answers

  • Using <folder> didn't worked.

     I  used <projectitem targetFileName="$fileinputname$\$fileInputname$" to add item inside the folder.

    Regards


    singhhome
    • Marked as answer by singhhome Thursday, January 6, 2011 5:30 PM
    Thursday, January 6, 2011 5:30 PM

All replies

  • Hi Singhhome,

     

    Thanks for your post.

    I suppose the situation you expected is as below:

    If you add a item named "a.ext" then its directory would be myproject\a\a.ext,

    then if its name is "b.ext", the directory specified as myproject\b\b.ext

    If so, I think you can make it by create a custom wizard which implement IWizard

    you can generate the path by custom input and add a folder with

    ProjectItems.AddFolder Method (EnvDTE) 

    Then get the folder and add the project item as:

    DTE.ActiveWindow.Object.GetItem("ProjectName\FolderName").Select(vsUISelectionType.vsUISelectionTypeSelect)
            DTE.ItemOperations.AddNewItem("Project Items\ProjectItem")

    Hope my reply helps resolve your issue.

     

    Best Regards,

    Ziwei Chen

     

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

     

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 28, 2010 7:19 AM
  • HI Victor,

    I am not able to understand how to do it, can you kindly provide the answer in more details?

    1. In which method of Iwizard i should write this code

    2.The item(from item template) can be added at any level and not necessary from Project only. For example, if in Solution explorer user selects "Project node" and click add Item; then structure will be like project\a\a.ext. But if Project has some folder, say Folder1 and user selects that folder in Solution explorer and add item then structure will be Project\FOlder1\a\a.ext Basically folder has to be created under the Item which user selected in solution explorer

    3. can you kindly elaborate(more code) your last step for getting the folder and adding project item.

    Regards


    singhhome
    Tuesday, December 28, 2010 3:51 PM
  • Hi Singhhome,

     

    Thanks for your post.

    I think in the RunStarted method of IWizard, you can get the file input name in replacementsDictionary with the item name parameter specified in .vstemplate file.

    and add folder in RunStarted method:

    automationObject.SelectedItems.Item(1).Project.ProjectItems.AddFolder("Item Name");

    Then select the new folder and create a new item:

    automationObject.ActiveWindow.Object.GetItem("SolutionName\ProjectName\FolderName").Select(vsUISelectionType.vsUISelectionTypeSelect)

    automationObject.SelectedItems.Item(1).Project.ProjectItems.AddFromTemplate("Item Template Path")

    If anything is unclear, be free to let me know.

    Best Regards,

    Ziwei Chen

     

     

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

     

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 29, 2010 8:29 AM
  • HI,

    I am not able to try it as 

    automationObject.SelectedItems.Item(1).Project is coming as null for me. Not sure what wrong i am doing.

    will this approach not add the item twice? First for the user action of adding item(for which IWizard is executing) and second time by the programmatic add I will be doing in IWizard, ProjectItems.AddFromTemplate("Item Template Path")

    Regards


    singhhome
    Thursday, December 30, 2010 3:43 PM
  • Hi Singhhome,

     

    Thanks for your feedback.

    I think you need to cast automationObject to DTE first.

    DTE dte = (DTE)automationObject;

    Then add new folder and select the folder as before but in RunFinished Method:

    dte.SelectedItems.Item(1).Project.ProjectItems.AddFolder("Item Name");

    Then select the new folder and create a new item:

    dte.ActiveWindow.Object.GetItem("SolutionName\ProjectName\FolderName").Select(vsUISelectionType.vsUISelectionTypeSelect)

     

    Then cut the item into the new folder with

     

    dte.SelectedItems.Item(1).Project.ProjectItems.AddFromFile("SolutionName\ProjectName\FileName")

    Hope it helps resolve your issue.

     

    Best Regards,

    Ziwei Chen

     

     

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

     

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 31, 2010 5:58 AM
  • thanks for your reply, but somehow i am not able to get it working!! may be I am not able to express my question clearly.

    this is what i understood and what i am doing.

    1. dteInstance.SelecteItems.Item(1).Project is available only when, in solution explorer I click on Project Node and then add new Item. But if my project has some folder and user select Folder and then right click and then select "add new Item". dteInstance.SelecteItems.Item(1).Project is null. My requriement is that user can select any node in project and then add new Item.

    2. It always add file for the action that user triggered. I want that VS should not create that file, it should create that file under the new folder that i want to create.

    Probable I can fix this by deleting that file in ProjectItemFInishedGenerating with projectItem.Delete(). But I don't want VS to even add that file at first place. I want VS to treat/see it that it is actually adding the file under the new folder, so that it appropriately updates the namespace. If I move file to new folder after it has been added then the namespace will not contain the new folder that i have created.

    3. I have just implemented Iwizard.RunFinished method. I haven't implemeneted other methods of interface.

    2. in runfinished

       ProjectItem newFolder = dteInstance.SelectedItems.Item(1).Project.ProjectItems.AddFolder(featureFileName);
                dteInstance.ActiveWindow.Object.GetItem(newFolder.Name).Select(vsUISelectionType.vsUISelectionTypeSelect);

                string projectName = newFolder.ContainingProject.Name;
                string solutinoName = dteInstance.Solution.FullName;

            dteInstance.SelectedItems.Item(1).Project.ProjectItems.AddFromFile(String.Format(@"{0}\{1}\{2}", solutinoName, projectName, featureFileName));

    3. Now when I add item, the second line throws an exception argumentException. Value doesn't fall within expected range.

    4. I know 5th line is wrong. i could not get when you said "cut the item into new folder with". How do i get  parameter value for AddFromFIle(). It can be solution\project\someFolder\someanotherFolder\fileName or any other hierarchy.

    Regards


    singhhome
    Friday, December 31, 2010 8:13 PM
  • Hi Singhhome,

     

    Thanks for your feedback.

    I'm trying to consult this issue from our experts and will come back as soon as possible.

    Thanks for your patience.

     

    Best Regards,

    Ziwei Chen

     

     

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

     

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 3, 2011 9:06 AM
  • Hi singhhome,

     

    I’m very new to all this, and I am not sure if this is your exact answer, but it should be enough to get your directory working.

     

    Inside the zip file that actually has the files to be installed by the template (which is the same file that has my IWizard dll in it), I have the following code inside the <TemplateContent> element.

    By using the <Folder> element, the folder is created, and the files listed are populated inside that folder.

     

    <Folder Name="ReferenceResources">

          <ProjectItem ReplaceParameters="true" SubType="Code" TargetFileName="ReferenceResources.xaml">ReferenceResources.xaml</ProjectItem>

          <ProjectItem ReplaceParameters="true" SubType="Code" TargetFileName="$fileinputname$ReferenceStyle.xaml">DockablePaneReferenceStyle.xaml</ProjectItem>

        </Folder>

     

    Hope this helps,

    Jim Tomasko


    Jim Tomasko
    Monday, January 3, 2011 6:57 PM
  • Hi Jim,

     

    Thanks for sharing.

    I didn't know the "Folder" element, if change the folder's name to a parameter, that can be a good solution.

     

    Best Regards,

    Ziwei Chen

     

     

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

     

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 4, 2011 2:55 AM
  • As per http://www.visualstudiodev.com/visual-studio-extensibility/how-to-add-folder-using-item-template-18840.shtml,

    folder will not substitite parameter name. I can't fix/hardcode my folder name, i need that folder name should be same as file name. post also suggest to use IWizard to achieve it.

    Anyway I will still try that option, as that post is very old, and probably it might have been fixed in VS 2010.

    Regards.


    singhhome
    Tuesday, January 4, 2011 4:59 AM
  • Using <folder> didn't worked.

     I  used <projectitem targetFileName="$fileinputname$\$fileInputname$" to add item inside the folder.

    Regards


    singhhome
    • Marked as answer by singhhome Thursday, January 6, 2011 5:30 PM
    Thursday, January 6, 2011 5:30 PM
  • Hi Victor,

    Glad to give back when I can.

    The only way I even knew about <Folder> was by searching a variety of other samples from other people.  I think this clearly indicates the documentation needs both more samples, and more meat.

    Happy new year,

    Jim Tomasko

     

    Thursday, January 6, 2011 7:27 PM
  • Thanks for sharing, I'm glad it works now.

     

    Best Regards,

    Ziwei Chen

     

     

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

     

     

     

     

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 7, 2011 2:55 AM