Thursday, January 18, 2018

Export a Sharepoint list to a csv using C#.

Below is the code to export a Sharepoint List to a CSV file using C#.NET

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;

namespace export_sharepoint
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "<Site URL>" ;

            using (SPSite oSite = new SPSite(url))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    SPList oList = oWeb.Lists["<List Name>" ];

                    foreach (SPListItem oItem in oList.Items)
                    {
                         FileStream fs = new FileStream ( "C:\\Documents and Settings\\administrator.domain\\Desktop\\sites.txt", FileMode.Append, FileAccess.Write, FileShare .Read);
                         StreamWriter writer = new StreamWriter(fs);
                        // Access each item in the list... 
                        writer.Write(oItem[ "Site ID"]);
                        writer.Write( ",");
                        writer.Write(oItem[ "Column Name"]);
                        writer.Write( ",");

                        writer.Close();
                        fs.Close();
                    }

                }
            } 
        }
    }

No comments:

Post a Comment

Thank you for your comment.