Disable Mobile Redirection for a Web Application via PowerShell

So I’ve got a SharePoint site that is optimised for viewing in mobile browsers, problem is that OOTB SharePoint is trying to show the fugly “mobile view”. Now Waldek has a post about just this problem, in my case I’ve decided that his first offered work around is perfectly acceptable for my scenario.

Now if you haven’t worked it out yet I also have a requirement that my deployments must be 100% repeatable and driven my PowerShell. So I need to modify the web.config file via PowerShell, enter a helpful post from the Script Guy blogs.

From there it wasn’t too hard to create my own script:

#Disable Mobile Redirection for all browsers for a given web application

param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] 
			[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]
			$WebApplication)
	
# SharePoint cmdlets
Add-PSSnapin Microsoft.SharePoint.PowerShell
if([Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")-eq$null){throw "Unable to load Microsoft.SharePoint.dll";}

$WebApp = $WebApplication.Read()

$configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$configMod1.Path = "configuration/system.web"
$configMod1.Name = "browserCaps"
$configMod1.Value = '<browserCaps><result type="System.Web.Mobile.MobileCapabilities, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /><filter>isMobileDevice=false</filter></browserCaps>'
$configMod1.Sequence = 0
$configMod1.Owner = "contoso\administrator"
## SPWebConfigModificationType.EnsureChildNode -> 0
$configMod1.Type = 0

$WebApp.WebConfigModifications.Add( $configMod1 )

$WebApp.Update()

$WebApp.Parent.ApplyWebConfigModifications() 
	
Remove-PsSnapin Microsoft.SharePoint.PowerShell
	

I highly recommend that you read both the blogs I linked above, it’s articles like those that enable me to stand on the shoulders of giants to achieve my ends

Posted in Deployment, Development, PowerShell, SharePoint | 7 Comments

Provisioning Web Parts via the AllUsersWebParts element

Right, so you want to have a Page Layout that contains some web parts when a user creates a page using that layout. Cool, no problem, use an <AllUsersWebParts> element just like Andrew Connell blogs about problem is, if you activate that feature multiple time you’ll get an instance of the web parts in the provisioned page for each activation.

Now, Waldek suggests some approaches to use for provisioning web part instances and the one that fits is going to depend on your needs.

But what happens if you’ve gone down the AllUsersWebParts path and are now running into problems, as I was today? Well, with a quick bit of coding I whipped up a command line tool to remove all the web parts form a page layout. If you do this then re-activate the feature that provisions the page layouts (and their web parts) you will get back to a state of one instance of each desired web part.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebPartPages;
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;

namespace RemoveWebPartsFromPageLayout
{
    class Program
    {
        static void Main(string[] args)
        {
            if(2 != args.Length)
            {
                Console.WriteLine("usage: RemoveWebPartsFromPageLayout <SPSiteUrl> <PageLayoutTitle>");
                return;
            }
            using(SPSite site = new SPSite(args[0]) )
            {
                using (SPWeb web = site.RootWeb)
                {
                    if (PublishingWeb.IsPublishingWeb(web))
                    {
                        PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
                        foreach (var layout in pubWeb.GetAvailablePageLayouts())
                        {
                            if (args[1] == layout.Title)
                            {
                                DeleteWebParts(layout);
                            }
                        }
                    }
                }
            }
        }

        private static void DeleteWebParts(PageLayout layout)
        {
            SPFile file = layout.ListItem.File;
            if (file.CheckOutType == SPFile.SPCheckOutType.None)
            {
                file.CheckOut();
            }
            using (SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {
                List<WebPart> deletes = (from object webPart in wpm.WebParts select webPart as WebPart).ToList();
                foreach (var webPart in deletes)
                {
                    wpm.DeleteWebPart(webPart);
                }
                file.CheckIn("WebParts Deleted");
                file.Publish("WebParts Deleted");
                file.Approve("WebParts Deleted");
            }
        }
    }
}
Posted in Development, SharePoint | Leave a comment

Boo-yah! @nzben to join Intergen

So, according to both Twitter and the NBR, Ben Gracewood, aka @nzben aka  that guy with the cool toys from Breakfast, is joining Intergen.

I’m pretty stoked that he’ll be joining the team here and hope to lean a lot while working with him.

-Gavin

Posted in General, Intergen, News | Leave a comment

Try out Azure and win*

So Microsoft New Zealand have set up a sweet competition, basically try out the Windows Azure platform and you’re in with a chance to win up to $1550 worth of Prezzy Cards. One $50 daily draw and then a $500 draw at the end.

You don’t even need to be a ‘Developer’ to enter, even the ‘Next, Next, Finish’ IT Pro crowd can handle this Winking smile (Kidding, I love the work done by IT Pros). Here’s what you need to do:

1. Visit http://be.1tri.be/

2. Setup your free (Credit Card Free too) 30 Day Windows Azure trial account.

3. Follow the instructional video (http://be.1tri.be/Begin) to upload the supplied Windows Azure Solution Package

Once you’ve done that the great thing is that you have a month of free access to the Azure platform to try it out and see what Microsoft are offering in the cloud computing space.

*Only people living in New Zealand are eligible to register for the Competition and enter the prize draws, full competition Terms and Conditions
Posted in Azure | Leave a comment

Content Migrated

Whew….

Well I couldn’t find the database backup from my old site, but I still recovered all the old posts from my old blog, all care of Internet Archive Wayback Machine! The Beta version I’ve linked too included all the images and files I’d directly linked too. I was particularly pleased with that as it allowed me to recover the Parameterised SQL Reports post which would have been useless without those images.

I’ve not moved any of the comments, there weren’t all that many anyway.

Now that job is done I can start working on more new content.

Posted in General | Leave a comment

FAST Custom Refiner on Content Type

I’ve been trying to add a custom refiner to my FAST Search Centre that allows refinement by Content Type.

After a quick search I found this article, Refining on Content Type in SharePoint 2010, however I know a means of allowing the built-in Content Type managed property to be used as a refiner

Add-PSSnapin Microsoft.SharePoint.PowerShell
Add-PSSnapin Microsoft.FASTSearch.PowerShell
Start-SPAssignment -Global

$managedPropertyName = 'Content Type'
$managedProperty = Get-FASTSearchMetadataManagedProperty -Name $managedPropertyName

if($managedProperty -ne $null)
{
	Set-FASTSearchMetadataManagedProperty -Name $managedPropertyName -RefinementEnabled $true
}

Stop-SPAssignment -Global
Remove-PsSnapin Microsoft.SharePoint.PowerShell
Remove-PSSnapin Microsoft.FASTSearch.PowerShell

So after running that, kicking off a full Crawl and then editing the Refiner Web Part as outlined in the Adding Search Refiners in SharePoint 2010 to add a refiner option for Content Type I was able to see refinement options based off the Content Type.

Great…. But there’s a catch….

CT_Refiner

The refiner appears to be only giving me the underlying MIME Type Sad smile

To get a useful refiner based on the SharePoint Content Type, I used the method outlined by Glyn in his article: Create a content type search refinement panel in SharePoint 2010

Posted in FAST, PowerShell | 8 Comments

Adding FAST Keywords and Best Bets via Powershell.

Reading the TechNet docs on Add, edit, remove and display keywords by using Windows PowerShell (FAST Search Server 2010 for SharePoint) was a little confusing. Specifically the important note about making sure that you have a Search Settings Group. When you create or find a Search Settings Group you do so using a –Name parameter. The is nothing there to suggest what this Name parameter corresponds to.

However with a PowerShell hacking I found that this name is actually the GUID of the Site Collection for which you want to add keywords and best bets. Now armed with this info I was able to craft up this PowerShell script to do my bidding.

# SharePoint cmdlets
Add-PSSnapin Microsoft.SharePoint.PowerShell
if([Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")-eq$null){throw "Unable to load Microsoft.SharePoint.dll";}

Add-PSSnapin Microsoft.FASTSearch.PowerShell
if([Reflection.Assembly]::LoadWithPartialName("Microsoft.FASTSearch.PowerShell")-eq$null){throw "Unable to load Microsoft.FASTSearch.dll";}

Start-SPAssignment -Global            # This cmdlet takes care of the disposable objects to prevent memory leak.

#get the Search Settings Group for the Site Collection
$site = Get-SPSite -Identity "http://northwindtraders"
$ssg = Get-FASTSearchSearchSettingGroup -Name $site.ID

if($ssg -eq $null)
{
	#create the ssg if it doesn't exist
	$ssg = New-FASTSearchSearchSettingGroup -Name $site.ID
}

$ssg.Keywords.AddKeyword("snowmobile")
$keyword = $ssg.Keywords.GetKeyword("snowmobile")
$uri = New-Object -TypeName System.Uri -ArgumentList "http://northwindtraders/products/snowmobiles/pages/snm881.aspx" 
$keyword.AddBestBet("SNM881", "A large sled with plenty of storage", $uri)
$uri = New-Object -TypeName System.Uri -ArgumentList "http://northwindtraders/products/snowmobiles/pages/snm313.aspx" 
$keyword.AddBestBet("SNM313", "Our most powerful sled. Extreme Power!", $uri)

Stop-SPAssignment -Global

Remove-PsSnapin Microsoft.SharePoint.PowerShell
Remove-PSSnapin Microsoft.FASTSearch.PowerShell

Posted in FAST, PowerShell, SharePoint | 7 Comments

Powershell: Getting a useful error message

So I’ve been writing a lot of Powershell scripts lately. Particularly with respect to SharePoint config and SQL Backup & Restores.

Running some of these restores has been problematic and I’d quote often see this unhelpful error.

Exception calling "SqlRestore" with "1" argument(s): "Restore failed for Server 'demo2010a'. "
At C:\Scripts\RestoreDB.ps1:104 char:23
+ $smoRestore.SqlRestore <<<< ($server)

So what really went wrong here?
Enter this command:

$error[0]|format-list –force

Basically the errors that have occurred are put into a stack and this command allows you to reference then array style where the 0th element is the most recent error and format-list –force outputs a full stack trace of the exception that occurred.

In my case I didn’t have exclusive access to the database which is required for a restore, so I was able to add this line to me restore script:

$server.KillAllprocesses($dbname)

Hope you find this useful

-Gavin

Posted in Uncategorized | 1 Comment

And, we’re back…..

Kinda…..

My old site is gone and dead, I’m hoping to rescue the posts out of a backup I have kicking around and port them into this one.

As for this site, it’ll be delivering new content in the same vein as my first blog site. Hopefully I’ll be able to provide some new insights into the world of SharePoint and associated technologies.

-Gavin

Posted in News | Leave a comment

Lake Louise Life

For those of you who were unaware I am now living in Lake Louise, Alberta, Canada 😀

My current job is working in the Mountain Operations department for the Lake Louise Ski Area as a part of the Trail Crew team. One of the long time staff members Chris Mosely has set up his own blog on the inner working and happenings of Lake Louise, it is the Lake Louse Lowdown: http://lakelouiselowdown.wordpress.com/

So check it out and drool over the lovely place I’m getting to live work and play in 😀

Posted in General | Leave a comment