Monday, February 26, 2007

How to add a site column to a list by code

A colleague just asked me how to add a site column defined in the root web to a list somewhere in the site, so here is the small snippet:

private void AddSiteColumnToList(string sitePath, string listName, string columnName)
     using (SPSite site = new SPSite(sitePath))
          using (SPWeb web = site.OpenWeb())
                //Since the site column is on the root web,
                //we need to get the root web
                //If it was'nt on the root web, but instead on the same site as the 
                //list, we could have just used the "web" object
                using (SPWeb root = site.RootWeb)
                     //get the site column from the root web
                     SPField fld = root.Fields[columnName];
                     //get the list from the site
                     SPList list = web.Lists[listName];
                     //add the column
                     //update the list

How to use the GetListItems web service?

Small tip for using the web service easily.
I posted this on the msdn forums a lot of times for people who had problems getting the web service to work.
The following are two functions that you can use after adding referances to Lists.asmx and SiteData.asmx (why sitedata? because I use it to get the WebID that is needed by lists.asmx)

private System.Xml.XmlNode GetItems(string webPath)


ListsOnLocalhost.Lists listsWS = new Sharepoint_Unit_Tests.ListsOnLocalhost.Lists();

listsWS.Url = webPath + "/_vti_bin/lists.asmx";

listsWS.UseDefaultCredentials = true;

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.LoadXml("<Document><Query /><ViewFields /><QueryOptions /></Document>");

System.Xml.XmlNode listQuery = doc.SelectSingleNode("//Query");

System.Xml.XmlNode listViewFields = doc.SelectSingleNode("//ViewFields");

System.Xml.XmlNode listQueryOptions = doc.SelectSingleNode("//QueryOptions");

Guid g = GetWebID(webPath);

System.Xml.XmlNode items = listsWS.GetListItems(listName.Text, string.Empty, listQuery, listViewFields, string.Empty, listQueryOptions, g.ToString());

return items;




private Guid GetWebID(string webPath)


SiteDataWebService.SiteData siteDataWS = new SiteDataWebService.SiteData();

siteDataWS.UseDefaultCredentials = true;

SiteDataWebService._sWebMetadata webMetaData;

SiteDataWebService._sWebWithTime[] arrWebWithTime;

SiteDataWebService._sListWithTime[] arrListWithTime;

SiteDataWebService._sFPUrl[] arrUrls;

string roles; string[] roleUsers; string[] roleGroups; 

siteDataWS.Url = webPath + "/_vti_bin/sitedata.asmx";

uint i = siteDataWS.GetWeb(out webMetaData, out arrWebWithTime, out arrListWithTime, out arrUrls, out roles, out roleUsers, out roleGroups);

Guid g = new Guid(webMetaData.WebID);

return g;


Thursday, February 15, 2007

Are you in Sydney? Are you in Canberra? Come see me!

Hey Guys!

This is for all you Aussies out there. I will be presenting in both Australian user groups next week - Sydney user group on Tuesday and Canberra User group on Wednesday. I am still waiting to get invited to the Melbourne SharePoint User Group and the Brisbane SharePoint User Group (which I really am looking forward to - I want to take the trip and scuba dive in the great barrier reef!)

The Sydney talk is going to be very practical in SharePoint. I am going to show how to use my utility pack efficiently, and I am going to show how to customize the built in search web parts.
This will actually be a summary of my many posts on the subjects here in this blog.

The Canberra talk is going to be more theoretical - I am going to talk about my bets practices and patterns for sharepoint software architecture design. I will try to summarize that into an article over the weekend and post it here.

Wednesday, February 14, 2007

Publishing Infopath To SharePoint: "the following url is not valid" Error

I spent nearly a week of work before I got this error figured out.

I was trying to publish a form that I succesfully published to all development servers to a test server. Every time I tried to publish it would say "the following url is not valid" when I entered the path to the sharepoint site.

Searching the internet, I found some possibilities, but none worked. For the sake of people who encounter this error, I will outline my troubleshooting recommendations below:

  1. Make sure the sharepoint application that contains the site you are publishing to has a site in the root.
    That was my mistake (mistake?). If you create all the sites under a virtual path such as "/sites/", infopath cannot publish.
    So make sure there is a sharepoint site on the "/" path.
  2. Create a new SSP.
    I have no idea why, but people on the internet claimed this solved the problem they had.
  3. If the sharepoint in question is of version 2003, uninstall .NET2 from the server. Again, no explanation was given, but one post in the news groups claim it solved their problem (and probably started a lot of others...)

Thursday, February 08, 2007

SharePoint Tips Utility Pack source code is released!

Finally I got the project open in codeplex, and finally I installed team foundation, and now version 1.1 of the utility pack, including the source code is available from the utility pack project site.

The new version includes improvements to the field changing page (you can now rename a field, and you can define that the change should be in the selected list and in all the lists in all the sub sites of the selected site), and it remembers the last site you opened.
I even went all out and created an installation application.

So use it, and use it wisely. If you have requests - the project site has a issue list where you should post your requests.
If you change the code - please post any changes for the community to review, and always remember to give credit where it's due.