8/02/2013

PowerShell to the rescue (again!)

 

Using PowerShell to replace text in a file.

I have a custom class next week. I need to make a little fix to the lab files… actually 526 .css files and 380 .htm files need to be updated with several fixes in each. Lots of little fixes.

 

How did I know how many files?  PowerShell of course…

(Get-ChildItem *.css –Recurse).count

 

Find and replace some text…

First update was to fix some URLs. I needed to remove a backslash at the start of every "href=" and "src=".  A little PowerShell could also do all of the file edits!

$from = 'href="/'
$to = 'href="'

Get-ChildItem *.htm -recurse |
%{
     $name = $_.fullname; Write-Host $name; (Get-Content $name) |
     %{ $_ -replace $from,$to } |
     Set-Content -path $name –force
}

I wrapped the above line in a function, called it eight times with my eight replacements, and it was done in seconds!

 

Notes:

Find all of the files…

  Get-ChildItem *.htm -recurse

and run some code on each one:

   %{   }

Save the file path to a variable, display the file path and then get the file's contents:

   $name = $_.fullname;
   Write-Host $name;
  (Get-Content $name)

The pipe then has the text to be replaced, so let's do the replace:

   %{ $_ -replace $from, $to }

And write the replaced text out to a file with the same name:

   Set-Content -path $name –force

 

.

6/03/2013

Free SharePoint 2013 Webinar

 

Tomorrow I will be giving a one hour webinar with MAX Technical Training on what's new in SharePoint 2013. This will include both the what's new bullet points, and a lot of little things I've found over the last year or so. Grab a sandwich and join me for a SharePoint lunch and learn!

Tuesday, June 4th from 12-1 pm
   
Reserve Your Webinar Seat Now

What's New in SharePoint 2013 - Features and User Interface

SharePoint 2013 is now in its 5th generation. While 2013 appears to have changed dramatically you will find that it is an interesting mix of unchanged features that you and your users already know, and a lot of new functionality that you will be exploring for the next few years.

In this webinar we will try to reduce the "fear of change" by showing you what has not changed while also showing how many new features and changes will add value to your world of information management and sharing.

Our presenter, Mike Smith, is a SharePoint Microsoft Most Valuable Professional (MVP), Microsoft Certified Trainer (MCT), Senior Technical Instructor with MAX Technical Training and author of the book: SharePoint 2007 and 2010 Customization for the Site Owner.   

.image

6/02/2013

SharePoint Versioning Trivia, or maybe a start on a versioning FAQ

 

The number of versions retained is one more than the limit you set. Entering 10 as the version limit will keep up to 11 versions of the item. I guess the current "version" is not counted as a "version".

image

Then maximum number of versions per item is 5000. (at least in SP 2010)

image

We you exceed the version limit the oldest item is immediately discarded. It is not moved to the recycle bin.

Only libraries support Minor or Draft versioning. Lists only support major versions.

The maximum number of draft versions is 511, and you can't change that number. Attempting to create a 512th draft version generates this error:

image

"Keep drafts for the following number of major versions" will automatically discard minor versions of published major versions over the entered limit. Actually… it will keep one less than you probably expect.

image

For example, if the limit is 3 and you have some unpublished drafts, then you will see drafts from the previous 2 published versions plus the current drafts:

image   (click image to enlarge)

Notice that you see 13.1, 14.1, 15.1 and 15.2, but not 12.1. Three sets of drafts as expected. Now let's publish 15.2 into 16.0:

image(click image to enlarge)

Now we only have two sets of drafts, 14.1 and 15.1.

 

Clicking "Delete All Versions" actually deletes all but one version. The latest version is retained.

Reducing the version limit will not automatically delete excess old versions. The excess old versions will be deleted the next time the item is deleted. (You can create a PowerShell script to clean out the excess versions.)

Visitors to the site can see all of the old versions by default. You may want to edit the Read and View Only permissions levels to remove the "View Versions" permission.

Major version numbers are assigned internally using “version number” times 512. You can see this when display a version of a list item. Example of a direct URL: http://intranet /Lists/Announcements/DispForm.aspx?ID=1&VersionNo=1024
No that’s not version 1,024! Version numbers are assigned using “version number” times 512. Version 1 is 512, version 2 is 1024, version 3 is 1536 (i.e. 3 * 512), version 4 is 2048 (i.e. 4 * 512) …

 

Notes for developers and PowerShell scripters:

Versioning related properties of SPListI:

  • .MajorVersionLimit – the "Keep the following number of major versions" field in Versioning Settings
  • .MajorWithMinorVersionsLimit - the "Keep drafts for the following number of major versions" field in Versioning Settings

Versioning related properties of SPListItem:

  • .Versions – a collection of retained versions
  • .Versions.Count
  • .Version[0]  gets the most recent version of the item
  • .Verions[x] where x is .Versions.Count, is the oldest version of the item (i.e. versions are stored in reverse order)
  • .HasPublishedVersion   -  boolean

 

Here's a little PowerShell script that will take the first document (ID=1) in the Shared Documents library and create 10 major versions, each with two minor versions.

# before running this, enable major and minor versioning on the library

# get the web site
$web = Get-SPWeb http://intranet.contoso.com

# publish the first document 10 times
for ($versionsToCreate=0; $versionsToCreate -lt 10; $versionsToCreate++)
{
  # get the document
  $doc = $web.lists["Shared Documents"].getitembyid(1)

  # create two minor versions
  for ($x=0; $x -lt 2; $x++) { $doc.update() }

  # publist the last minor as a major version
  $doc.File.publish("")
}

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.