Saturday, July 30, 2011

PowerShell to help with Branding Deployment

Whilst working on a project to deploy a new branding solution, there was a requirement to disable existing brand features on all webs and site collections across all web applications, and then deploy a new branding solution to all root webs on all site collections. To do this manually would have been very tedious and time consuming, so using some simple PowerShell scripts made this process very easy and fast. Here are the scripts used:

DisableFeatures

$webApp = "http://WebAppUrl/"
$StapleFeature = Get-SPFeature | where {$_.DisplayName -eq "Name"}
$BrandingFeature = Get-SPFeature | where {$_.DisplayName -eq "Name"}
$WebPartsFeature = Get-SPFeature | where {$_.DisplayName -eq "Name"}

# Site collection scoped features
# Get all sites in the web app and if the feature exists, disable it
Get-SPSite -WebApp $webApp | foreach {
if($_.Features[$StapleFeature.ID]) {
Disable-SPFeature $StapleFeature -url $_.URL -Force -Confirm:$false
Disable-SPFeature $WebPartsFeature -url $_.URL -Force -Confirm:$false
}
}

# Web scoped features
# Get all webs in all sites in the web app and if the feature exists, disable it
Get-SPSite -WebApp $webApp | Get-SPWeb -limit all | foreach {
if($_.Features[$BrandingFeature.ID]) {
Disable-SPFeature $BrandingFeature -url $_.URL -Force -Confirm:$false
}
}

Enable Features

$webApp = "http://WebAppUrl/"
$StapleFeature = Get-SPFeature | where {$_.DisplayName -eq "Name"}
$BrandingFeature = Get-SPFeature | where {$_.DisplayName -eq "Name"}
$WebPartsFeature = Get-SPFeature | where {$_.DisplayName -eq "Name"}

# Site collection scoped features
# Enable features in all sites in the web app
Get-SPSite -WebApp $webApp | foreach {
Enable-SPFeature $StapleFeature -url $_.Url -Force
Enable-SPFeature $BrandingFeature -url $_.Url -Force
Enable-SPFeature $WebPartsFeature -url $_.Url -Force
}

I only had a couple of web apps, so just replaced the web app URL $webApp for each one and ran the scripts. You could just put another foreach and loop through all the webs if needed.

Monday, June 6, 2011

FileNotFoundException with new SPSite

When debugging a SharePoint console application in Visual Studio 2010, I ran into the error FileNotFoundException during the line:

using (SPSite site = new SPSite("http://dev.company.com/mysite/"))

The fix that worked for me was to make sure the Platform target: on the Build tab under the Project's properties page, was set to Any CPU from x86 which makes sense since SharePoint binaries are targeted to the x64 CPU.
The FileNotFoundException error is a little misleading, so if you run into, please check your Platform target.

Friday, June 3, 2011

AD Security Groups Not Showing in Web Part Audience Picker

When attempting to set audiences on a web part using AD Security groups, you click the Target Audiences Browse icon (under Advanced in the Web Part properties), and this displays the Select Audiences dialog box. Then you select Distribution/Security Groups from the Find drop down list, type a search key word and then click Search. Then a list of all the AD Security Groups should be displayed. However, in my case I was getting the message:

No security groups or distribution lists were found based on your search request. Refine the search text, and then rerun the search request.

So I checked the synchronization connection under Configure Synchronization Connections on the Manage Profile Service page in Central Administration, to see if I was omitting an AD container. (You can get to this page by going to Manage Service Applications on the Application Management menu, selecting your User Profile Service Application and selecting Manage in the ribbon). This looked fine as all the AD OUs with the security groups were already being included.

Then I did a full profile synchronization but the AD Security Groups were still missing from the Web Part Audience Picker. I then tried setting permissions on a site and list item, and the AD Security Groups were showing up, but just not in the Audience Picker.

So being unable to find a solution, I decided to delete and re-create the User Profile Service application. I would delete the sync database, but keep the existing user profile and social databases. Steps I followed:
  • Went to Manage Service Applications in Central Administration, deleted the User Profile Service Application (without deleting its data - clear the Delete data associated with the Service Application check box).
  • Deleted the Sync database from SQL Server
  • Created a new User Profile Service Application using the existing user profile and social databases
  • Added the Farm account to the local server admin group (can be removed after the next step runs)
  • Started the User Profile Synchronization service under Manage services on server link on the System Settings page. Wait for a little while for the status to go from Starting to Started
  • Removed the Farm account from the local server admin group (if added it above)
  • Re-created the synchronization connection under Configure Synchronization Connections on the Manage Profile Service page in Central Administration.
  • Started a full profile synchronization
Went back to the Audience Picker and now the AD Security Groups were showing up. Seems like way over-kill, and perhaps there is a much more simple and elegant solution, but this worked for me.