Tuesday, 24 March 2015

How can I easily debug an email utility in SharePoint?

Working with emails (especially in Timer Service jobs) can be a pain to debug. Fortunately, there are a few freely available utilities that make this chore very simple.

I recently wrote a simple server that send numerous emails using the following code:

                        var headers = new StringDictionary
                        {
                            {"from", web.Site.WebApplication.OutboundMailSenderAddress},
                            {"to", user.Email},
                            {"subject", subject},
                            {"content-type", "text/html"}
                        };

                        SPUtility.SendEmail(web, headers, CreateEmailMessage());

I then downloaded the fantastic FakeSMTP to act as my SMTP server. This gem intercepts emails on the default mail port (25). Download the code and run 'java -jar fakeSTMP.jar'. (Be sure to start the server before you begin).

The next step is to configure SharePoint to use the local machine as its SMTP server.
In Central Administration -> System Settings, select 'Configure outgoing email settings'


Set the 'Outbound SMTP server' to the full name of your development machine. You can get the name from 'Control Panel\All Control Panel Items\System'

You are now in a position to retrieve the emails. The last piece of the puzzle is to download an eml viewer. I use FreeViewer.

NOTE: Remember to associate .eml files to this application or you will be prompted when double clicking on the email.

Monday, 9 March 2015

SharePoint 2013: What do the items in a claims token mean?

The code for 'All Authenticated Users' is c:0(.s|true.

Its all very confusing - but everything is revealed here

SharePoint 2013: How can I get a users SPPrincipal token?

In a SharePoint Claims authenticated environment, you need to extract the full claims token (not just DOMAIN\Username). Here are two simple methods using the ResolvePrincipal function to get the information:

PowerShell:

function GetUserPrincipalFromUsername($siteUrl, $login)
{
$web = Get-SPWeb $siteUrl
$principal = [Microsoft.SharePoint.Utilities.SPUtility]::ResolvePrincipal($web, $login, [Microsoft.SharePoint.Utilities.SPPrincipalType]::All, [Microsoft.SharePoint.Utilities.SPPrincipalSource]::All, $null, $false)
$web.Dispose()
return $principal
}

C#:

public SPPrincipalInfo GetUserPrincipalFromUsername(SPWeb web, string userName)
{
  return SPUtility.ResolvePrincipal(web, login, SPPrincipalType.All, SPPrincipalSource.All, null, false);
}

Thursday, 5 March 2015

SharePoint 2013: Error updating managed account credentials

I encountered the following message when trying to change a password for a managed account:

Error deploying administration application pool credentials. Another deployment may be active. An object of the type Microsoft.SharePoint.Administration.SPAdminAppPoolCredentialDeploymentJobDefinition named "job-admin-apppool-change" already exists under the parent Microsoft.SharePoint.Administration.SPTimerService named "SPTimerV4".  Rename your object or delete the existing object.

The error message is self explanatory - Rename your object or delete the existing object. I choose the latter. Powershell to the rescue.

$job = Get-SPTimerJob -Identity "job-admin-apppool-change"
$job.Delete()

Reset the password and all should be good.

Wednesday, 4 March 2015

SharePoint 2013: The sandboxed code execution request was refused because the Sandboxed Code Host Service was too busy to handle the request.

This was a nasty problem to resolve. I googled it and went through some basic steps:

1. follow the suggestions from msdn blog (here)
2. restart the windows service
but no luck.

I then restarted the SharePoint Service (Central Administration -> Settings -> Services on Server) and found that the password for my managed account had expired.

I ran the powershell script Set-SPManagedAccount to correct the problem.

SharePoint: The contents of the feature’s solution requires the Solution Sandbox service to be running

In 'System Settings' -> 'Manage Services on Server', ensure that the service 'Microsoft SharePoint Foundation Sandboxed Code Service' is running.

Open a new instance of PowerShell and you are good to go ...