Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm looking to mass delete certain views on a list (about 1000). the naming convention used for the views is [user name] [r#] [view name] view.

the # is a value ranging from 1-5 and the view name has one of 14 values. I am looking to remove about 5 of the views per user from the list, as I believe it is effecting the performance of the site.

Is this possible? I've only used powershell for the most basic tasks (deleting a column, uploading a solution). And if it's not possible with powershell, is there another way?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

It should be. If you can get to a column in a list in PowerShell, you should be able to work out how to get to the Views property of a List object. This will return all of the Views in the list. You can them simply iterate over them all (using a for loop, not a foreach), check the name of the View and delete it.

share|improve this answer
    
Thanks, it sounds possible to me as well, but I haven't been able to find a function that pulls all the views from a list. The how to delete a view ones all have the user explicitly say the view name. –  hrezs Feb 13 '12 at 18:05
    
I have found the property List["List Name"].Views, which I assume will return a collection of the views. Why is it that you will use a for loop and not a foreach? –  hrezs Feb 13 '12 at 20:08
    
It's a way the programming/scripting language works when you deal with 'collection objects'. Everytime the code runs inside a foreach, it looks to a pointer to the collection which it knows it has X-many items in it. If you change that number of items (by adding or deleting) then it screws up that pointer as the count doesn't match what it was at the start, so the script engine prevents you running code that will do that. –  James Love Feb 13 '12 at 20:19
    
Instead, if you use a for loop, and access the View numerically.. List["MyList"].Views.Delete(IDOfList). An important thing to note here is you must have a reverse-counting loop - one that starts with the number of views there are, and counts back one at a time towards 0. The reason for this, if there are 100 Views, and you delete 5, then once your code gets to the 96th iteration, it'll bail out and say there's no View there (give an ArgumentOutOfRangeException). –  James Love Feb 13 '12 at 20:22
    
Ah, ok. So it's basically an arraylist. The other issue I see then is that you will skip over views, where if I delete number 5, 6 becomes the new 5 but I have already incremented past that. Thanks for your help! –  hrezs Feb 13 '12 at 21:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.