Friday, November 19, 2010

Sharepoint 2010 Foundation - Create nested folders from workflows

Problem: Create a nested folder structure in response to an event without writing code in Sharepoint 2010 Foundation.

Why: We have a need for this functionality on a hosted Sharepoint site where we don't have the ability to write custom code other than what can be done in Sharepoint Designer.

Research Approach:
1 - Look through Sharepoint Doc
Nothing useful found
2 - Google it!
Very little useful found
3 - Beat head against wall until a solution manifests
YEAH!

Solution: Interrelated Workflows.

The user would like to track a set of "projects." A project is a collection of status' as well as documents in a nested folder structure. I decided to create several sharepoint artifacts to represent the structure.

1 - "Project Details" list which holds the Project and associated fields/columns.

2 - "Projects" Document Library which holds Project Folders, Project Sub Folders and Project Document

3 - "Project Folder"
This is a Folder object with additional columns and holds objects associated with the project

4 - "Project Sub Folder"
This is derived from Project Folder and is used to nest structure in a Project Folder

5 - "Project Document"
This is a Document object with extra, project specific columns

There are also several list workflows:
1 - Update Project Manager Name - Project Details List
Copies a lookup field's contents to a text field so it can be carried along with a site column

2 - Create Project Folder - Project Details List
Creates a Project Folder in the Projects Document Library when an item is created in the Project Details list.

3 - Update Project Folder Status - Projects List (Document Library)
Updates the status of the Project Folder so that the Create Project Sub Folders workflow knows to run

4 - Create Project Sub Folders - Projects List (Document Library)
Creates the Sub Folders for a new projects.

There were several problems that needed to be solved. The main one is how do I create objects in a new, arbitrary list?

My first approach was to attempt to create a workflow that fired when an item was added to the Project Details list that created the items in another list. This worked fine to create the folder in the Projects list, but I couldn't go any farther because creating objects in an arbitrary list isn't straight-forward.

I created another workflow that ran when a Project Folder was created to create a folder in the "current" list which worked to create a sibling of the new Project Folder, but not a child. BUT, after a lot of experimentation, I determined that I could create a sibling of the current item but code the name as follows: [%CurrentItem%]/NewFolderName
This worked to create a child of the current folder! After I got this working, I was able to run the workflow manually and life was good. However, as soon as I changed the workflow to run automatically at the creation of a Project Folder, Sharepoint Designer would no longer let me save the workflow. It complained about errors in the create-list-item operations. I was able to get around it by setting the workflow to fire on changing a Project Folder rather than creating it. Surprisingly, that worked.

Now my problem was: how to fire the workflow at object creation. I decided to create a status field on the Project Folder and have a workflow that fires at Project Folder create time to change the status so that the Project Sub Folder creation workflow would run. So, I created a "SubFolders" column in the Project Folder. When the first workflow creates the Project Folder, it sets this value to Waiting. The workflow that runs at Project Folder create time changes this value to Creating which triggers the Create Project Sub Folders workflow. This workflow creates the Sub Folders and then changes the SubFolders column to Created.

That's it!

I'll give some more details on the objects, workflows, and interactions soon.