Wednesday, December 28, 2016

Faking Microsoft.SharePoint.ServiceContext.Current with Powershell

While debugging some server side code, I tried to mimic it in Powershell. The challenge was that the backend code relied on Microsoft.SharePoint.SPServiceContext.Current. Fortunately, there are many posts on how to do this including:



Frustratingly, none of this was working when I tried. I was running as the Farm Account.

Then, I came across this: http://blog.claudiobrotto.com/2011/sharepoint-management-shell-vs-standard-powershell-console/

I normally use just the Windows Powershell while on my servers and call Add-PSSnapin Microsoft.SharePoint.Powershell. However, I forgot that the SPServiceContext::Current is being lost when a new thread is being used.

I added the following and all is good again:
$Host.RunSpace.ThreadOptions = 'ReuseThread' 


Outlook add-in logging

I recently inherited support of an Outlook plugin (add-in) and needed to output a few more debugging statements. I put those lines into the code, but not events were logged into the Event Viewer Application Log as expected. Fumbling around I saw this: https://msdn.microsoft.com/en-us/library/ms269003.aspx?f=255&MSPPError=-2147217396 and added an environment variable VSTO_SUPPRESSDISPLAYALERTS = 0.