Developer´s SharePoint blog (SharePoint, Workflows, Apps, Document Management Systems, etc…)

Connecting to Exchange 2013 from SharePoint

Posted by skywalker3171 on January 25, 2016

When trying to connect to Exchange 2013 from SharePoint (2010/2013) there are several possibilities.

  1. Use Exchange Web Services (EWS), running operations via XML
  2. Use EWS Managed API
  3. Use EWS generated object models for Exchange

However, you may come into situations where none of the above are sufficiently powerfull to achieve what you want. For Instance if you want to Add new User from you site to a Exchange Distribution Group. This is where Powershell Remoting comes in.

Instead of connecting to WebService, you create a remote Powershell Session. This session will then have to be imported.

This is the Powershell version:

$pwd = ConvertTo-SecureString "mudar" -AsPlainText -Force
$username = "jboullay"
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $pwd

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://A1-QUAD-X-XXX/Powershell -Authentication Kerberos -Credential $cred
import-pssession $session
$disgroup = Get-DistributionGroup l-X-gerente-III
Set-DistributionGroup -identity l-X-gerente-III -BypassSecurityGroupManagerCheck -ManagedBy "Johannes Boullay", "new_user@xxxcorp.intranet"

Some conditions will have to be met before eeing the script successfully and moving it to SharePoint:

  1. Install the Microsoft Exchange Server 2013 Management Pack
  2. Install the Microsoft Exchange Web Services Managed API 2.0

Then Execute the Following Command from a Powershell with elevated rights:

c:\windows\syswow64\WindowsPowerShell\v1.0\powershell.exe -command set-executionpolicy unrestricted

When the Powershell Script above executes successfully, your’e ready for the next step, which is doing the same thing
from C# code within SharePoint:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;

using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;

namespace DistributionLists.SharePoint.WebParts.ApplyDistributionRights
    public class ApplyDistributionRights
        public void executePowershellScript(String listname, String username)
                    using (Runspace runspace = RunspaceFactory.CreateRunspace())

                        using (PowerShell ps = PowerShell.Create())
                            PSCommand command = new PSCommand();
                            command.AddParameter("Name", "myVar");
                            command.AddParameter("Value", GetSession(runspace));

                            ps.Commands = command;
                            ps.Runspace = runspace;

                            command = new PSCommand();
                            command.AddParameter("Name", "myVar");

                            ps.Commands = command;
                            ps.Runspace = runspace;

                            command = new PSCommand();
                            command.AddScript("Invoke-Command -ScriptBlock { Set-DistributionGroup -identity " + listname + " -BypassSecurityGroupManagerCheck -ManagedBy Add={@\"" + username + "\"}}" + " -Session $myVar");
                            ps.Commands = command;
                            ps.Runspace = runspace;

                            var result = ps.Invoke();

                            StringBuilder stringBuilder = new StringBuilder();
                            foreach (PSObject obj in result)
            catch (Exception ex)
            { }

        private static object GetSession(Runspace space)
            object result = null;

            PowerShell ps = PowerShell.Create();
            PSCommand command = new PSCommand();

            command.AddParameter("ConfigurationName", "Microsoft.Exchange");
            command.AddParameter("ConnectionUri", "http://A1-QUAD-X-XXX/PowerShell/");
            command.AddParameter("Credential", getCre("jboullay", "mudar"));
            command.AddParameter("Authentication", "Kerberos");

            ps.Commands = command;
            ps.Runspace = space;

            result = ps.Invoke()[0];
            return result;

        private static PSCredential getCre(string name, string passowrd)
            System.Security.SecureString ss = new System.Security.SecureString();
            foreach (var i in passowrd) ss.AppendChar(i);
            return new PSCredential(name, ss);

Two great Resources that provide information about Powershell Remoting from within code are:

  1. Blog Jonathan Huss
  2. Technet//Powershell Remoting





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: