Error: Collection was modified; enumeration operation may not execute

Many times I encountered error while executing PowerShell snippet: “Collection was modified; enumeration operation may not execute” while using ForEach loop of the list object. Here is the code that throws an error:

$web = Get-SPWeb "http://sharepointsiteurl"
ForEach($list in $web.Lists)
     if (!$list.Hidden)
         # do something to change the list or add list
         # Error: "Collection was modified; enumeration operation may not execute..."

To overcome this issue simply need to change ForEach loop to For, see below updated snippet to use:

For($i=0; $i -lt $web.Lists.count; $i++)

And if working for deletion of list in the collection, loop the collection in reverse order, see below code:

For ($i=$web.Lists.count-1; $i –gt 0; $i--)

Check Not Null condition in Keyword Query Language for SharePoint Search

In one of my project, I was using Keyword query language to generate search query string. There was a situation where I spent lot of time to find to see if I can apply not null condition for the specific text field(EnterpriseKeyword).

Unfortunately that’s not possible in direct way where you use just one keyword like NotNull or something else to achieve it.

By default, SharePoint can’t search empty values using Keyword Query Language.

Because empty value is not stored in index, so if you want to filter the manged property which is not empty, so there is different unearthly way to achieve is that you need to identify in the query how to include everything that has any value:

Text values consist of alphabetical characters (a..z) and numerical characters (0..9). SharePoint isn’t case sensitive so you have to do it only once per letter, see below example:

(EnterpriseKeyword:a* OR EnterpriseKeyword:b* OR EnterpriseKeyword:c* OR EnterpriseKeyword:d* OR EnterpriseKeyword:e* OR EnterpriseKeyword:f* OR EnterpriseKeyword:g* OR EnterpriseKeyword:h* OR EnterpriseKeyword:i* OR EnterpriseKeyword:j* OR EnterpriseKeyword:k* OR EnterpriseKeyword:l* OR EnterpriseKeyword:m* OR EnterpriseKeyword:n* OR EnterpriseKeyword:o* OR EnterpriseKeyword:p* OR EnterpriseKeyword:q* OR EnterpriseKeyword:r* OR EnterpriseKeyword:s* OR EnterpriseKeyword:t* OR EnterpriseKeyword:u* OR EnterpriseKeyword:v* OR EnterpriseKeyword:w* OR EnterpriseKeyword:x* OR EnterpriseKeyword:y* OR EnterpriseKeyword:z* OR EnterpriseKeyword:1* OR EnterpriseKeyword:2* OR EnterpriseKeyword:3* OR EnterpriseKeyword:4* OR EnterpriseKeyword:5* OR EnterpriseKeyword:6* OR EnterpriseKeyword:7* OR EnterpriseKeyword:8* OR EnterpriseKeyword:9* OR EnterpriseKeyword:0*)

Above example will help you apply not null condition for any text field in Keyword Query Language.

Download WSP from SharePoint Farm

Many times we come across a situation where we have the latest WSP with us in the Solution form (Visual Studio Solution), but we don’t have the installed version of WSP from the SharePoint farm (Central Administrations > System Settings > Manage farm Solutions).

So to get a copy of Installed version of WSP file from the SharePoint farm, following is the PowerShell snippet, which will download the WSP file to the location specified in the cmdlet:

$farm = Get-SpFarm
$file = $farm.Solutions.Item("MyWSPName.wsp").SolutionFile

Above snippet will save WSP file to the path C:\Backup. Make sure WSP file name MyWSPName.wsp specified does exist on the farm.

Note that in order to run this command, you need to be a member of the Farm Administrators group and have permission to the configuration database.