Free Chart Control. About time!

I am sure all developers in the past have attempted to create their own bar or pie chart using .NET’s System.Drawing methods. However, I never been able to match a high quality finish similar to what you would expect from third party charting software. You can see my charting attempt in my post: Outputting Custom Made Charts.

As much as I would like to use .NET charting solutions from the likes of DotNetCharting and FusionCharts, they are just too expensive to buy. I have downloaded trial versions and been thoroughly impressed with their range of charts and functionality.

But it seems I have been quite late in noticing Microsoft have released their own range of chart controls to easily create rich professional looking visual data. Having had a play around in Microsoft Charts earlier this week, I have to say I am a big fan. Its a shame I hadn’t found it earlier.

From looking around that net, there are many articles and blogs on how to use Microsoft Chart so I am not going to write another. Ha! But in the next day or two, I plan to blog on how I implemented Microsoft Chart as a web part within my Report Center in SharePoint 2007. So watch this space.

In the meantime here are some useful links I have used to get started in Microsoft Chart:

Change SharePoint 2007 My Site URL

A little while back I renamed all URL’s within my SharePoint 2007 virtual environment. You can view my posting here. When it came to viewing users My Site, the new URL did not update after making changes to the Alternate Access Mappings within Central Administration.

So if you have the same problem or if you just need to change your My Site URL, here is a tutorial on how to fix this problem:

1) Go to “Alternate Access Mappings” in SharePoint Central Administration > Operations. Click on the URL that corresponds to your My Site. This will take to you to the “Edit Internal URLs” page, allowing you to modify the My Site URL.

Alternate AccessMappings

Edit Internal Urls

2) Go to “My Site Settings” which can be found in the Shared Service you configured in Shared Services Administration. Again, enter your new My Site URL.

My Site Settings

3) Modify your IIS site host header value to match the URL your entered in Central Administration.

4) Carry out an iisreset.

Cannot View SharePoint Intranet in Internet Explorer, Firefox is fine

I created a SharePoint 2007 installation on a Development Virtual Server. The installation and configuration of SharePoint was no problem. It actually went quite smoothly compared to my previous attempts. Lucky me! I thought to myself: “Man, things can’t get better than this”.

But I then encountered a small hitch. For some reason, I could not view my intranet through Internet Explorer. The login popup box kept of appearing even though my user credentials were correct. I had no problem accessing my Intranet in Firefox. As much as I love to use Firefox (because it is such an awesome browser), some SharePoint features are restricted when a non-IE browser is used.

The first thing I did was to add my SharePoint intranet URL to my Local Intranet trusted sites in Internet Explorer settings. From looking on the Internet, this has worked for some SharePoint developers. However, this did not fix my problem.

Add to trusted sites

This confirmed that Internet Explorer is not passing my login credentials to Active Directory causing problems when it came to authentication. I started snooping around Internet Information Services and viewed the Authentication Settings: Directory Security tab > Authentication and Access Control > Edit.

I changed my authentication in IIS for all my intranet web sites: Central Administration, Main Portal and My Site. By default, the IIS Authentication methods were set to Enable Anonymous Access and Integrated Windows Authentication. I removed these options and just selected: Basic Authentication.

Authentication Methods

After you have changed these settings just carry out an iisreset.

Custom SharePoint 2007 Bulk Document Uploader

I have noticed that one of many reasons clients like to get on to the SharePoint bandwagon is to use its detailed Document Management features to control the life cycle of each individual document within their organisation.

This got me thinking. Most organisations have hundreds, if not thousands of documents they would like to move from their networked storage devices to the SharePoint 2007 platform. It would be time consuming to upload all these documents to a document library. So I decided to create a C# application that would allow me to upload multiple files from a folder on a PC to a document library web part of my choice.

Just to note, this program I created has not been tested to upload documents in their thousands. I have tested uploading over 100 documents successfully. But feel free to modify my code to work more efficiently! ;-)

SharePoint Document Uploader

As you can see from my program above, I have managed to upload numerous documents from “Z:\My Received Files” to a document library called “Shared Documents”.

SharePoint Document Library

I created my program by using the following code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using System.IO; 
 
namespace MOSSDocumentLibraryUploader
{
    public partial class frmHome : Form
    {
        public frmHome()
        {
            InitializeComponent();
        }
  
        private void frmHome_Load(object sender, EventArgs e)
        {
            ddlDocumentLibList.Enabled = false;
            ddlSubSites.Enabled = false;
        }
 
        private void btnStartUpload_Click(object sender, EventArgs e)
        {
            lstUploadedDocuments.Items.Add(String.Format("Upload operation started at {0} {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
 
            //Start uploading files
            try
            {
                //Get site collection, website and document library informationn
                SPSite intranetSite = new SPSite(txtIntranetUrl.Text);
                SPWeb intranetWeb = intranetSite.AllWebs[ddlSubSites.SelectedItem.ToString()];
                SPList documentLibrary = intranetWeb.Lists[ddlDocumentLibList.SelectedItem.ToString()];
 
                intranetWeb.AllowUnsafeUpdates = true;
                intranetWeb.Lists.IncludeRootFolder = true;
 
                //Start iterating through all files in you local directory
                string[] fileEntries = Directory.GetFiles(txtDocumentDirectory.Text);
 
                foreach (string filePath in fileEntries)
                {
                    SPFile file;
 
                    //Get file information
                    FileInfo fInfo = new FileInfo(filePath);
 
                    Stream fileStream = new FileStream(filePath, FileMode.Open);
 
                    //Load contents into a byte array
                    Byte[] contents = new Byte[fInfo.Length];
 
                    fileStream.Read(contents, 0, (int)fInfo.Length);
                    fileStream.Close();
 
                    //Upload file to SharePoint Document library
                    file = intranetWeb.Files.Add(String.Format("{0}/{1}/{2}", intranetWeb.Url, documentLibrary.Title, fInfo.Name), contents);
                    file.Update();
 
                    lstUploadedDocuments.Items.Add(String.Format("Successfully uploaded: {0}", fInfo.Name));
 
                    lstUploadedDocuments.Refresh();
                }
 
                //Perform clean up
                intranetWeb.Dispose();
                intranetSite.Dispose();
 
                lstUploadedDocuments.Items.Add(String.Format("Operation completed at {0}", DateTime.Now.ToLongTimeString()));
            }
            catch (Exception ex)
            {
                lstUploadedDocuments.Items.Add(String.Format("Error: {0}", ex));
            }
        }
 
        private void btnCancelUpload_Click(object sender, EventArgs e)
        {
            lstUploadedDocuments.Items.Add(String.Format("Upload operation cancelled at {0} {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
        }
 
        private void txtDocumentDirectory_Validating(object sender, CancelEventArgs e)
        {
            if (((TextBox)sender).Text == "")
            {
                MessageBox.Show("You must enter an upload directory.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (!Directory.Exists(((TextBox)sender).Text))
            {
                MessageBox.Show("Document upload directory does not exist.", "Directory Does Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void txtIntranetUrl_Validating(object sender, CancelEventArgs e)
        {
            if (((TextBox)sender).Text == "")
            {
                MessageBox.Show("You must enter a intranet url.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
 
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
        private void ddlSubSites_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ddlDocumentLibList.Enabled = true;
 
                //Get site collection, website and document library informationn
                SPSite intranetSite = new SPSite(txtIntranetUrl.Text);
                SPWeb intranetWeb = intranetSite.AllWebs[ddlSubSites.SelectedItem.ToString()];
 
                intranetWeb.Lists.IncludeRootFolder = true;
 
                //Iterate through all document libraries and populate ddlDocumentLibList
                foreach (SPList docList in intranetWeb.Lists)
                {
                    ddlDocumentLibList.Items.Add(docList.Title);
                }
            }
            catch
            {
                ddlDocumentLibList.Enabled = false;
            }
        }
 
        private void txtIntranetUrl_TextChanged(object sender, EventArgs e)
        {
            try
            {
                ddlSubSites.Enabled = true;
                ddlDocumentLibList.Enabled = true;
 
                //Get site collection, website and document library information
                SPSite intranetSite = new SPSite(txtIntranetUrl.Text);
 
                //Iterate through all sites and propulate ddlSubSites
                foreach (SPWeb web in intranetSite.AllWebs)
                {
                    ddlSubSites.Items.Add(web.Url.Replace(txtIntranetUrl.Text, ""));
 
                    //Iterate through child sites
                    foreach (SPWeb childSite in web.Webs)
                    {
                        ddlSubSites.Items.Add(childSite.Url.Replace(txtIntranetUrl.Text, ""));
                    }
                }
            }
            catch
            {
                ddlSubSites.Enabled = false;
                ddlDocumentLibList.Enabled = false;
            }
        }
    }
}

It would have been really cool if my program only listed Document Libraries instead of all lists within a portal site. Unfortunately, I could not find any code to get a list of type Document Library. If anyone knows how to do this, I would be grateful if you could post some code.

If you have any questions on the code or know of a better (free) solution out there, please leave a comment.

DebuggerHidden Attribute and Other Cool Debugging

Debugging a page that uses many methods from other classes can become a right pain in the neck. I find myself accidentally stepping into a method that I don’t need to debug or wanting to output specific values from my methods straight away. Being the cowboy (correction, agile) developer that I am, one of my code boffins at work showed me a two cool ways to meet my debugging needs:

1) DubuggerHidden Attribute

Using the DubuggerHidden attribute tells the Visual Studio debugger that the method is hidden from the debugging process. The simple example below, shows the DebuggerHidden attribute in use:

protected void btnDoSomething_Click(object sender, EventArgs e)
{    
    //Output random number to Textbox
    txtOutput.Text = GetNumber(1, 10).ToString();
} 
 
[DebuggerHidden]
int GetNumber(int min, int max)
{    
    System.Random random = new Random();
    return random.Next(min, max);
}

2) DebuggerDisplay Attrubute

The DebuggerDisplay attribute allows us to output variable values from a class or method to be displayed in our Visual Studio debugger. The attribute includes a single argument that is supplied as a string. The string can contain references to fields, properties and methods in the class so that the actual values from an object may be included in its description.

[DebuggerDisplay("a={a}, b={b}, ValueTotal={CalculateValues()}")]
public class AddValues
{    
    public int a { get; set; }
    public int b { get; set; }
    
    public int CalculateValues()
    {
        return a + b;
    }
}

You also have the ability to include simple expressions, like so:

[DebuggerDisplay("a={a}, b={b}, ValueTotal={CalculateValues() * 2}")]

In addition to the “DebuggerHidden” and DebuggerDisplay attributes, .NET contains some very useful attributes that modify the behaviour of a debugger. For me, they weren’t as interesting as the two debugger attributes I listed above. :-)