Creating new files – Extending Visual Studio SharePoint development tools tip #5


It’s probably one of the most obvious types of extensibility – to generate files out of content existing in a SharePoint Site. There are many examples already there available either in SharePoint Designer or the Visual Studio SharePoint development tools. Still there are many things yet to be created. Knowing how to programmatically create a new file in Visual Studio, is the basic thing that you need to know how to do, before you get started with your own extensions.

References

In your project you need the following assembly references:

  • EnvDTE (Embed Interop Types: True)
  • EnvDTE80 (Embed Interop Types: True)
  • Microsoft.VisualStudio.Shell.10.0
  • Microsoft.VisualStudio.Shell.Interop
  • Microsoft.VisualStudio.Shell.Interop.10.0 (Embed Interop Types: True)
  • Microsoft.VisualStudio.Shell.Interop.8.0
  • Microsoft.VisualStudio.Shell.Interop.9.0
  • Microsoft.VisualStudio.OLE.Interop

You can also paste the following snippet into your project file:

<Reference Include="EnvDTE">
  <EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="EnvDTE80">
  <EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.VisualStudio.OLE.Interop" />
<Reference Include="Microsoft.VisualStudio.Shell.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop.10.0" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop.8.0" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop.9.0" />

Code

/// <summary>
/// Adds new file to the current Solution/Project and inserts the contents
/// </summary>
/// <param name="fileType">File type, eg. General\XML File</param>
/// <param name="title">File title</param>
/// <param name="fileContents">File contents</param>
internal static void CreateNewFile(string fileType, string title, string fileContents)
{
    DTE2 dte = Package.GetGlobalService(typeof(SDTE)) as DTE2;
    Document file = dte.ItemOperations.NewFile(fileType, title).Document;
    if (!String.IsNullOrEmpty(fileContents))
    {
        TextSelection selection = file.Selection;
        selection.SelectAll();
        selection.Text = "";
        selection.Insert(fileContents);
    }
}

/// <summary>
/// Creates new text file with the given title and contents
/// </summary>
/// <param name="title">File title</param>
/// <param name="fileContents">File contents</param>
internal static void CreateNewTextFile(string title, string fileContents)
{
    CreateNewFile(@"General\Text File", title, fileContents);
}

The code consists of two methods: CreateNewFile and CreateNewTextFile. The CreateNewTextFile method is the simplified version of the CreateNewFile method which is sufficient for the most scenarios. Because you’re defining the file name (including the extension) and the contents of the file the file type used for creating the new file doesn’t matter. However, if you want to create a new file using a specific Project Item Template, you can use the CreateNewFile method and pass the type of the Project Item as the first parameter.

Important Please note, that the above example doesn’t add the file to the active project. What it does is, that it simply creates a new file in Visual Studio that has to be saved and added to the project.

Example

[Export(typeof(ISharePointProjectExtension))]
public class CreateNewFileProjectExtension : ISharePointProjectExtension
{
    public void Initialize(ISharePointProjectService projectService)
    {
        projectService.ProjectMenuItemsRequested += new EventHandler<SharePointProjectMenuItemsRequestedEventArgs>(projectService_ProjectMenuItemsRequested);
    }

    void projectService_ProjectMenuItemsRequested(object sender, SharePointProjectMenuItemsRequestedEventArgs e)
    {
        e.ActionMenuItems.Add("Create file").Click += new EventHandler<MenuItemEventArgs>(CreateNewFileProjectExtension_Click);
    }

    void CreateNewFileProjectExtension_Click(object sender, MenuItemEventArgs e)
    {
        CreateNewTextFile("MyFile.txt", "Lorem ipsum");
    }
}

The above code snippet adds a new menu item to the SharePoint Project. After clicking the menu, a new file called MyFile.txt is created. The created file contains the Lorem ipsum content.

Technorati Tags: SharePoint 2010,Visual Studio 2010

Others found also helpful: