Fljótur og Þægilegur: Fá SPFolder af SPItemList

Ég að halda áfram að keyra inn í þetta vandamál og Google aldrei virðist að skilja hvað ég vil gera, svo ég mynstrağur ég myndi skrifa þetta niður.

Ég hef verið að gera mikið af atburði móttökutæki kembiforrit í síðustu viku eða tvær. The ER is defined against a document library. The individual items in the document library are tightly related to their parent folders. Svo, 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, vinsamlegast eftir athugasemd.

<code>
með System;
með System.Collections.Generic;
með System.Text;
með Microsoft.SharePoint;
með System.Collections;

namespace Conchango
{
    /// <Yfirlit>
 /// </Yfirlit>
 flokki ManualFolderUpdate
    {
        truflanir ógilt Main(band[] args)
        {
            band msh = "ManualFolderUpdate (v1.0): "; // msh = "Message Header"

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

            band url = http://localhost/xyzzy;

            með (SPSite oSPSite =  SPSite(url))
            {

                með (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;
                    band NewStatus = "xyzzy";

                    FolderID = System.Umbreyta.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.");

                    Console.ReadLine();

                    thisItemFolder.Item["Approval Status"] = NewStatus;
                    thisItemFolder.Item.Update();

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

                } // using SPWeb

            } // using SPSite

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

        } // Main

    } // class foldersync
} // namespace
</code>

</enda>

Gerast áskrifandi að bloggið mitt.

Technorati Tags:

Eftir svar

Netfangið þitt verður ekki birt. Nauðsynlegir reitir eru merktir *