Generate Multiple Views on SharePoint Lists or Libraries

I was having a weird problem with a list we had migrated, and one of the unusal characteristics was that this list had about 50 different views.

To try and replicate the issue with another list, I wanted to create 50 views very quickly.

Here is how I did it in PowerShell:

$w = Get-SPWeb http://portal.company.com
$l = $w.Lists["Documents"]
$maxViews = 50
 
for ($viewCount=0;$viewCount -lt $maxViews;$viewCount++)
{
	# $null prevents flooding the screen with view information
	# Clone Method Parameters: Name, Row Limit, Paged, Make Default View
	$null = $primaryView.Clone(($primaryView.Title + " " + $viewCount) , 30, $true, $flase)
}

The script worked like a charm in creating the views, however didn’t help me resolve the issue I had with my list.

Disable SharePoint Mobile View with PowerShell

A client recently had a request to disable the mobile view feature of SharePoint 2010. Fortunately, Jeremy Thake has written an excellent article on this already – How We Did It – Mobile View. We tested the second option and decided to go with that approach. The only problem was that I wasn’t keen on editing 10 web.config files.

Again, PowerShell comes to the rescue! I discovered that we could make web.config changes with the SharePoint object model, and therefore have all configurations updated at once. Special thanks to Farhan Faiz for his blog entry: SharePoint 2010: PowerShell script to add in web.config.

$webApp = Get-SPWebApplication http://portal.company.com
$configMod = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$configMod.Path = "configuration/system.web"
$configMod.Name = "browserCaps"
$configMod.Sequence = 0
$configMod.Type = 0
$configMod.Value = "<browserCaps> </browserCaps>"
 
$configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$configMod1.Path = "configuration/system.web/browserCaps"
$configMod1.Name = "result"
$configMod1.Sequence = 0
$configMod1.Type = 0
$configMod1.Value = "<result type=""System.Web.Mobile.MobileCapabilities, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a""/><filter>isMobileDevice=false</filter>"
 
$webApp.WebConfigModifications.Add($configMod)
$webApp.WebConfigModifications.Add($configMod1)
$webapp.Update()
$webapp.Parent.ApplyWebConfigModifications()
$webapp.WebConfigModifications.Clear()

Finding User Profiles With A Specific Property

Today I was looking at a user profile property in SharePoint 2010, and wondered why the usage (ie “number of profiles using this property”) was relatively low. I needed a quick way to get this information out.

The first two lines should be modified to your MySites host and the property you want to audit (eg PictureURL). The last line is an object representing the value of each property where populated and can be easily modified to suit your query.

$mySiteUrl = "http://mysites.company.com"
$findProperty = "PictureUrl"
 
$mySiteHostSite = Get-SPSite $mySiteUrl
$mySiteHostWeb = $mySiteHostSite.OpenWeb()
$context = Get-SPServiceContext $mySiteHostSite
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$AllProfiles = $profileManager.GetEnumerator()
$outputCollection = @()
 
foreach ($profile in $AllProfiles)
{
    $output = New-Object System.Object
    $output | Add-Member -type NoteProperty -Name AccountName -Value $profile["AccountName"].ToString()
    $output | Add-Member -type NoteProperty -Name $findProperty -Value $profile[$findProperty] 
    $outputCollection += $output
}
 
$outputCollection | ? {$_.($findProperty) -ne ""} | Sort-Object -Property AccountName

Turn Off Second Stage Recycle Bin

A quick way to turn off the Second Stage Recycle Bin in SharePoint 2010 – set the quota to zero. In Central Admin, you cannot set a zero value, but there is an option to turn it off.

foreach ($webApp in Get-SPWebAPplication)
{
    $webApp.Name
    $webApp.SecondStageRecycleBinQuota = 0
    $webApp.Update()
}

Simple enough, and while working on a client site I’ve come across an issue with a particular web application and received the error:

Exception setting "SecondStageRecycleBinQuota": "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
At line:4 char:13
+     $webApp. <<<< SecondStageRecycleBinQuota = 0
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Both in PowerShell and in Central Admin, I was able to set the quota for the second stage recycle bin to as low as 1%, but not turn it off.  In Central Admin, I was getting this error:

CA-SecondStageRecycleBin

After turning on verbose logging, this error was picked up:

Access Denied for /_admin/vsgeneralsettings.aspx?type=gen&WebApplicationId=707dac5e9db64a96b509c5f153ace591&IsDlg=1. StackTrace:  
 at Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(HttpContext context)   
 at Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(Exception ex)   
 at Microsoft.SharePoint.Library.SPRequest.GetAdminRecycleBinItems(String bstrSiteUrl, Object& pvarItems, UInt32& pdwNumberOfItems)   
 at Microsoft.SharePoint.SPRecycleBinItemCollection.EnsureContent()   
 at Microsoft.SharePoint.SPRecycleBinItemCollection.get_Count()   
 at Microsoft.SharePoint.SPRecycleBinItemCollection.GetSortedSecondStageIds()   
 at Microsoft.SharePoint.SPRecycleBinItemCollection.DeleteAllSecondStageItems()    
 ... 

It appeared that the logged in account did not have access to the recycle bin to remove the contents.  This was not a problem for other Web Applications, and did not use the Farm Service Account as I expected.  Granting the logged in account with Full Control to the Web Application User Policy for the problematic Web Application allowed me to turn off the second stage recycle bin with both PowerShell and Central Admin.  I can only presume the logged in account already had access to the other Web Applications which is why they worked.

Replace URL in a SharePoint List

Had a recent situation where a client had changed the URL of their K2 infrastructure during an upgrade.  Since all the links from their SharePoint lists were now broken it was up for PowerShell to update the 14,000 items. This customer had three lists in the same site with different column names, so I wrote it as a function for re-use.

function Replace-URL-In-List ([string]$webUrl, [string]$listName, [string]$columnName, [string]$oldValue, [string]$newValue) {
 
    $web = Get-SPWeb $webUrl
    $list = $web.Lists[$listName]
    $allItems = $list.Items
    foreach ($item in $allItems)
    {
         if ($item[$columnName] -like "*"+$oldValue+"*")
         {
            "OLD: " + $item[$columnName] + "    NEW: " + ($item[$columnName] -Replace $oldValue,$newValue)
            $item[$columnName] = ($item[$columnName] -Replace $oldValue,$newValue) #Comment this line out to run as a test
            $item.Update()  #Comment this line out to run as a test
          }
    }
#This is to confirm the values parsed in
"OLD VALUE: " + $oldValue
"NEW VALUE: " + $newValue
 
}
 
$webUrl = "https://portal/web/"
$oldValue = "https://oldK2/_tools"
$newValue = "https://newK2/tools"
 
Replace-URL-In-List -webUrl $webUrl  -listName "Requests" -columnName "ViewForm" -oldValue $oldValue -newValue $newValue