Adventure in SPWonderland

Take apart and put back together

NAVIGATION - SEARCH

Presenting at the UK PowerShell User Group 22 March Meeting

I’m presenting at the next UK PowerShell User Group meeting on March 22nd doing an updated talk around using PowerShell with SharePoint featuring an intro to PowerShell,  Demos and roundup with SharePoint/PowerShell gotchas and 'tips and tricks'.

The talk should be be of interest of anyone who might like to talk to a managed API with Powershell.

I’m doing the first talk and Richard Siddaway is doing the second session with a talk on using ActiveDirectory with PowerShell.
If you need an intro to using PowerShell or want or ask more advanced questions this will be a good place to be.

Location:
Memphis Room
Microsoft Building 3
Thames Valley Park
Reading

Registration 18:00
Start           18:30

You need to register for attendance so follow the instructions in this link: http://www.culminisconnections.com/sites/get-psuguk/Lists/Events/DispForm.aspx?ID=2

Hope to see you there.

SharePoint European Conference highlights

I've posted a short piece about the SharePoint European conference on the SUGUK blog, reproduced here

SharePoint European Conference

This is the first SharePoint specific conference I’ve been to and I was impressed by the amount of interest shown by the European’s, with over 2000 attendees from 50 countries and with the UK providing around 160 delegates it was very busy, it seems that even MS was taken by surprise at the amount of interest.

The organisation was of a high standard as you’d expect from a conference hotel, lots of staff and lots of food the only omission as Eric Shupps has already mentioned being the lack of free WIFI access and the really slow speed of the paid for one, they were plainly not geared up for a WIFI swarm drain of techies popping open their laptops and trying to surf the web.

Most of the sessions were of a high standard with speakers either from Microsoft or their partners, the only real dud I attended was the business intelligence one which fell completely flat, poor content and speakers meant I ended up walking out something which I never normally do.

Kudo’s to Steve Heaney of Nintex who did 30 minutes of workflow coding in Visual Studio 205 and had it compile and run without errors, I think he was more surprised at this than anyone.

Here’s my take on a couple of sessions that opened my eyes to new features of the Office 2007 suite.

OpenXML

This session by Peter Koen described the OpenXML format of the new Office 2007 programs Word, Excel and PowerPoint. It showed how easy it is to modify existing document using the Packaging API in .Net 3.0 and pointed out how powerful this could be in conjunction with List Events and Workflow. The power of this API is that you do not need to automate any of the Office programs you just deal with the file itself, this is very important in server side code as most SharePoint code tends to be.

 A couple of examples, lets say you have a Document library called Draft and one called Confidential, what you would like is for all documents that are placed into these document libraries to have a watermark applied that says either DRAFT or CONFIDENTIAL applied to them. With list events and some fairly simple code this should be pretty easy to do.

Or lets say you have 100’s of documents in various document libraries that you send out to clients on a regular basis and each one has your logo in it along with your company name and details in the footer of each one, lets say you undergo a re-branding exercise or an office move, you are now faced with opening each one of those documents and changing the logo and footer by hand or perhaps automating Word with VBA. With the OpenXML format you can crack open the Docx file and with a few lines of code manipulate the parts of the document you need to, it would also be an order of magnitude faster than calling Word.

The one current hassle is you have to deal with the Word XML directly, there is no API available that maps say a Word object model onto the XML needed but apparently that is on the development timeline.

http://openxmldeveloper.org/default.aspx

 Groove

This was a really good session by Mark Ryan of Microsoft, clearly someone who has been there and got the T-shirt.

Groove is a program I’ve ignored up to now but this session makes clear its not something that can be ignored for much longer, it just too useful a program.

Groove supports offline distributed and replicating workspaces containing lists, discussions, files and custom data form. It basically allows you to do offline collaboration in the same way as SharePoint allows online collaboration.

The integration with SharePoint is limited at the moment as it currently only supports offline documents from document libraries with no support for lists, but if the Groove team gives the SharePoint team some ‘love’ this should improve in the next version.

As Groove is new to the Office stable it does not have the .Net/Visual Studio integration that it possibly should have and although you can embed InfoPath forms to create data entry forms Mark recommends to stick with Groove forms for now.

 One scenario mentioned was for offline workgroup collaboration on a design project involving images but perhaps the most interesting scenario is crisis management.

Picture this: you have a physical disaster at your main office, lets say a flood, all company VPN and mail communication through the head Office is down, with Groove you could have a nominated person who holds a predefined workspace containing the company contact list along with the disaster recovery plans and checklists, that user can quickly invite other needed parties into the workspace and as long as people have an internet connection they can stay informed and up to date on all activities as the company works through the steps to get back to normal. Even if you don’t use Groove on a day to day basis in a case like this it would be worth its weight in gold.

 Definitely check Groove out, http://office.microsoft.com/en-gb/groove/default.aspx

 Looking forward to the next one, don’t know when it will be but one thing’s for sure they are going to need much bigger venue.

 

UK SharePoint User Group Meeting March 7th

The UK SharePoint User Group meetings are coming thick and fast, the next one is being held on 7th March in Ullesthorpe, Leicestershire.

This should not be missed as it features 3 speakers, Andrew Woodward, Bill English and Todd Bleeker.

I've seen both Bill and Todd speak and its an experience ;-)

Put your name on this thread to signup for the meeting http://suguk.org/forums/2300/ShowThread.aspx#2300

 

SharePoint/PowerShell 8: The one with the Contact Web Part

 

Following on from the work in my previous post where I set the users Picture property in their profile the next step is to add a Contact Web Part to our imported publishing pages. I’m using the default layout page to add the contact Web Part to, this page has the Web Part Zones underneath the content fields but you could use another layout that has a Web Part zones to the right of the content fields and of course you can use SharePoint Designer to create your own layout formats and embed the Contact control without using code.

This is how our publishing pages will look once we have added the Web Part to each page

An interesting feature of a Publishing page is that it has a contact property where you can assign user details to each page, you can either lookup the user from the picker in which case the page will pull the users details from the profile database or you can type in the details yourself.

 

The nice thing about the Contact Web Part is that once you have added it to a publishing page it will automatically pull the picture and the users description (if set in the Web Part) that has been assigned to the page from the profile database.

One bad thing about the part is you only get the choice to put the users name left or right not top or bottom.

So on to the code, I need a function that will take a site URL and then set the default Publishing Page’s contact property and then add the Web Part to the page.

 

 

# Function: Add-ContactWebPart # Description: Adds the Contact User Web Part to a publishing page # Parameters: SiteURL - Server relative URL of the Area # UserName - UserName to show as the contact in domain\user format # # Requirements: Needs to have the System.Web assembly loaded # function Add-ContactWebPart($SiteURL, $UserName) { $comment = "Contact WebPart Added" $site = new-object Microsoft.sharePoint.SPSite($SiteURL) $web=$site.OpenWeb() $user= $web.Users.get_item($UserName) $pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web) $defaultpage=$pubweb.GetPublishingPages()[$pubweb.DefaultPage] $defaultpage.CheckOut() # Set Contact "Setting Contact on " + $pubweb.url + " to " + $user.Name $defaultpage.set_Contact($user) $defaultpage.Update() $webpartmanager=$web.GetLimitedWebPartManager($defaultpage.Url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) $webpart=new-object Microsoft.SharePoint.Portal.WebControls.ContactFieldControl $webpart.ChromeType=[System.Web.UI.WebControls.WebParts.PartChromeType]::TitleOnly; $webpart.Title="Page Contact" $webpart.PicturePosition=[Microsoft.SharePoint.Portal.WebControls.PictureDirection]::Left $webpart.IsDisplayJobTitle=$true $webpart.IsDisplayPicture=$true $webpartmanager.AddWebPart($webpart, "LeftColumnZone", 0); " Checking in page" $defaultpage.CheckIn($comment) # Publish if($defaultpage.listItem.ParentList.EnableMinorVersions -eq $true -and $publishingPage.ListItem.File.MinorVersion -ne 0) { " Publishing" $defaultpage.listItem.File.Publish($comment) } # If moderation is being used handle the approval if ($defaultpage.listItem.ParentList.EnableModeration) { $modInformation = $defaultpage.listItem.ModerationInformation " Moderation on, Current Status: " + $modInformation.Status # Check for pending approval if($modInformation.Status -ne [Microsoft.SharePoint.SPModerationStatusType]::Approved) { " Approving" $defaultpage.ListItem.File.Approve($comment) } } # Clean up $pubweb.Close() $web.Close() $site.Close() }

 

In this code we find the SPUser object for the given username and set the Contact property of the PublishingPage to it. We then add the web Part using the new SPLimitedWebPartManager class. Most of the code is concerned with checking the page out and back in and assumes the page is not already checked out.

An interesting line is

$defaultpage=$pubweb.GetPublishingPages()[$pubweb.DefaultPage]

I'm indexing into the collection return by GetPublishingPages because Powershell doesn't support generics in the current version.

UK SharePoint user group meeting

Don't forget the SharePoint user group meeting taking place tomorrow Feb 8th at Reading.

'Mr SharePoint community' Lawrence Lui is over from Redmond to take us through the Fab 40 Application Templates and lead a discussion on Office 14.

Sign ups and more detail here: http://suguk.org/forums/1/1901/ShowThread.aspx

SharePoint/PowerShell 7: Put the User in the Picture

Now we have the portal setup I want to add a Contact WebPart to each page but before I can do that I have to add a picture for each user I have imported, in order to keep this post a little shorter I’m only going to concentrate on using the UserProfile API’s to set our picture for each user.
(The Contact WebPart is MOSS WebPart that shows a users name and description and optionally their picture)
The field that we are going to set programmatically is accessed on the Users edit profile page as Picture:

Once set this will display the picture on the user profile like this


To do this I need to revisit our User.csv import file and add an extra field that gives the name of the JPG file that holds the users picture.
I’m going to assume that the previous blog post Upload a directory of files in 4 lines has already uploaded the users picture to the SiteCollectionImages picture library, in real life you’d probably use a separate picture library.


Ideally a profile import has occurred after we have added the users to Active Directory and populated the SharePoint profile database.

Now to set the Users Picture property we know to know the Property Name of the Picture Field.
To make this easy to find out here’s the first function for our toolbox Get-SPUserProfileConfigManager.
This function returns a UserProfileConfigManager (http://msdn2.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofileconfigmanager.aspx), note this is the new class that resides in the Microsoft.Office.Server namespace not the v2 obsolete one that lives in Microsoft.sharePoint.Portal.UserProfiles.
Also ignore the sample currently given in the MSDN documentation above, it won’t work as it uses the old classes where you pass a PortalContext to the constructor whereas the new version of the classes take a ServerContext object.

 

# Function: Get-UserProfileConfigManager # Description: return a UserProfileConfigManager object which is used for management of MOSS User Profiles # Parameters: PortalURL URL for the Portal Site Collection # # function Get-UserProfileConfigManager([string]$PortalURL) { # Need to get a PortalContext object # as we do not have a HttpContext we need to source one the hard way $site=new-object Microsoft.SharePoint.SPSite($PortalURL) $servercontext=[Microsoft.Office.Server.ServerContext]::GetContext($site) $site.Dispose() # clean up # Return the UserProfileConfigManager new-object Microsoft.Office.Server.UserProfiles.UserProfileConfigmanager($servercontext) }

 

Once we get the UserProfileConfigManager we can call GetProperties and list the internal and display names for each profile property

$cm=get-userprofileconfigmanager "http://sps:2828" $cm.getproperties() | select name, displayname Name DisplayName ---- ----------- UserProfile_GUID Id SID SID ADGuid Active Directory Id AccountName Account name FirstName First name LastName Last name PreferredName Name WorkPhone Work phone Office Office Department Department Title Title Manager Manager AboutMe About me PersonalSpace Personal site PictureURL Picture UserName User name QuickLinks Quick links WebSite Web site PublicSiteRedirect Public site redirect SPS-Dotted-line Dotted-line Manager SPS-Peers Peers SPS-Responsibility Responsibilities SPS-Skills Skills SPS-PastProjects Past projects SPS-Interests Interests SPS-School Schools SPS-SipAddress SIP Address SPS-Birthday Birthday SPS-MySiteUpgrade My Site Upgrade SPS-DontSuggestList Don't Suggest List SPS-ProxyAddresses Proxy addresses SPS-HireDate Hire date SPS-LastColleagueAdded Last Colleague Added SPS-OWAUrl Outlook Web Access URL SPS-ResourceSID Resource Forest SID SPS-ResourceAccountName Resource Forest Account Name SPS-MasterAccountName Master Account Name Assistant Assistant WorkEmail Work e-mail CellPhone Mobile phone Fax Fax HomePhone Home phone

So from this list I see that I need to set the PictureURL property, to get a UserProfile we first need a UserProfileManager object:

# Function: Get-SPProfileManager # Description: Return a UserProfileManager object which is used for accessing MOSS User Profiles # Parameters: PortalURL URL for the Portal Site Collection # function Get-SPProfileManager([string]$PortalURL) { # Need to get a PortalContext object # as we do not have a HttpContext we need to source one the hard way $site=new-object Microsoft.SharePoint.SPSite($PortalURL) $servercontext=[Microsoft.Office.Server.ServerContext]::GetContext($site) $site.Dispose() # clean up # Return the UserProfileManager new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($servercontext) }

And then a helper function Get-SPUserProfile to obtain the UserProfile object itself:

# Function: Get-SPUserProfile # Description: Return a UserProfile object, this will be created if it does not exist # Parameters: PortalURL URL for the Portal Site Collection # DomainUser UserName in Domain\user format function Get-SPUserProfile([string]$PortalURL, [string] $DomainUser) { $upm= Get-SPProfileManager([string]$PortalURL) if ($upm.UserExists($DomainUser) -eq $false) { $upm.CreateUserProfile($DomainUser) } $upm.GetUserProfile($DomainUser) }

Note that this function will create the UserProfile if it does not already exist.

Now we just need a function that makes it easy to set a single UserProfile property, if you have multiple properties to set it would be best to do them all at once and then call commit.

# Function: Set-UserProfileProperty # Description: Sets a property on a User Profile # Parameters: UserName [optional] UserName in Domain\user format # PropertyName Property to set # PropertyValue Property Value to set # $UserProfile UserProfile object, if using this in a loop this should be set # function Set-UserProfileProperty([string]$UserName, [string] $PropertyName, [string] $PropertyValue, [Microsoft.Office.Server.UserProfiles.UserProfile] $UserProfile) { # If we are not passed a UserProfile object then create it # if ($UserProfile -eq $null) { $UserProfile = Get-SPUserProfile($UserName) } $UserProfile[$PropertyName].Value= $PropertyValue $UserProfile.Commit() }

Note this function can either be called with a pre-created userProfile object or a UserName.

Heres the updated Users.CSV with the Picture Field added at the end  

LoginName, DisplayName, FirstName, LastName, Email, Picture brianb, Brian Ballack, Brian, Ballack, brianb@contoso.com, cowner10.jpg walterf, Walter French, Walter, French, walterf@contoso.com, cowner12.jpg

Now a function to tie this all together, it imports the CSV files, locates the user profile by login name and updates the user’s Picture URL:

function Set-UserPictures([string] $PortalURL, [string] $UserFile, [string] $Domain ) { Import-Csv $UserFile | foreach-object { $name=$Domain + "\" + $_.LoginName; $fullURL=$PortalURL + "/" + $_.Picture; Set-UserProfileProperty $PortalURL $name "PictureURL" $fullURL } }

 And you can make use of all of the above code by running this command:

Set-UserPictures "http://sps:2828" "users.csv" "contoso"

Ok now we're all set to add the Contact WebPart to each publishing page in the next post.

Powershell User Group Meeting

Made the UK PowerShell User Group meeting last night in Wokingham, just, the weather and people having accidents on the M3 and M4 kept me late. Got to try out my new Acer GPS unit based on Windows Mobile 5, worked beautifully until setting the return journey when a fatal exception dialog popped up on route calculation, I had to reboot the unit, having seen that dialog many times developing on WM I just had to laugh.

Nice to meet other PowerShell enthusiasts, kudos to PowerGadgets for supplying the beer and pizza and Global Knowledge for hosting.

The meeting was hosted by all round good guy Thomas Lee along with Richard Siddaway. Thomas gave an interesting presentation on PowerShell installation a lot of which I didn't know.

One point to come out of the discussion was the number of profiles that PowerShell will load when it first starts up. Here are the locations and order the profiles get loaded in, note these files won't exist unless you or your admin has created them

  1. %windir%\system32\WindowsPowerShell\v1.0\profile.ps1  (all users, all shells)
  2. %windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1 ( all users, the Windows Powershell shell)
  3. %userprofile%\My Documents\WindowsPowerShell\profile.ps1  (per user, all PowerShell versions)
  4. %userprofile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 ( per user and the default Windows Powershell shell)

Now you can put code or functions in each of these files but the last loaded file wins if there are any conflicts.

This could be a problem with some companies that would like to define a corporate wide profile for every user and put it in system32\WindowsPowerShell\v1.0, as a user I can override any of the company function definitions with my own code in my own profile. You could lock down the PowerShell %userprofile% files but that might cause other problems.

Someone pointed out at the meeting what was needed is a readonly profile that can be loaded globally and last for every user but hey presto there's a PowerShell team blog out today on making functions read-only http://blogs.msdn.com/powershell/archive/2007/01/25/controlling-powershell-function-re-definition.aspx , now that's what I call service.

 

As the meeting closed Thomas also gave out a USB Pen drive to everyone who turned up loaded with PowerShell related goodies, woo hoo free stuff, and oh yeah I'll be speaking at the next meeting about controlling SharePoint with PowerShell, should happen sometime in March.

 

 

Spreken zie SharePoint?

I just finished finalising the flights and hotels to attend the SharePoint 2007 European Conference in Berlin today when I realised I knew zip about the German language, yes I know the usual German words picked up from films, schnell, bitte etc. but I'd never spoken one single german phrase for real.

Whatever country I go to I try to speak a little of the local language, if nothing else it gives the waiters a laugh, so I needed a quick and easy starters course in German, a little Googling and I found this gem German Podcasts by Stephan Wiesner

This is fantastic, its a series of podcasts where Stephan takes you through a story teaching you German as he goes about a Hans a German progammer who flies into an airport, goes through customs and talks about a conference he's been at!

The story is nicely pitched at the complete beginner and Stephan has produced mp3's, pdfs to accompany and even a video.

The content is also posted at http://www.archive.org which lets you listen on the webpage itself via a Flash plugin.

I wonder can he do a podcast that teaches me how to say: 'What?? you're NOT using PowerShell to script SharePoint?'