Archive | March 2010

My PowerShell ISE WishList

This will probably be a Part I of several. As the SharePoint community becomes more versed in PowerShell, the more folks are going to demand of it, and the ISE (Integrated Scripting Environment). Here are some features I wish Microsoft had incorporated into the PowerShell ISE.

1. Find in Files (from Visual Studio)

2. Projects (from SSMS, SQL Server Management Studio)

SharePoint 2010 Developer Exams Announced

Microsoft released some exam information on SharePoint 2010, but only for developers. ITPros, I guess you’re going to have to wait.

Check out the Training Page – at the bottom you’ll see an exam list.

Exam 70-573 TS: Microsoft SharePoint 2010, Application Development

Exam 70-576 PRO: Designing and Developing Microsoft SharePoint 2010 Applications

For IT Pros, Exams Exam 70-667, 70-668 don’t have pages.

Overall Exam List

Get a SharePoint Site Collection Storage List for PowerShell

I wanted to see the breakdown of my site collections’ size. And this one’s pretty easy.

Here’s the command I used:

 foreach ( $site in get-spsite ) {
write-host $site.url -  $site.Usage.storage }

Here’s the output:

SharePoint Listing of all Lists in a Farm or a Site Collection using PowerShell

Here’s how you can view all the lists within a farm, even hidden lists like the TaxonomyHiddenList. Now, if you have a large farm, you obviously aren’t going to display all lists. You may, however, still want to display all lists in a site collection. That’s covered at the end of the post.

PS> foreach ($site in get-spsite) { 
foreach ($web in $site.AllWebs) { 
foreach ($list in $web.lists) 
{write-host $site.url - $web.Title - $list.Title 
} } } 

Great. Now let’s get something more. But what? What do we have to choose from? Well, here’s how to find that out.

And our output should look something like this:

You can run these 2 commands. The first stores all site objects into memory. The second then uses that memory to get the members of objects in memory.

PS C:\Users\david.frette> $sitelists = foreach ($site in get-spsite) {
 foreach ($web in $site.AllWebs) { 
foreach ($list in $web.lists) { 
$list } } }
PS C:\Users\david.frette> $sitelists | get-member

Then you’ll get this output! (Screen shots truncated)

What we see are the members of two different objects, the SPList and the SPDocumentLibrary.

Let’s choose a report that displays the Title, Hidden, and ItemCount. Our statement should look something like:

PS C:\Users\david.frette> $sitelists | select Title, Hidden, ItemCount

So that’s it in a nutshell: a quick listing of all lists in a farm. Now, if you just want a listing of all lists in a site, here’s what you want. Three commands.
The first sets a reference to your specific site collection.
The second places each list into memory (sitelist variable).
The third displays a report.

PS C:\Users\david.frette> 
$site = new-object Microsoft.SharePoint.SPSite("http://demo9/sites/BI")

PS C:\Users\david.frette> 
$sitelists =  foreach ($web in $site.AllWebs) { 
foreach ($list in $web.lists) { $list } }

PS C:\Users\david.frette> 
$sitelists | select Title, Hidden, ItemCount

How to determine PowerShell Version

I wanted to check my PowerShell version information. Perhaps I’ll need to do so in future scripts. Here’s how to determine the PowerShell Version from within PowerShell.

PS C:\Users\david.frette> $host.version

Major Minor Build Revision
—– —– —– ——–
2 0 -1 -1

PS C:\Users\david.frette> $host.version.major
2
PS C:\Users\david.frette> $host.version.minor
0
PS C:\Users\david.frette> $host.version.build
-1
PS C:\Users\david.frette> $host.version.revision
-1

Reading Term Values from the Managed Metadata Service

The DLL is “Microsoft.SharePoint.Taxonomy” but it refers to the Managed Metadata service, which includes the Taxonomy. The following code seems logical for writing out all the term values. However, that’s not the case. It’s just a start.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace MossDelight.Demos.ManagedMetadataConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSite site = new SPSite("http://demo9/sites/MMS");
            TaxonomySession txsn = new TaxonomySession(site);
            foreach (TermStore termstore in txsn.TermStores)
            {
                Console.WriteLine(">>> " + termstore.Name);

                foreach (Group group in termstore.Groups)
                {
                    Console.WriteLine("+ " + group.Name);

                    foreach (TermSet termset in group.TermSets)
                    {
                        Console.WriteLine("++ " + termset.Name);

                        foreach (Term term in termset.Terms)
                        {
                            Console.WriteLine("   - " + term.Name);
                        }
                    }
                }
            }
            Console.WriteLine("* * * END * * *");
            Console.ReadLine();
        }
    }
}

What we are missing is the fact that a term can contain terms. Thus, the MMS is heirarchical. We’ll have to keep this in mind when importing data into our MMS.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace MossDelight.Demos.ManagedMetadataConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSite site = new SPSite("http://demo9/sites/MMS");
            TaxonomySession txsn = new TaxonomySession(site);
            foreach (TermStore termstore in txsn.TermStores)
            {
                Console.WriteLine(">>> " + termstore.Name);

                foreach (Group group in termstore.Groups)
                {
                    Console.WriteLine("+ " + group.Name);

                    foreach (TermSet termset in group.TermSets)
                    {
                        Console.WriteLine("++ " + termset.Name);

                        foreach (Term term in termset.Terms)
                        {
                            WriteTerm(term,0);
                        }
                    }
                }
            }
            Console.WriteLine("* * * END * * *");
            Console.ReadLine();
        }

        private static void WriteTerm(Term term, int level) {

            Console.WriteLine("- ".PadLeft(level + 4, ' ') + term.Name);

            foreach (Term childterm in term.Terms)
            {
                WriteTerm(childterm, ++level);
            }
        }
    }
}

SharePoint 2010 Console Application System.IO.FileNotFoundException Error

I created a new console application to test some code snippets for SharePoint 2010 in Visual Studio 2010.

It threw the following error:

System.IO.FileNotFoundException was unhandled
Message=The Web application at http://demo/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

What a striking error! FileNotFound?

The issue is that, as we already know, SharePoint 2010 only runs in x64 bit and thus we only have 64-bit code on our machine. The default console application template in Visual Studio has a default of x86. If we change our Build platform target, we’ll get past this FileNotFound error.

Right-click the Project, select Properties, and on the Properties canvas, click the build tab. Change the platform target to Any (or x64 if you prefer).