Mittwoch, 21. Dezember 2011

Create xml file from string and downloading it with save dialog

I needed to create an XML programmatically and then to send it to the user to download. For this purpose I created a Page that is called from a customAction. Here is the code for the xml downloading part.
Aspx:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExportApplication.aspx.cs" Inherits="ListItemExport.Layouts.ListItemExport.ExportApplication" Debug="true" %>

Code Behind:
namespace ListItemExport.Layouts.ListItemExport
{
    public partial class ExportApplication : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e){
            base.OnLoad(e);
            String xmlString = "<xml><test>testvalue</test></xml>";

            String listId = Request.Params["listId"];
            String itemId = Request.Params["itemId"];

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(xmlString);

            MemoryStream xmlStream = new MemoryStream();
            xDoc.Save(xmlStream);
            Response.Clear();
            Response.AppendHeader("Content-Disposition","attachment; filename=test.xml");
            Response.ContentType = "text/XML";
            Response.BinaryWrite(xmlStream.ToArray());

        }
    }
}

Keine Kommentare:

Kommentar veröffentlichen