Categories
SharePoint

Looping through SPList items

Now this is something I’m guilty of time and time again. Within my PowerShell scripts I quite often query SharePoint lists and loop through them in the following way…

$site = New-Object Microsoft.SharePoint.SPSite( $url )
$web = $site.OpenWeb()
$list = $web.List

for ( $i = 0; $i < $list.Items.Count; $i++) {
   SPListItem listItem = activeList.Items[i];
   htmlWriter.Write(listItem["Title"]);
}

What I should be doing is this…

SPListItemCollection items = SPContext.Current.List.Items;

for(int i=0;i<100 && i<items.Count;i++) {
  SPListItem listItem = items[i];
  htmlWriter.Write(listItem["Title"]);
}
Categories
SharePoint

Adding users to a Yammer group

I’m making a note of this blog post as I can foresee a future need to be able to subscribe specific subsets of users to Yammer groups.

How To Add A User To Yammer Group Using PowerShell

Also see the official documentation for /group_memberships.json

Categories
SharePoint

XSL and dumping XML

When you’re attempting to construct an XSL template its always helpful to know the structure of the XML you’re transforming. When working with lists in SharePoint, this isn’t always obvious.

One quick solution is simply to dump the XML to the page so you can review it.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<pre>
<xsl:copy-of select="*"/>
</pre>
</xsl:template>
</xsl:stylesheet>

Simple.

Categories
SharePoint

Creating SharePoint alerts via CSOM

I’m posting this as a useful reminder. It outlines how alerts can now be created via CSOM.

New SharePoint CSOM version released for SharePoint Online – February 2017

The included code sample is in C#, but I’ve translated it to PowerShell – which suits my administrative needs better.

[System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.SharePoint.Client" ) | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.SharePoint.Client.Runtime" ) | Out-Null


# Set variables
$path = "C:\Temp\Usernames.txt"

$url = "https://xxxxxx.sharepoint.com/sites/help"

$list = "My List"

$title = "$list (Auto-subscribed)"


# Prompt the administrator to log in
$credential = Get-Credential -Credential $null


# Create a connection to SharePoint Online
$context = New-Object Microsoft.SharePoint.Client.ClientContext( $url )
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials( $credential.UserName, $credential.Password )


# Retrieve the targeted web
$web = $context.Web

$context.Load( $web )
$context.ExecuteQuery()


# Loop through the provided usernames and create a new alert for each one
$usernames = Get-Content -Path $path

foreach ( $username in $usernames )
{
    $user = $context.Web.EnsureUser( $username )

    $alert = New-Object Microsoft.SharePoint.Client.AlertCreationInformation
    $alert.List = $context.Web.Lists.GetByTitle( $list )
    $alert.AlertFrequency = [Microsoft.SharePoint.Client.AlertFrequency]::Daily
    $alert.AlertTime = ( Get-Date ).AddDays( 1 )
    $alert.AlertType = [Microsoft.SharePoint.Client.AlertType]::List
    $alert.AlwaysNotify = $false
    $alert.DeliveryChannels = [Microsoft.SharePoint.Client.AlertDeliveryChannel]::Email
    $alert.Status = [Microsoft.SharePoint.Client.AlertStatus]::On
    $alert.Title = $title
    $alert.User = $user
    $alert.EventType = [Microsoft.SharePoint.Client.AlertEventType]::All
    $alert.Filter = "1"

    $guid = $user.Alerts.Add( $alert )

    $user.Update()

    $context.ExecuteQuery()
}
Categories
SharePoint

User Profile Synchronization Service

No able to start the User Profile Synchronization Service within SharePoint 2013? Ensure you’re logged into Central Administration using the same account as you’ve specified to run the service and click the Start action.

Categories
SharePoint

Secure credentials within PowerShell

For PowerShell scripts that need to access resources (e.g. a site within SharePoint) and be run as scheduled tasks, I will normally use an encrypted password…

Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File "$PSScriptRoot\Password.txt"

… and then refer to it within my script.

$username = "me@domain.co.uk"
$password = Get-Content "$PSScriptRoot\Password.txt"

$securePassword = $password | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PSCredential( $username, $securePassword )

This <a href="https://blog.kloud.com.au/2016/04/21/using-saved-credentials-securely-in-powershell-scripts/">blog post by Kloud</a> covers some this technique in greater detail.
Categories
SharePoint

Falling foul of SharePoint SSL

Recently, despite ensuring the SSL certificates within Internet Information Services (IIS) were updated ahead of their expiry, I fell foul of the hidden certificate within SharePoint 2013’s Security Token Service. In my defence, this is a certificate not mentioned within IIS or the Central Administration site. Its one of those lovely settings that can only be seen, analysed, and amended via PowerShell commands. Once you know/remember its there, its relatively straight forward to update.

$path = "C:\certificate.pfx"
$password = "thepassword"

$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $path, $password, 20

Set-SPSecurityTokenServiceConfig -ImportSigningCertificate $certificate

certutil -addstore -enterprise -f -v root $certificate

iisreset

net stop SPTimerV4
net start SPTimerV4

See: Replace the STS certificate for the on-premises environment

 

Categories
SharePoint

SharePoint Workflows, Emails, and Groups

Another note to self. When wanting a SharePoint workflow to send an email to the members of a specific SharePoint group:

  1. ensure that the group has at least Read access to the site
  2. within the group settings, check that “Who can view the membership of this group?” is set to Everyone
Categories
SharePoint

Migrating Lists from SharePoint 2010 to SharePoint 2013

While waiting for a purchase order for Sharegate to be approved, I needed to quickly migrate content of an old SharePoint 2010 list to our new SharePoint 2013 farm. Out of the box this isn’t straight forward, and Microsoft recommend a complete backup, move and upgrade approach for the entire content database. Time-wise, this wasn’t an option and seemed overkill for a single list.

In the past I’ve experimented by writing a PowerShell script to export the list’s content to a .CSV file and it’s attachments into folders, and then another script to suck it back into the destination site.

Before heading down that route again, I searched for a different approach and found the Fast & Cheap method documented by Vlad Catrinescu.

It basically involves:

  1. Exporting the list from SharePoint 2010 via the Backup and Restore option within Central Administration.
  2. Changing the extension of the resulting files from .cmp to .cab, and extracting their contents.
  3. Amending the versions stated within the SystemData.xml file from 14.0.x.x to 15.0.x.x
  4. Repackaging them back into a .cab file via CapPack.
  5. Changing the extension from .cab back to .cmp
  6. Importing the list into SharePoint 2013 via PowerShell.
Import-SPWeb -Identity http://mysharepointsite -Path \\temp\SharePointList.cmp -IncludeUserSecurity -Verbose

It seems to have worked like a charm!

Categories
SharePoint

SharePoint 2013 and IE11 Support

Very odd. Even though I’ve been using Internet Explorer 11 and SharePoint 2013 together for some time, this morning, when I created a new Site Collection and added a Calendar, it wasn’t rendering as it should and many links were broken.

After a little research,  it seems that IE11 doesn’t work out-of-the-box with SharePoint 2013 and boils down to an inconsistency with the X-UA-Compatible meta tag.

To overcome this, I added a new URL rewrite rule into IIS that spoofs the User Agent of the visitor’s browser, forcing the SharePoint page to be rendered correctly. No Compatibility View required.