Quick and Easy: Get SPFolder of SPItemList

I keep running into this problem and the Google never seems to understand what I want to do, so I figured I would write this down.

I’ve been doing a lot of event receiver debugging in the last week or two. The ER is defined against a document library.  The individual items in the document library are tightly related to their parent folders.  So, I am always getting the folder of the item for various manipulations.  While debugging, I needed to update the metadata of a folder for a specific item whose ID I know.

Here’s a little console application (designed to run on server in the farm) that takes two arguments: the ID of an item and a value to assign to a field, "Approval Status".  It hard codes a lot of stuff and has no error checking.

The code looks up a hard coded site, gets a hard coded document library and then finds the indicated item.  It then finds the parent folder of that item and assigns the status value.

The key lesson here for me is that SPItem doesn’t get you access to the folder.  You need to use SPListItem.File.

If anyone cares to offer a critique or suggest a better way to get the folder of an item, please leave a comment.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Collections;

namespace Conchango
    /// <summary>
 /// </summary>
 class ManualFolderUpdate
        static void Main(string[] args)
            string msh = "ManualFolderUpdate (v1.0): "; // msh = "Message Header"

 Console.WriteLine(msh + "Starting up.  I was last modified on 08/04/08.");

            string url = http://localhost/xyzzy;

            using (SPSite oSPSite = new SPSite(url))

                using (SPWeb oSPWeb = oSPSite.OpenWeb())
                    SPList docLib = oSPWeb.Lists["Documents"];

                    Console.WriteLine(msh + "Got the document library.");

                    Console.WriteLine(msh + "Doc lib item count: [" + docLib.ItemCount + "].");

                    int FolderID = 0;
                    string NewStatus = "xyzzy";

                    FolderID = System.Convert.ToInt32(args[0].ToString());
                    Console.WriteLine("Seeking folder for item: [" + FolderID + "].");

                    SPListItem li = docLib.GetItemById(FolderID);

                    SPFolder thisItemFolder = li.File.ParentFolder;

                    Console.WriteLine(msh + "Got the parent folder.");

                    NewStatus = args[1].ToString();
                    Console.WriteLine("Setting status to [" + NewStatus + "].");

                    Console.WriteLine("Press return to commit the update or CTRL-C to abort.");


                    thisItemFolder.Item["Approval Status"] = NewStatus;

                    Console.WriteLine(msh + "Finished updating the folder.  Exiting.");

                } // using SPWeb

            } // using SPSite

 Console.WriteLine(msh + "Finished.");

        } // Main

    } // class foldersync
} // namespace


Subscribe to my blog.

Technorati Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *