Delete All items in a Sharepoint list

Posted: February 25, 2009 in Development, Lists, SharePoint
Tags: , ,

I was trying to create a method to delete all items in a sharepoint list .

here is the code , It’s simple but a question remained for me at the end :


        public void DeleteAllRecords()
        {
            SPList list_a = web.Lists["UserDocs"];
            SPListItemCollection itemsCol = list_a.Items;
            if (itemsCol.Count > 0)
                for (int i = itemsCol.Count-1; i > -1; i--)
                {
                    itemsCol.Delete(i);
                }
        }

I don’t know why the “for” loop does not work when I used it in a incremental way .  I mean that I found that I should use it in a decremental way !! I’m trying to find the answer and if so I’ll post it soon.

Advertisements

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 )

Google+ photo

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

Connecting to %s