PowerShell – How to detect/add the SharePoint snapin

Hi again :)

Just a quick post that will show you how you can check if the SharePoint snapin is loaded and if it isn’t, how to add it. This will be a really useful function that you can use at the start of all your SharePoint PowerShell scripts and the solution if you receive an error similar to this:

The term ‘_________’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the s
pelling of the name, or if a path was included, verify that the path is correct and try again.

This error occurs because the SharePoint Snapin is not loaded and you try using a command like: Get-SPWeb, Get-SPUser, Enable-SPFeature, Backup-SPFarm, etc.

Anyway here is the PowerShell script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function Check-SPSnapin
{
    Write-Host "Checking for SharePoint Snapin. Please wait....`r`n"

    if ((get-pssnapin | where-object {$_.name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
    {
        # Snapin wasn't loaded so we load it...
        Write-Host "Loading SharePoint Snapin. Please wait...`r`n"
        add-pssnapin "Microsoft.SharePoint.PowerShell"

        # This is just a final check to see if the Snapin is now successfully loaded
        if ((get-pssnapin | where-object {$_.name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
        {
            # Snapin still can not be detected, log it. You could put "exit" here if you desire.
            Write-Host "SharePoint Snapin was not successfully added.`r`n"
        }
        else
        {
            # It is now successfully loaded
            Write-Host "SharePoint Snapin loaded.`r`n"
        }
    }
    else
    {
        # The snapin was already added
        Write-Host "SharePoint Snapin detected.`r`n"
    }
}

Then just call the function in your script with Check-SPSnapin.

The script is self documented but if you have any problems, please ask.

Thanks,
Luke :D

SharePoint – How to programmatically reorder a Content Type’s Fields/Site Columns

First Post! :D

This Post will be showing you how you can reorder the order in which your Fields in the Content Type are displayed . This will effect the order in which they appear in the New, Edit and Display forms when the Content Type is applied to the List as well as in the Content Type Settings page:

New Item List Form

Personally I used the following code in a Feature Event Receiver because my Content Types were declaritively created in XML/CAML, but this code can also be used if and when the Content Types are created in code.

First of all we use a string array to specify the Field order that we want to achieve using the fields static names:

1
2
3
4
5
6
7
8
9
10
11
12
13
string[] fieldOrder =
{
    "Title", "EventType",
    "TimeZone", "StartDate",
    "EndDate", "Location",
    "fAllDayEvent", "fRecurrence",
    "Duration", "EventCanceled",
    "Comments", "Category",
    "WorkspaceLink", "UID",
    "RecurrenceID", "Workspace",
    "RecurrenceData", "XMLTZone",
    "MasterSeriesItemID"
};

Then we get an instance of the Content Type from the SPWeb object of the site we want to apply the change to:

1
SPContentType eventContentType = (((Your SPWeb Object))).ContentTypes["Event"];

Using the Reorder method of FieldLinks, we can pass in the string array we declared earlier:

1
eventContentType.FieldLinks.Reorder(fieldOrder);

Then we use the Update method of the Content Type object, also passing in true which updates the inheriting Content Types as well (Not really required):

1
eventContentType.Update(true);

And voilĂ ! It’s Complete.

Complete Code Example:
Note: In the below example complete code it creates new SPSite and SPWeb objects but keep in my mind the way that you create/retrieve these objects will differ depending on where you are using the code and what is considered best practice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string[] fieldOrder =
{
    "Title", "EventType",
    "TimeZone", "StartDate",
    "EndDate", "Location",
    "fAllDayEvent", "fRecurrence",
    "Duration", "EventCanceled",
    "Comments", "Category",
    "WorkspaceLink", "UID",
    "RecurrenceID", "Workspace",
    "RecurrenceData", "XMLTZone",
    "MasterSeriesItemID"
};

using (SPSite oSPSite = new SPSite("http://examplesharepointsite.com"))
{
    using (SPWeb oSPWeb = oSPSite.OpenWeb())
    {
        SPContentType eventContentType = oSPWeb.ContentTypes["Event"];
        eventContentType.FieldLinks.Reorder(fieldOrder);
        eventContentType.Update(true);
    }
}

If you have any suggestions or notice anything that could be done better please tell me, as I am a junior SharePoint developer and infant blogger :P

Cheers,
Luke :D