Thursday, February 26, 2009

Recreating a Column in SharePoint's User List

I did something really stupid. I wanted to customize the columns in the User Information List and decided that I did not need the Department column. Normally you can't delete the Department column, but a quick Google search pointed me to a tool called SharePoint Manager 2007(SPM). Using SPM, I was able to change the flag that was preventing me from deleting the Department column. Less than two minutes after deleting the Department column I started getting complaints about errors when trying to edit list items. It turns out that you really can't delete the Department column. What made this mistake even more stupid was that I did not make a backup of the site before playing with the User Information List.

The first thing I tried was to recreate the column with the web interface. Even if you name the column "Department", there is something different and you are still left with a broken site. After weeks of digging through Google and analyzing the SharePoint database, I was finally able to discover a method to create the original Department column and fix my errors. What made my fix possible were the export, import and update field tools created by Gary Lapointe.

Here are the steps I followed:
  1. Create new site collection with a new top level site. This creates a new User Information List with the Department column.
  2. Run gl-exportlistfield on the new site to get the XML for the department column. The command I used was:
    stsadm -o gl-exportlistfield -url "http://sharepoint:1234/_catalogs/users/detail.aspx" -fielddisplayname "Department" -outputfile "c:\backups\usersList"

  3. Run gl-importlistfield on the broken site to recreate the department column. The command I used was:
    stsadm -o gl-importlistfield -url "http://sharepoint/_catalogs/users/detail.aspx" -inputfile "c:\backups\usersList"
After running those two commands, the Department column was restored and my errors were resolved. Gary Lapointe has created some very useful addons to the stsdm command line tool. His blog is a must read for anyone managing a SharePoint server. More information can be found on his blog at http://stsadm.blogspot.com/.

Friday, February 20, 2009

Download a File Using the SharePoint Copy Web Service

Here is a code sample I put together to download a file from a Document Library using SharePoint's Copy web services.
// sharepoint is a web reference that points to http://sharepoint/_vti_bin/copy
sharepoint.Copy myCopyService = new sharepoint.Copy();
myCopyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

// URL to the file you want to downlaod
string copySource = "https://sharepoint/file.doc";

// Define the variables that will store the output from the web service call
sharepoint.FieldInformation myFieldInfo = new sharepoint.FieldInformation();
sharepoint.FieldInformation[] myFieldInfoArray = { myFieldInfo };
byte[] myByteArray;

// Call the web service
uint myGetUint = myCopyService.GetItem(copySource, out myFieldInfoArray, out myByteArray);

// Convert into Base64 String
string base64String;
base64String = Convert.ToBase64String(myByteArray,0,myByteArray.Length);

// Convert to binary array
byte[] binaryData = Convert.FromBase64String(base64String);

// Create a temporary file to write the text of the form to
string tempFileName = Path.GetTempPath() + "\\" + myFieldInfoArray[0].Value;

// Write the file to temp folder
FileStream fs = new FileStream(tempFileName, FileMode.CreateNew, FileAccess.ReadWrite);
fs.Write(binaryData, 0, binaryData.Length);
fs.Close();

//Open file in default program
System.Diagnostics.Process.Start(tempFileName);

There is more information on the Copy web service here: http://msdn.microsoft.com/en-us/library/copy.aspx. Look under the Copy class and its GetItem method.

Wednesday, February 18, 2009

Welcome to my Blog

My company just assigned me to manage our internal deployment of SharePoint. The goal is to get all the departments using it, so this project is going to take some time. We have several plans for customizations and would like to connect to Great Plains down the road. This blog in going to be my place to share things I find useful and hopefully provide some help to others that are doing similar things. It should be a fun and eventful journey. Enjoy the ride!