Nick Grattan’s SharePoint Blog

About Microsoft SharePoint and some .NET

Document Libraries: SPList and SPFolder.Delete Differences

leave a comment »

Removing items from a document library can be done in two different ways. Firstly, using SPList.Delete:

int iCount = spList.Items.Count;

for (int i = 0; i < iCount; i++)

{

    spList.Items[0].Delete();

}

This code will delete all the items/documents in all folders, but will leave the folders undeleted. Alternatively, you may write:

int iCount = spFolder.Files.Count;

for (int i = 0; i < iCount; i++)

{

    spFolder.Files[0].Delete();

}

This will delete the items/documents only in the folder represented by ‘spFolder’, and items/documents in other sub-folders will remain. The references to spList and spFolder can be obtained like this:

spList = currentWeb.Lists["MyDocumentLibrary"];

spFolder = currentWeb.Folders["MyDocumentLibrary"];

In this case, spFolder refers to the root folder in the document library.

 

 

 

 

 

 

 

 

Written by Nick Grattan

October 9, 2007 at 11:43 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.