Brzo i jednostavno: Nabavite SPFolder od SPItemList

Ja i dalje prikazivati ​​u ovaj problem i čini se da Google nikada ne razumiju ono što želim učiniti, tako da sam shvatio bih napisati ovo dolje.

Ive 'bio događaj puno događaja prijemnik za ispravljanje pogrešaka u posljednjem tjednu ili dva. The ER is defined against a document library. The individual items in the document library are tightly related to their parent folders. Tako, 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, molimo vas da ostavite komentar.

<code>
pomoću Sustav;
pomoću System.Collections.Generic;
pomoću System.Text;
pomoću Microsoft.SharePoint;
pomoću System.Collections;

namespace Conchango
{
    /// <rezime>
 /// </rezime>
 klasa ManualFolderUpdate
    {
        statički poništiti Glavni(niz[] args)
        {
            niz msh = "ManualFolderUpdate (v1.0): "; // msh = "Message Header"

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

            niz url = http://localhost/xyzzy;

            pomoću (SPSite oSPSite = novi SPSite(url))
            {

                pomoću (SPWeb oSPWeb = oSPSite.OpenWeb())
                {
                    SPList docLib = oSPWeb.Lists["Documents"];

                    Utješiti.WriteLine(msh + "Got the document library.");

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

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

                    FolderID = System.Pretvoriti.ToInt32(args[0].ToString());
                    Utješiti.WriteLine("Seeking folder for item: [" + FolderID + "].");

                    SPListItem li = docLib.GetItemById(FolderID);

                    SPFolder thisItemFolder = li.File.ParentFolder;

                    Utješiti.WriteLine(msh + "Got the parent folder.");

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

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

                    Utješiti.ReadLine();

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

                    Utješiti.WriteLine(msh + "Finished updating the folder.  Exiting.");

                } // using SPWeb

            } // using SPSite

 Utješiti.WriteLine(msh + "Finished.");

        } // Glavni

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

</kraj>

Pretplatite se na moj blog.

Technorati Tags:

Dopust jedan Odgovor

Vaša email adresa neće biti objavljena. obavezna polja su označena *