Monthly Archives: November 2012

Reading a text file from SharePoint Document Library

Was asked to set up a quick text file import into a list – I needed somewhere to shove the test text file so I put it into a Document Library and then realised I needed to be able to read it from there.  Simple, once you know how.

The key thing to remember is that you pull the file using its full URL, so http://mysite/mysubsite/library/folder/document.txt is required rather than grabbing the Document Library and

var debugText = new List<string>();
string url="http://mysite/mysubsite";
string docLibrary = "Shared Documents";
string fileName = "mytextfile.txt";
using (var site = new SPSite(SPContext.Current.Site.Url))
  using (var web = site.RootWeb)
    var targetFile = String.Format("{0}/{1}/{2}", url, docLibrary, fileName);
    SPFile file = web.GetFile(targetFile);                                           
    StreamReader reader = new StreamReader(file.OpenBinaryStream());
    while (!reader.EndOfStream)
      var line = reader.ReadLine();
      //do stuff

Hat tip: Gitolekha

Extracting WSPs from MOSS

Currently in the middle of a MOSS 2007 -> SharePoint 2010 migration and there is a lot of cruft in the existing configuration.  I think that a lot of it can be removed, but you never actually know until you have migrated successfully.  Since time is of the essence and I could waste of lot it rewriting WSP files and solutions that aren’t needed, it is much more useful to try extracting the existing solutions out of SharePoint.  I had done this many moons ago as part of an upgrade from SP1, but it required a Console App and some custom code.  Thankfully, the MOSS farm was on SP2 and the server had Powershell installed.  A swift Googling led me to the following Powershell script:

Start-Transcript "c:\wsp\transcript.txt"
$solutions = [Microsoft.SharePoint.Administration.SPFarm]::Local.Solutions;
foreach ($solution in $solutions) {
   $solution.SolutionFile.SaveAs("c:\wsp\" + $solution.Name);


Which works perfectly.  So massive hat tip to Ben Leach.

PowerShell is swiftly becoming one of the best things Microsoft ever did.

Heads Up: Apress E-book Offer

Just got an email from Apress about their Cyber Monday book sale.  All Apress e-books are down to $15 for one day only, which is about a tenner in the Queens Own English Pounds.

I have a couple of Apress books and I can recommend them.  Especially at that sort of price, you really can’t go wrong.  The sale is a one day thing, and is Monday 26th November, so stick a date in the diary.