Aidan Garnish
MOSS 2007 and other Microsoft technologies

Generic CSV or SQL data importer for SharePoint list

September 9, 2008 17:01 by Aidan

I finally got bored of rewriting the same console application to move data from a csv or a sql table into a SharePoint list! The result is a generic console application that is capable of importing either a CSV file or some SQL data from a stored procedure. The app uses the object model so it needs to be run on the SharePoint server farm. I may get round to rewriting it using web services at some point. The CSV import parses the file so that things like line breaks and commas within fields are handled nicely. Any blank fields in the CSV do need to be filled using find and replace on blank space otherwise data ends up in the wrong columns.

To import a CSV the first row of the of the CSV file is used to define which columns the data will import to. Eg. A CSV that has one column with a header of Title will import the data into the Title column of the specified list. Modify the App.Config file to include the URL of your site collection, the list name, the path to the CSV and whether or not you want to delete all items in the list before importing.

To import SQL data create a stored procedure that returns the data as the names of the site columns

Eg. Select Name AS Title from tblPerson - will return one column of data that will import into the Title column of a list. Modify the App.Config to include the name of the stored procedure and the SQL connection string and run the app.

You can download the project from the following link:

AG.GenericSharePointListImporter.zip (41.72 kb)

This application is supplied as is and offers no guarantees feel free to use as you wish all I ask is that if you make any improvements then you share them in the comments or by email.

Technorati Profile

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Web part custom property - dropdown list

December 19, 2007 09:40 by aidan

It isn't immediately obvious how to include a drop down list as a custom property on a web part. To do this you simply use an enum.

Eg.

//create enum

public enum ProductByEnum

{Type = 0,

Brand,

Chemistry};

//create get/set for the property

protected ProductByEnum productsBy;

[Personalizable(PersonalizationScope.User),WebBrowsable, WebDisplayName("Products By"),WebDescription("Use this property to change web part grouping")]public ProductByEnum ProductsBy

{

get { return productsBy; }

set { productsBy = value; }

}

A dropdown list will now appear in the miscellaneous section of your web part custom properties containing the enum values.


Currently rated 3.7 by 3 people

  • Currently 3.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Creating XHTML compliant ASP.Net server controls

December 12, 2007 09:38 by aidan

ASP.Net is great but many of the controls use table tags when they render. I am currently in the process of developing some Internet sites using MOSS 2007 which need to meet accessibility guidelines so these controls, in their out of the box state, are no use to me. We have tried applying the CSS adapters that get rid of the tables but this also had the effect of removing tables from the site settings screens making them difficult to use.

The solution we are going for is to create new controls that inherit from the base control. The new control then overrides the render event to remove the non-compliant tags and replaces them with compliant ones.

We are also using this method to add script to the master pages which removes table tags from web part zones etc.

DISCLAIMER - I have created a non-tabular control for the SPGridView. You could argue that the SPGridView presents tabular data so does not need any changes to the rendered HTML, you would probably be right but this example shows what can be done. :-)

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.IO;

namespace XHTMLCompliantMOSSControls
{
    public class CompliantSPGridView : SPGridView
    {
        protected override void RenderContents(HtmlTextWriter output)
        {
            //get the rendered HTML
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            base.RenderContents(hw);
 
            //remove tables
            string str = sb.ToString();
          
            str = Regex.Replace(str, "<table[^>]*>", "<div class=\"mainSPGridView\">");
            str = Regex.Replace(str, "<tr>", "<div class=\"normalSPGridView\">");
            str = Regex.Replace(str, "<tr class=\"ms-alternating\">", "<div class=\"alternatingSPGridView\">");
            str = Regex.Replace(str, "</tr>", "</div>");
            str = Regex.Replace(str, "<td[^>]*>", "<div class=\"itemSPGridView\">");
            str = Regex.Replace(str, "</td>", "</div>");
            str = Regex.Replace(str, "<tr class=\"ms-viewheadertr\">", "<div class=\"headerSPGridView\">");
            str = Regex.Replace(str, "<th class=\"ms-vh2-nofilter ms-vh2-gridview\" scope=\"col\">", "<div class=\"itemSPGridView\">");
            str = Regex.Replace(str, "</th>", "</div>");
          
            output.Write(str);
        }
    }
}

If you then apply a bit of CSS using the classes that have been added it is possible to get it looking just like the tabular SPGridView.

This approach could be used with any server control to make it compliant with XHTML.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Excellent post about making SharePoint xhtml compliant

December 10, 2007 15:02 by aidan
For anyone using the web content management components of MOSS 2007 to produce Internet sites this is essential reading. SharePoint uses a lot of ASP.Net controls which render using tables and non compliant html. To get rid of this there are a few things you can do which are explained in this blog post http://zac.provoke.co.nz/archive/2007/04/19/guide-to-making-sharepoint-xhtml-compliant.aspx

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

An efficient way to set web control values

December 10, 2007 11:11 by aidan

Rather than setting the widths of label and textbox controls individually like this:

oLabel1.Width=150;

oLabel2.Width=150;

oLabel3.Width=150;

It is much more efficient to add the controls to the controls collection and use:

foreach(Control oControl in this.Controls)
{
     if (oControl.GetType().ToString() == "System.Web.UI.WebControls.Label")
     {
       Label oLabel = (Label)oControl;
       oLabel.Width = 120;
     }
}

This then allows you to change the widths of all your labels within the page or web part in one place. This approach can also be taken with other controls like text boxes and drop downs or could be used to set other properties such as font or color.

Or you could just use CSS.  Smile


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: ASP.Net
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Setting custom properties on web parts

December 3, 2007 10:49 by aidan

When creating web parts it is often useful to allow users to supply some additional parameters to the web part. This can be done by adding custom attributes which can then be set by the user and used in your code.

To do this use the following syntax:

   [Personalizable(PersonalizationScope.User),
         WebBrowsable, WebDisplayName("Products List"),
         WebDescription("Use this property to change list used to retrive products from")]
        public string ProductList
        {
            get { return strProductsList; }
            set { strProductsList = value; }
        }

In this case, rather than hard coding the name of the products list, the user is able to specify which list to use.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

ILMerge to the rescue

November 8, 2007 11:57 by aidan

I recently needed to add a reference to a web part where the dll had not been strongly named. The result of doing this was a build error complaining about the unsigned dll. I did not have access to the source code so couldn't resolve the issue by rebuilding the dll with a strong name. Instead I used the ILMerge tool which can be found here.

Rather than using the tool for it's intended use of to combining a number of dlls into a single dll I used it to sign my single dll using the following command line syntax:

ILMerge.exe /out:c:\signed.dll pathToFile\unsigned.dll /keyfile:pathToFile\aidangarnish.snk


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Accessing Outlook using C#

November 6, 2007 10:49 by aidan

I have been writing some code that retrieves unread Outlook messages and displays today's appointments to a user. This KB article has it covered: http://support.microsoft.com/kb/310265


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , ,
Categories: ASP.Net
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed