It's ok. Be different.

So I'm leaving the company I'm currently at and joining a new one next week. I have personal contacts in my address book but it took me forever trying to figure out how to export my contacts from Outlook 2010. Let's just say the location is counter-intuitive. Anyways, to export your contacts, here are the steps:

Click the Browse button and navigate to the folder you want to save the file to and give the file a file name.

Click the Ok button.

Ensure the appropriate action is displayed and selected in the actions list and click the Finish button.

That's it. I know, not the most helpful or thought-provoking blog post I've ever written but I hope this can help some poor sap like me who doesn't think quite like the Microsoft UX guys do and automatically assume that the most appropriate place for an Export action would be under Open | Import ;)

So I was bored and decided I wanted to learn a little more about the new SharePoint 2010 UI Framework. There are some really cool things you can do with the new framework and I wanted to put some of them to use but at the same time create something that’s useful for other people. I remember a while back someone asking me to write code for MOSS 2007 that would allow users to download files down to their computer as a zip file because they didn’t like having to download each file one by one. For some reason or another, I never got around to it – I think I told the person that they could download multiple files from SharePoint using Windows Explorer and, although it wasn’t exactly what they were looking for, it at least got the job done. But I decided recently that this might not be a bad enhancement to add to the UI in 2010 so that’s what I set out to build today.

The first thing to look at is my Visual Studio 2010 project. I will discuss all the important files that are part of this project. Below is a screenshot of my project:

The first thing I did was to create a new SharePoint 2010 Empty SharePoint Project called DeviantPoint.DownloadZip. Then, I added a reference to ICSharpCode.SharpZipLib.dll which is a part of SharpZipLib(a .NET library that is well-used for working with Zip files). I then created a helper class, ZipBuilder, used to actually create the zip file. I created this helper class with the intent that I could reuse this elsewhere, not just for this project.

Basically, when an instance of this class is constructed, you need to pass in a stream that will be used to write the contents of the zip file to. This could be any type stream (FileStream, MemoryStream, etc). There are a couple of helper methods in this class that allow you to add files and folders and method that “finalizes” the zip file. This Finalize() method must always be called to ensure that the zip file is written out correctly. This class also implements the IDisposable pattern since it is handling streams.

This is the code for the ZipBuilder class:

ZipBuilder.cs

1:using System;

2:using System.Collections.Generic;

3:using System.Linq;

4:using System.Text;

5:using System.IO;

6:using ICSharpCode.SharpZipLib.Zip;

7:using ICSharpCode.SharpZipLib.Core;

8:

9:namespace DeviantPoint.DownloadZip

10: {

11:publicclass ZipFileBuilder : IDisposable

12: {

13:privatebool disposed = false;

14:

15: ZipOutputStream zipStream = null;

16:protected ZipOutputStream ZipStream

17: {

18: get { return zipStream; }

19:

20: }

21:

22: ZipEntryFactory factory = null;

23:private ZipEntryFactory Factory

24: {

25: get { return factory; }

26: }

27:

28:

29:public ZipFileBuilder(Stream outStream)

30: {

31: zipStream = new ZipOutputStream(outStream);

32: zipStream.SetLevel(9); //best compression

33:

34: factory = new ZipEntryFactory(DateTime.Now);

35: }

36:

37:publicvoid Add(string fileName, Stream fileStream)

38: {

39://create a new zip entry

40: ZipEntry entry = factory.MakeFileEntry(fileName);

41: entry.DateTime = DateTime.Now;

42: ZipStream.PutNextEntry(entry);

43:

44:byte[] buffer = newbyte[65536];

45:

46:int sourceBytes;

47:do

48: {

49: sourceBytes = fileStream.Read(buffer, 0, buffer.Length);

50: ZipStream.Write(buffer, 0, sourceBytes);

51: }

52:while (sourceBytes > 0);

53:

54:

55: }

56:

57:publicvoid AddDirectory(string directoryName)

58: {

59: ZipEntry entry = factory.MakeDirectoryEntry(directoryName);

60: ZipStream.PutNextEntry(entry);

61: }

62:

63:publicvoid Finish()

64: {

65:if (!ZipStream.IsFinished)

66: {

67: ZipStream.Finish();

68: }

69: }

70:

71:publicvoid Close()

72: {

73: Dispose(true);

74: GC.SuppressFinalize(this);

75: }

76:

77:publicvoid Dispose()

78: {

79:this.Close();

80: }

81:

82:protectedvirtualvoid Dispose(bool disposing)

83: {

84:if (!disposed)

85: {

86:if (disposing)

87: {

88:if (ZipStream != null)

89: ZipStream.Dispose();

90: }

91: }

92:

93: disposed = true;

94: }

95: }

96: }

The next thing I wrote was SPExtensions.cs, a class for adding extension methods to some of the Microsoft.SharePoint objects. This class basically just adds a few simple helper methods to the SPListItem class and the SPList class. For the SPListItem class, I just added a method to determine if the SPListItem instance is actually a folder and for the SPList class, I added a method to determine if the list is actually a document library.

The code for SPExtensions is below:

SPExtensions.cs

1:using System;

2:using System.Collections.Generic;

3:using System.Linq;

4:using System.Text;

5:using System.Runtime.CompilerServices;

6:using Microsoft.SharePoint;

7:

8:namespace DeviantPoint.DownloadZip

9: {

10:publicstaticclass SPExtensions

11: {

12:publicstaticbool IsFolder(this SPListItem item)

13: {

14:return (item.Folder != null);

15: }

16:

17:publicstaticbool IsDocumentLibrary(this SPList list)

18: {

19:return (list.BaseType == SPBaseType.DocumentLibrary);

20: }

21: }

22: }

The next thing I did was to add a SharePoint Mapped Folder to my project mapping to the Layouts directory located in the SharePoint root. When you add a mapped folder, Visual Studio will automatically create a sub-folder in that mapped folder with the same name as your project. This is a good thing as you don’t want to be mixing up all your project files with all of the files that come out of the box from SharePoint.

After I had my Layouts mapped folder and subfolder created, I added a SharePoint 2010 Application Page item to the sub-folder called DownloadZip.aspx. The purpose of this application page is to actually handle the request from the client to build the zip file and send it back down to the client. Having an application page handle this is the same technique that is used with the ‘Download a Copy’ action button you see in the SharePoint 2010 ribbon. Basically, a POST request from a client is sent to my DownloadZip.aspx page and this page takes care of packaging up a zip file and sending it down to the client’s browser. This page expects two parameters:

sourceUrl –the full url of the document library (and folder, if inside of a subfolder) where the request is being made from

itemIDs – a semi-colon delimited list of the SPListItem IDs that should be included as part of the zip file. Note that folders also have ids so if a folder is selected, that folder’s id would also be sent.

The code-behind for this application page basically takes the list of item ids and for each item id, goes and grabs the corresponding file from the document library in SharePoint and, using the ZipBuilder class, packages it up as a zip file. If one of the items that was selected is actually a folder, it will create that folder in the zip file as well and put all the items that are in that SharePoint folder into the corresponding zip file folder. It will also traverse through all the sub-folders in the hierarchy.

Below is the code-behind for the DownloadZip.aspx application page (there is nothing I added to the Download.aspx file itself):

After creating the application page, I added a SharePoint 2010 Empty Element item called DownloadZip to my project. This is nothing more than an Elements.xml file that takes care of adding my custom action to ribbon (CustomAction.Location=”CommandUI.Ribbon”). By default, for document libraries, this is what the ribbon looks like:

I wanted to add my action inside of the area in the Documents tab, inside of the Copies group so to do this, for the CommandUIDefinition, I set the Location attribute to "Ribbon.Documents.Copies.Controls._children”. I also wanted it to appear right after the Download a Copy action so for the Button element’s Sequence attribute, I set the value to 15 (the Download a Copy button has a sequence of 10 and the Send To button has a sequence of 20 so I needed to set the sequence of my button to something in between). To understand where everything is placed and what the sequences are by default, you need to look at the file C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\GLOBAL\XML\CMDUI.xml. I also specified the icons I wanted to use for my action (these icons are also part of my project, located in a sub-directory of the Images SharePoint mapped folder) and I also set the TemplateAlias to “o1” so that my icon shows up large like Download a Copy does. I also define the actual command handler in this Elements.xml file by adding a CommandUIHandler element. The CommandAction attribute is used to specify what exactly the button is supposed to do and the EnabledScript attribute is used to determine whether or not the button/command is enabled. These two attributes’ values both point to javascript functions I define in a separate file (discussed later). Because I’m using a separate javascript file, I also have to add another CustomAction element in the Elements file that points to the location of my javascript file. This is the result:

Finally, I created the CustomActions.js file. This file is used to define the actions/behavior of my new ribbon button. The enable() function is used to determine whether or not my button is enabled. If there is at least one item selected, then my button is enabled. The downloadZip() function just starts off the download process. Actually, I could have probably written the javascript so I didn’t even need this function or calls to SP.ClientContext.executeQueryAsync() but I was just trying to get something done quickly and actually writing it this way gave me another place to show-off another one of the UI features, the Status. If the call to SP.ClientContext.executeQueryAsync() fails, then the onQueryFailed delegate is executed. The onQueryFailed() function uses the SP.UI.Status to display the error message, shown here:

The function onQuerySucceeded() is where the majority of the action happens. I use the SP.ListOperation.Selection object to get a list of the selected items. I then create a request to my DownloadZip.aspx application page and send that page the list of selected item ids as well as the current url (the url of the page the user is on). Like I said earlier, that application page takes care of packaging everything up as a zip and streaming it down to the browser.

I wrote my very first PowerShell script today which I wanted to share. I’m definitely no PowerShell guru; in fact, the only times I’d ever used Powershell before is to run a script someone else has written. I have never written even one line of a PowerShell script before but I had to do so today. Basically, I needed to write something to quickly turn all my MOSS 2007 site collections into read-only mode. Since we’re talking about hundreds of site collections here, there was no way I was going to do this manually. In Service Pack 2 for WSS/MOSS, the setsitelockSTSADM operation was added to allow you to specify a lock type on a site collection. I knew I could use this and combine it with the enumsitesSTSADM operation to quickly turn my site collections into read-only mode and then later turn it back into ‘normal’ mode.

Below is my script. Remember that again, this is my first PowerShell script and I needed to get it done quickly so be gentle in the critique ;).