Friday, May 16, 2008

Add SPLookup column declaratively through CAML XML

Up until a day ago, I thought that adding a SharePoint lookup column through CAML XML was impossible, and that you could only do it programmatically using a feature receiver. It made sense; how can you add a lookup to a list that doesn't exist yet?

A colleague of mine sent through this link to Josh Gaffey's Blog.

Check out how easy it is. I bet there are a lot of smart SharePoint dev's out there saying "I wish I thought of that".

Monday, May 12, 2008

Deploy a user control(.ascx) as a SharePoint web part

Frustrated by the lack of control over the UI when creating SharePoint web parts, want something more lightweight than SmartPart. The following code snippet illustrates how you can deploy a user control as a SharePoint web part.

1) Place your complied code in either the GAC, or web application \bin folder.
2) Save your user control to the file system
(usually: "12\TEMPLATE\CONTROLTEMPLATES").
3) Replace the string "MyUserControlPath" with the location of the usercontrol.
4) Deploy web part as normal.

 
[Guid("9c974b27-d41a-4053-8eb2-76c25e762f1b")]
public class MyWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
public MyWebPart()
{
this.ExportMode = WebPartExportMode.All;
}

protected override void CreateChildControls()
{
try
{
base.CreateChildControls();

this.Controls.Add((System.Web.UI.UserControl)
Page.LoadControl("MyUserControlPath"));
}
catch (Exception ex)
{
EventLog.WriteEntry("WebParts", ex.Message, EventLogEntryType.Error);
}
}
}

Wednesday, May 7, 2008

"sitedefinitiontemplate.wsp" has an unsupported extension, and cannot be added to the solution store.

I came across this problem when I was using STSADM to add my solution to SharePoint.
I used the command stsadm.exe -o addsolution -name sitedefinitiontemplate.WSP and couldn't figure out what was wrong?

It turns out that stsadm doesn't recognise the capitalised .WSP, when I used stsadm.exe -o addsolution -name sitedefinitiontemplate.wsp the "opperation completed sucessfully"

How to programatically associate a workflow with a list

I needed to do this in a previous project where we created a custom workflow and needed to associate that workflow with a list that existed within a SharePoint site.

We used a site definition that provisioned a pages library, and activated the workflow feature each time a site was created.

However it was still a manual step to associate the workflow with the pages library. To solve this we created a site feature receiver that ran when the site was created and called the following code.


 
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPWeb siteWeb = (SPWeb)properties.Feature.Parent)
{
SPList list = siteWeb.Lists["Name here"];
SPWorkflowTemplate baseTemplate =
siteWeb.WorkflowTemplates["Workflow Guid"];

SPWorkflowAssociation assoc = SPWorkflowAssociation.CreateListAssociation(
baseTemplate
, "NameOfWorkflow"
, "SPList task list"
, "SPList history list");

list.AddWorkflowAssociation(assoc);
}
}

Thursday, May 1, 2008

Adding a Custom web part to SharePoint and MOSS

Below is a method that shows how you can add a custom web part to SharePoint
 
public static void AddCustomWebPart(SPWeb web
, System.Web.UI.WebControls.WebParts.WebPart webPart
, string fullOrRelativeUrl
, PersonalizationScope scope
, string webPartZoneID
, int webPartZoneIndex)
{
SPLimitedWebPartManager wpManager =
web.GetLimitedWebPartManager(fullOrRelativeUrl, scope);

wpManager.AddWebPart(webPart, webPartZoneID, webPartZoneIndex);
}


The code looks pretty easy but it wasn't immediately obvious to me at the beginning. I struggled to find the second param webPart. Initially I looked within SharePoint to see if I could find it, you would think that you could look in the web part gallery find the web part and add it. This was not the case.

The easiest way I found was to deploy the web part as standard to your site. Then include the complied .dll for your custom web part as reference in your deployment console or feature receiver.

A sample usage is below. Notice I have included the "using SampleSharePointSolution.WebPart;" now I can pass new HelloWebPart() as the web part param.

 
using SampleSharePointSolution.WebPart;

namespace DeploymentConsole
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://vm-spdev-7497:1000/"))
{
using (SPWeb web = site.RootWeb)
{
Deploy.AddCustomWebPart(web
, new HelloWebPart()
, "Default.aspx"
, PersonalizationScope.Shared
, "Left"
, 0);
}
}
}
}
}

Adding a ListView web part to SharePoint and MOSS

At the moment I'm pretty interested in looking at ways we can manage an existing SharePoint or MOSS infrastructure.

For example say you had a MOSS installation with 200 client colaboration sites and somebody wants to add a new web part to all these sites? Short of making the web part available to all sites and then asking the clients to add the web part themselves what can we do?

Below is a code snippet that shows how to add a new list view web part.

 
public static void AddListViewWebPart(SPWeb web
, SPList list
, SPView listView
, string fullOrRelativeUrl
, PersonalizationScope scope
, string webPartZoneID
, int webPartZoneIndex)
{
ListViewWebPart listViewWebPart = new ListViewWebPart();

listViewWebPart.ListName = list.ID.ToString("B").ToUpper();
listViewWebPart.ViewGuid = listView.ID.ToString("B").ToUpper();

SPLimitedWebPartManager wpManager =
web.GetLimitedWebPartManager(fullOrRelativeUrl, scope);

wpManager.AddWebPart(listViewWebPart as
System.Web.UI.WebControls.WebParts.WebPart
, webPartZoneID
, webPartZoneIndex);
}


Example usage from a console app. It adds a list view webpart using the default view to the top of the left webpart zone on the default.aspx page.

This method could also be called from a feature receiver.

Try it out.

 
namespace DeploymentConsole
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://vm-spdev-7497:1000/"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists["New Library"];

Deploy.AddListViewWebPart(web
, list
, list.Views[0]
, "Default.aspx"
, PersonalizationScope.Shared
, "Left"
, 0);
}
}
}
}
}


I will also give code to add a custom web part in a future post.

April was a big month

Well here is my first post about "Life and Other Stuff". Today is the first of May and I just wanted to take a moment to reflect on what a great month April has been for me personally.

On the 5th of April I competed in my first Half Iron Man triathlon. This consists of a 2km sea swim, a 90km solo bike ride, and a half marathon. This was a special day and marked the culmination of a lot of hard work. I started training around Christmas with the initial goal of completing the event in under 5h:30m.

I finished 13th in the open men's competition in a time of 4h:51m:48s. In the process I swam the fastest I have ever swam for 2km, biked faster over 90km than ever, and after all that ran my first half marathon. A big thanks to all my friends and family, I definitely could not have done it without your support.

Over the next two weekends, dad and I rode two 100km+ road races in the South Island which was a lot of fun.

This month starts with my first 12hour Adventure Race in the Coromandel, with my girlfriend. Should be fun :s