From September 1st, after nearly 20 years as a freelance developer (during which I’ve also been working full-time), I’ll be taking a well-deserved break and accepting no further web development commissions. I’m hoping this will allow me time to focus on other interests and explore new avenues. Many, many thanks to everyone I’ve worked with and who’ve supported me over the years.
Author: Ryan
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()
}
To remove all data from a table and reset its primary key you can normally run the following SQL command:
TRUNCATE TABLE MyTable;
However, if you try this on a table with one or more foreign keys, you’re going to get an error message. Normally, the way around this is to drop the constraints, truncate the table, and recreate the constraints – which is a lot of fuss.
The following does the same job, but without the fuss.
DELETE FROM MyTable; DBCC CHECKIDENT (MyTable, RESEED, 0);
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.
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.
Upload limits within IIS7
By default, IIS7 will restrict uploaded greater than 30mb. To overcome this, the following settings can be added to the web.config file within your web application.
<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="157286400" /> </requestFiltering> </security> </system.webServer>
The value for the maxAllowedContentLength attribute should be specified in bytes (MB to Bytes Conversion).
It can also be helpful to define the maxRequestLength and executionTimeout attributes.
<system.web> <customErrors defaultRedirect="~/Error.aspx" mode="On"> <error statusCode="404" redirect="~/Error.aspx" /> </customErrors> <pages /> <globalization culture="en-GB" uiCulture="en-GB" /> <sessionState timeout="60" cookieless="AutoDetect" /> <httpRuntime executionTimeout="300" maxRequestLength="150000" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" /> </system.web>
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
Another note to self. When wanting a SharePoint workflow to send an email to the members of a specific SharePoint group:
- ensure that the group has at least Read access to the site
- within the group settings, check that “Who can view the membership of this group?” is set to Everyone
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:
- Exporting the list from SharePoint 2010 via the Backup and Restore option within Central Administration.
- Changing the extension of the resulting files from .cmp to .cab, and extracting their contents.
- Amending the versions stated within the SystemData.xml file from 14.0.x.x to 15.0.x.x
- Repackaging them back into a .cab file via CapPack.
- Changing the extension from .cab back to .cmp
- 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!
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.