Copying a set of Folders via Powershell and Robocopy

September 22, 2011

I wanted to move some files from one location to another. All of the folders started with a set prefix. For example, “Backup One”, “Backup Two”, “Backup 2011-09″, etc. I wanted to use Robocopy because of it’s “restartable” mode. I’m still quite new to Powershell so I figure I better blog this one as I’m sure I’ll need it, or something similar, again.

In my sample below, “ParentFolder” is the parent folder (duh) of the “Backup” folders above. Therefore, we have something like this:

D:\ParentFolder\Backup One
D:\ParentFolder\Backup Two
D:\ParentFolder\Backup 2011-09

The command:

[code lang="powershell"]
Get-ChildItem D:\ParentFolder -Filter "Backup*" -Name | ForEach-Object { robocopy "D:\ParentFolder\$_" "X:\$_" /Z /S /MOVE }

  • -Filter parameter only returns those items that start with “Backup”. This will probably return files too. I didn’t have them, so it wasn’t a problem, but I should probably figure that out too.
  • -Name parameter only shows the folder name, nothing else.
  • Iterate over each item returned and perform the robocopy command, moving all the files and subfolders to X:.

After the command runs, you should end up with, along with all of the files within, of course:

X:\Backup One
X:\Backup Two
X:\Backup 2011-09

Deleting a SharePoint List using Powershell

August 24, 2011

When I’m doing development tasks, there are times I need to quickly create and delete SharePoint content. Lists are one of those. Documenting a few different ways to delete a SharePoint 2010 list using Powershell.

Two lines:

[code lang="powershell"]
$w = Get-SPWeb "http://sps2010"
$w.Lists.Delete([System.Guid]$w.Lists["My Custom List"].ID)

One line:

[code lang="powershell"]
Get-SPWeb "http://sps2010" | Where-Object { $_.Lists.Delete([System.Guid]$_.Lists["My Custom List"].ID) }


[code lang="powershell"]
Get-SPWeb "http://sps2010" | % { $_.Lists.Delete([System.Guid]$_.Lists["My Custom List"].ID) }

Event Receivers

January 26, 2010

Two excellent resources for ramping up on SharePoint 2007 event receivers.

IIS7 Application Pool Reset

January 14, 2010

In IIS6 on Windows Server 2003, we could recyle the application pool that hosted our SharePoint site in order to force the web application to perform a JIT compile. This would then reload any DLLs it required. This saved a considerable amount of time over an IISRESET.

That command is:

[code]C:\windows\system32\IISApp.vbs /a "SP Site App Pool" /r[/code]

(Note that the first time you run IISAPP.vbs, you’ll get a few confirmation prompts.)

This VBS script does not exist in IIS7 on Windows Server 2008, but the good news is there is an even better command:

[code]c:\windows\system32\inetsrv\AppCmd Recycle AppPool "SP Site App Pool"[/code]

Type “AppCmd /?” for a list of all the useful actions you might want perform against your IIS7 installation.

SPC09 Recap

November 1, 2009

Better late than never…

It’s about time I wrote about the experiences of what was the SharePoint Conference, 2009 edition, in Las Vegas. By now many others have had their recap, and with my own experiences being somewhat similar, I shall try to keep mine succint, but entertaining.

My first major Microsoft conference was the Office/SharePoint conference in Redmond (or was it Bellevue) in March 2005. I had just started with Habañero Consulting Group, had just gone through a nasty learning experience on a custom “workflow” type issues and tasks solutions on SharePoint 2003 (which was NOT fun for many reasons), and so was pretty wide-eyed and overwhelmed when I attended the sessions. Aside from the two co-workers I went with, I knew no one else, didn’t know any of the “big names”, and didn’t really have any sort of game-plan whatsoever. We heard a lot of ideas, I saw some code, saw some bad presentations, and concluded the conference with pretty much a “meh”. The most exciting part was the road trip from Vancouver in my car, and I’ll never forget the “thud” that my car had to endure as I drove it off a curb because my “navigator” (she will remain nameless, for now…) missed the exit, it was dark, and I was desperate to turn around and find the hotel.

SharePoint Conference 2008 came to Seattle in February, if i recall correctly. Due to the success the product had in 2007, it was going to be a big deal for SharePoint 2007, with which I’d been working since the summer of 2006 (Beta 2). Having spent two years prior on MCMS 2002, I was fortunate to be involved in a WCM solution in the summer of 2007. By then, I had learned a lot through discovery, but also through reading the content of others. I also attended the first running of Andrew Connell‘s WCM401 online class. I had a bit of a game plan this time, but it mostly involved the sessions, attending them as strategically as I could, trying to learn things outside of my SharePoint comfort zone. I learned some new things, saw better presentations, but generally didn’t feel as out of place as I did in 2005. I attended SharePint, met a few people throughout the conference, but pretty much stayed with my co-workers and didn’t realize the social aspect of these conferences. Since the two MOSS exams were available for free, I decided to give them a shot and passed both. Cool. The stay at the W was also a nice luxury that Habanero generously provided.

Throughout 2007 and 2008, I was focussed primarily on SharePoint projects. I learned more and more with each one (as one should), became more confident, and looked forward to the next conference. Soon after it was announced, I bought an early-bird ticket. Throughout 2009, I grasped on to Twitter, started following a few SharePoint people, and once in a while would engage in some sort of “conversation” with people whom I’ve never met. It was very cool. Heading down to Las Vegas for SPC09 I decided I would try to meet as many people as I could, to personally thank them for the work they have put into the community over the last few years from whom I’ve benefitted directly as well. I decided to splurge a bit and stayed at THEhotel, attended SharePint at EyeCandy, and throughout the conference just shook hands and tried to track down a few people to say “hi” and “thanks”. I had never been to Vegas before, but it wasn’t until Tuesday evening (after the 80s party with Huey Lewis and the News) that I even stepped outside of the Mandalay Bay Center.

It was a great conference with a lot of content (overload), but what made it really special for me was the chance to meet many of the cool people in the SharePoint community of which I’m proud to be part of.

SharePoint Conference 2009

October 17, 2009

Well, like about 7000 other people from around the world, I’ll be at the SharePoint Conference in Las Vegas this coming week. I’m excited about it and looking forward to meeting many new people. I’ll also be looking to thank some great community contributors to whom I am very grateful and appreciative.

I’ll be leaving Vancouver mid-morning on Sunday, arriving by about 1:30 or so. Hope to see you there!

I'll be at SPC09

I'll be at SPC09

New Blog

September 30, 2009

Finally selected a web host for my new domain, installed WordPress, upgraded it to 2.8.4, and installed a new theme (custom one under development).

Wasn’t so bad, quite nice and easy actually. Next up, moving my old blog postings over to here. Any posts with a date earlier than this one are posts the old blog (which explains why this “New Blog” post appears “in the middle”).

Registering a CSS File in SharePoint

June 16, 2008

I have a custom publishing site definition. It was, of course, based off the default publishing site definition. I re-organized some features around, and added custom versions of required features just so I could control everything a little more to my liking. Anytime we do this, however, we run the risk of “what did I break?” syndrome.

One of my clients was using the Content Editor Web Part on one of her page layouts (instead of using a RichHtmlField – long story). She noted that she could not select the “Use Predefined Table Format” in the Table Editor web dialog. Hmmm… I didn’t do anything specific to disable that capability, so I needed to do some digging.

Since I was using a custom publishing site definition, I needed to determine if this behaviour was happening with the default Publishing Portal definition. Sure enough, adding a CEWP to the home page of the Publishing Portal yielded the same effect: I could not use the predefined table formats. I then check the Press Releases default page, which does have a RichHtmlField for me to play with. I can use the predefined formats here. I proceeded to add a CEWP to the zone at the bottom of the page, and here I can also use the predefined formats. Interesting…

I fired up my favourite search engine, and came across this posting from Microsoft: How to enable the “Use Predefined Table Format” setting when you use a Windows SharePoint Services template in SharePoint Server 2007. Cool! But note the location of the CSS: /_LAYOUTS/1033/STYLES/HtmlEditorTableFormats.css. In my case, I have the French language pack installed, which means I have to account for the 1036 LCID also.

I checked the master pages and page layouts that PublishingLayouts feature use, and note that there is not a reference to that CSS. Nor is it in the page layouts found in the PublishingResources feature. What gives? What’s the correct syntax?

No matter. I’ll just add a link to the CSS by building out the path to the right LCID folder, using something like System.Threading.Thread.CurrentThread.CurrentUICulture to get the LCID. After some frustrations (which I won’t get into here), I quickly realized that this would not work in my scenario (Canadian English is not 1033) either.

I looked at the source code for my out-of-the-box publishing portal (the Press Releases page). I noticed that core.css also comes from the LAYOUTS/1033/Styles folder. Some more digging, and I realized that I did not need to specify the full path, like this example taken from BlueBand.master:

[code lang="html"]<SharePoint:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/~language/Core Styles/controls.css %>" runat="server"/>[/code]

but instead I needed simply this:

[code language="html"]<SharePoint:CssRegistration name="HtmlEditorTableFormats.css" runat="server" />[/code]

and SharePoint takes care of the rest, building out the path to the appropriate Styles as required, like this:  

[code lang="html"]<link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Htmleditortableformats.css?rev=guYGdUBUxQit03E2jhSdvA" />[/code]

Here is a great post which talks about what’s going on, and what renders HtmlEditorTableFormats links. It explains why I could not find it in any of the master pages or page layouts: the RichHtmlField web control inserts CSS links to HtmlEditorTableFormats.css and HtmlEditorCustomStyles.css OnLoad() . <groan! />

ContentTypeID Syntax/Structure

February 6, 2008

There is a more efficient way to do content type IDS than we have done in the past:

Say we wanted to inherit from “Page”, our content type used to look something this (I’ve split up the lines so it will display properly):


Meaning, inherit from “Page” (the red part), include a delimiter (00), and then append a GUID.

If we then wanted to extend this content type, we could do this:


And so on… You can imagine how hairy-scary the ContentTypeIDs can get if you wanted your inheritance to go a few levels deep.

As per the article, there is a more efficient way; the second ContentTypeID above could become:


Where the tail 01 is the extended content type. Thus, we could have:



And then to extend from either of these:



IMO, this makes the ContentTypeIDs much easier to work with (and read), and we run less risk of running into URL length issues within SharePoint.

PS. The GUIDs used in the above examples have been changed to protect the coder. Any resemblance to any existing ContentTypeIDs are purely coincidental.

Custom Web Parts can “break” Variations process.

October 24, 2007

Ok, the title is vague, but there’s only so much I can put in a title, right? ;)

As per MSDN’s documentation for the Microsoft.SharePoint.WebPartPages class and Andrew’s post on v3 web parts, we should now inherit from System.Web.UI.WebControls.WebParts.WebPart when we are building custom web parts, except in the four scenarios he mentions (repeating here for my own records):

  1. Cross page connections
  2. Connections between Web Parts that are outside of a Web Part zone
  3. Client-side connections (Web Part Page Services Component)
  4. A data caching infrastructure that allows caching to the content database

However, I came across a situation recently whereby a very simple custom web part (all it did was render a “Get Adobe Acrobat” link, essentially) threw a SharePoint error to the screen when used in conjunction with Variations. Here’s the steps to replicate:

  1. Create your custom web part, inheriting from System.Web.UI.WebControls.WebParts.WebPart. Keep it simple; “Hello World” is fine.
  2. Build/deploy, etc. Use it in one of your page layouts. Of course, Variations has to be set up/enabled, but turn off automatic creation of target pages.
  3. Once the new page has been created, select either “Update Variations” from the Tools menu.
  4. SharePoint will start a long running operation, and then eventually throw an error to the screen.
  5. The target variation page is actually created, but this error message is annoying, and probably discomforting to the authors.

It took me a while to figure out what was causing the problem. I finally had a thorough look in the SharePoint logs, and found the message that told me the cause. If you are using SpsDev’s ULS Log Reader, filter by Area:CMS, Category:Publishing, or just look for the message “Unable to cast”. The relevant part of the message is:

Unable to cast object of type '******.Website.SharePoint.WebParts.AdobeLinkWebPart' to type 'Microsoft.SharePoint.WebPartPages.WebPart'

Um, what? Why does it want to do that? So…

  1. Re-write the web part to inherit from Microsoft.SharePoint.WebPartPages.WebPart.
  2. Create a new DWP file to match.
  3. Deploy, etc; re-test.
  4. Of course, it works without the error.

Microsoft’s own web parts that inherit from System.Web.UI.WebControls.WebParts.WebPart don’t seem to have the same behaviour. Nevertheless, I believe this to be a bug, but one that you should know about when using custom web parts in conjunction with Variations.

Powered by Wordpress and MySQL. Theme by Shlomi Noach,