SharePoint Internal Column Names

I needed to create a web part that simply output all information from an Announcements list. Sounds simple doesn’t it? But I came across a compiler error when using the “Created By” and “Modified By” columns. The error was telling me that the columns I am trying to read in my code do not exist.

After some Googling, I found out that SharePoint has something called “Internal Field Names”, whereby the display name of a column isn’t the actual column name. The full list of internal field names can be found at Michael Yeager's MSDN Blog.

If ever in doubt, you can find out the internal field names within SharePoint, by carrying out the following:

1) Go to your list and select “List Settings” from the ribbon.

2) Navigate to the where the list of columns are displayed and right click on the column in question. Select “Properties”.

SP2010 Hidden Field Right Click

3) When the link property window opens, scroll to the end of the “Address: (URL)” section. You will find a query string parameter called “Field”. This query string parameter contains the real field name.

SP2010 Hidden Field Properties

Confusing isn’t it? Well I guess it wouldn’t be SharePoint if the most simplest task failed to confuse a developer.

Using Microsoft Chart In SharePoint 2007

As promised, I have been busy creating a custom web part to display Microsoft Charts within my SharePoint reporting dashboard. Before you even think of wanting to use Microsoft Chart in a SharePoint environment, there are a few boring tasks you need to carry out beforehand:

Step 1: Copy Charting Files To SharePoint

  1. Download Microsoft Chart here.
     
  2. Ensure the System.Web.DataVisualization.dll is in the GAC of your SharePoint server. If not, go to your Microsoft Chart installation directory (C:\Program Files\Microsoft Chart Controls\Assemblies\). You will find all the dll’s here.
     
  3. Copy the charting images from the MS Chart project directory.

    Image Directory
  4. On your SharePoint server, go to the 12 hive and create a new folder called “MicrosoftChartControls” in the Images directory.

    SharePoint Image Directory

Step 2: Modify SharePoint’s Web.Config File

  1. Add “System.Web.DataVisualization” to the SafeControls section.
    <SafeControls>
        ...
        ... 
        <SafeControl Assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  Namespace="System.Web.UI.DataVisualization.Charting" TypeName="*" Safe="True" AllowRemoteDesigner="True"/>
    </SafeControls>
  2. Add the “ChartImg.axd” path to httpHandlers section.
    <httpHandlers>
        ...
        ...
        <add verb="*" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
    </httpHandlers>
  3. Add a new AppSetting after the </system.workflow.componentmodel.workflowcompiler> tag. As you can see, this AppSetting contains the URL to where our charting images are stored.
    </System.Workflow.ComponentModel.WorkflowCompiler>
    <appSettings>
        <add key="ChartImageHandler" value="storage=memory;timeout=20;URL=/_layouts/Images/MicrosoftChartControls/" />
    </appSettings>
  4. Lastly, ensure you add your own custom web part charting control to the SafeControls section as I have done.
    <SafeControl Assembly="MOSSCharts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d463a6b3aa294272" Namespace="MOSSCharts" TypeName="*" Safe="True" AllowRemoteDesigner="True" />

If you still have problems with SharePoint failing to recognise the charting API, view this MSDN Forum posting.

My Custom Chart Example – Bar Chart

I decided it would be more useful to create a charting web part that would display graphical data from a database, instead of using randomised or hard coded values. My charting web part contains the following properties:

  • Chart Title
  • Database Connection String – in the real world you wouldn’t include this property. But it just makes it so much more easier to change database connection strings.
  • Series 1 Query – will contain a SQL Query based on the results you want returned.
  • Series 1 X Data – the column data you want displayed in the Bar Chart X Axis.
  • Series 1 Y Data - the column data you want displayed in the Bar Chart Y Axis.

Chart Properties

Based on the properties above, your chart will look something like this:

Custom Chart

This is how you can create the Bar Chart web part:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Data;
using System.Data.Sql;
using Microsoft.SharePoint;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.ComponentModel;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
 
namespace MOSSCharts
{
    public class BarChart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private string chartName;
        [WebBrowsable(true), Personalizable(true)]
        [Category("Chart Details")]
        [FriendlyName("Chart Title")]
        public string ChartName
        {
            get { return chartName; }
            set { chartName = value; }
        }
 
        private string connectionString;
        [WebBrowsable(true), Personalizable(true)]
        [Category("Database Details")]
        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }
 
        private string querySeries1;
        [WebBrowsable(true), Personalizable(true)]
        [Category("Series 1 Data")]
        [FriendlyName("Series 1 Query")]
        public string QuerySeries1
        {
            get { return querySeries1; }
            set { querySeries1 = value; }
        }
 
        private string xColumnDataSeries1;
        [WebBrowsable(true), Personalizable(true)]
        [Category("Series 1 Data")]
        [FriendlyName("Series 1 X Data")]
        public string XColumnDataSeries1
        {
            get { return xColumnDataSeries1; }
            set { xColumnDataSeries1 = value; }
        }
 
        private string yColumnDataSeries1;
        [WebBrowsable(true), Personalizable(true)]
        [Category("Series 1 Data")]
        [FriendlyName("Series 1 Y Data")]
        public string YColumnDataSeries1
        {
            get { return yColumnDataSeries1; }
            set { yColumnDataSeries1 = value; }
        }
 
        protected override void CreateChildControls()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    //**************************************************************
                    //Create Connection String
                    //**************************************************************
                    SqlConnection sqlConn = new SqlConnection(ConnectionString);
 
                    //**************************************************************
                    // Create Chart Control
                    //**************************************************************
                    Chart barChart = new Chart();
                    barChart.Width = 412;
                    barChart.Height = 296;
 
                    //Specify palatte to use
                    barChart.Palette = ChartColorPalette.BrightPastel;
 
                    if (!String.IsNullOrEmpty(ChartName))
                    {
                        Title t = new Title(ChartName, Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
                        barChart.Titles.Add(t);
                    }
 
                    //Create chart area
                    barChart.ChartAreas.Add("ChartArea1");
 
                    // Show as 3D
                    barChart.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
 
                    // Show columns as clustered
                    barChart.ChartAreas["ChartArea1"].Area3DStyle.IsClustered = false;
 
                    // Show X axis end labels
                    barChart.ChartAreas["ChartArea1"].AxisX.LabelStyle.IsEndLabelVisible = true;
 
                    // Set rotation angles
                    barChart.ChartAreas["ChartArea1"].Area3DStyle.Inclination = 15;
                    barChart.ChartAreas["ChartArea1"].Area3DStyle.Rotation = 20;
                     
                    //**************************************************************
                    // Add series data to chart 
                    //**************************************************************
 
                    //Create Series 1 if there is data
                    if (!String.IsNullOrEmpty(XColumnDataSeries1) && !String.IsNullOrEmpty(YColumnDataSeries1) && !String.IsNullOrEmpty(QuerySeries1))
                    {
                        //Add a new series
                        barChart.Series.Add("Series1");
 
                        // Set series chart type
                        barChart.Series["Series1"].ChartType = SeriesChartType.Column;
 
                        // Draw as 3D Cylinder
                        barChart.Series["Series1"]["DrawingStyle"] = "Cylinder";
 
                        barChart.Series["Series1"].BorderColor = Color.FromArgb(26, 59, 105);
                        barChart.Series["Series1"].Color = Color.CornflowerBlue;
                        barChart.Series["Series1"].BackSecondaryColor = Color.Navy;
                        barChart.Series["Series1"].BackGradientStyle = GradientStyle.DiagonalLeft;

                        barChart.Series["Series1"]["PointWidth"] = "0.8";
 
                        SqlCommand sqlCmdSeries1 = new SqlCommand(QuerySeries1, sqlConn);
 
                        barChart.DataSource = sqlCmdSeries1;
 
                        barChart.Series["Series1"].XValueMember = XColumnDataSeries1;
                        barChart.Series["Series1"].YValueMembers = YColumnDataSeries1;

                    }
 
                    barChart.DataBind();
 
                    Controls.Add(barChart);
                }
                catch (Exception ex)
                {
                    Label lblError = new Label();
                    lblError.Text = "An error has occurred. Please ensure you have entered correct chart details";
 
                    Controls.Add(lblError);
                }
            });
        }
    }
}

The code (above) provides a basis to display a database driven chart within SharePoint. With some further tweaks, your Reporting Dashboard could look something like this:

SharePoint Report Dashboard

If you would like the code for some of my other charts displayed within my Reporting Dashboard, please leave a comment.

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.