Friday, July 11, 2008

Sharepoint Workflow to Make Items Context Aware

One of the things I have wanted since I first started working with Sharepoint 2007 is to have the documents and items in Sharepoint/MOSS be context aware. What I mean by that (and people may understand different things by "context aware") is that when an item is created or moved, the item itself knows where it now is. I want a custom column or metadata element that is automagically updated to reflect the context. For me, this mostly means having a custom column that keeps up to date with the folder a document or item is in.

I did the normal stuff people do to get this happening. I mucked around for ages with Microsoft Office Sharepoint Designer (MOSD), trying to get the workflow wizard to build a workflow to do this. No such luck. In the end I created a custom workflow in Visual Studio 2005 to do this for me, and thought I would share the code with my vast readership (yes, you know who you are, all 3 of you). This code may seem a bit cludgey to some of you. If you can see a better way to do this, let me know. Pre-empting one comment though, every time I tried to reference
WorkflowProperties.Item.File.ParentFolder, or any variation on this, VS kindly told me that this was null. I suspect that if I try this for a document it will work, but as I use this code to make tasks context aware, there is no file object?

This code is particularly useful, because if you copule this with my previous Sharepoint related post about displaying Sharepoint custom columns in Outlook, it means that you can show in Outlook the Sharepoint context of a task ... Handy!
private void logWorkflowStarted_MethodInvoking(object sender, EventArgs e)

//Default value
FolderName = "Unknown";
//Get the current item
SPListItem listItem = WorkflowProperties.Item;
//Get web reference
SPWeb web = listItem.Web;
//Get list reference
SPList list = web.Lists[listItem.ParentList.ID];
//Get URL for item
string FullURL = web.Url + listItem.Url;
//Get last slash
int index = FullURL.LastIndexOf("/");
//Set parent folder URL
string parentFolderUrl = string.Empty;
//If slash found
if (index > -1)
//Get url of parent
parentFolderUrl = FullURL.Substring(0, index);
//Get folder
SPFolder folder = list.RootFolder.SubFolders[parentFolderUrl];

if (folder.Exists)
FolderName = folder.Name;

//Update folder name
WorkflowProperties.Item["Project"] = FolderName;
//Update document meta data
catch (SPException ex)
throw ex;


Monday, July 07, 2008

Display Sharepoint Custom Columns in Outlook

This little problem has been troubling me for a few days and it appears I am not alone in this. I have a number of Project Task folders, each with very similar or identical tasks within them. For example, multiple folders may have the task "Update spec", assigned to the same person. So when these tasks are synched to Outlook 2007 using the "Connect to Outlook" function in Sharepoint, these identical tasks become indistinguishable. The only way around this appeared to be to expose a custom column that gave the task some context. However, Sharepoint custom columns cannot, it appears be exposed to Outlook, as they do not match the default schema for the object in Outlook (see here for Microsoft's lame explanation).

Found a way around this in the end. This solution has now been in testing for over 5 minutes and so I can conclude it is rock solid (not). Anyway ... Out of the box, Outlook will expose default columns from its own schema. So the answer is to put the context information for the task in a task column in Sharepoint that matches a default one in the Outlook task schema. Pick one that you aren't using for anything else important, perhaps like the Company column. This is called Related Company in Sharepoint. Put the context information in this, and this will be exposed as Company in Outlook. It appears you can then change the name in MOSS and still expose the information in Outlook ... although the name change itself will not be reflected in Outlook. Hope that helps.

Thursday, July 03, 2008

More Sharepoint Struggles

Struggling with some other Sharepoint stuff at the moment. Here are a few MOSS issues that I want to answer. Drop me a note if you have the answer or find a post that does.
  1. How to expose custom columns from Sharepoint in Outlook 2007?
  2. Why do some folders from a synchronised Sharepoint document repository get duplicated in Outlook?
  3. How can I add a dynamic title for a custom action in the drop down for an item? (I want to be able to progress a task's status from "In Progress" to "Complete" and so on, from the right click menu on the task's drop down menu, in Sharepoint. This means I have to be able to change the title in the drop down menu after the user clicks.)
No answers in sight for these questions so far but I am guessing that somewhere out there in google-space, someone has the answer.