Solved

Create a mailbox for an user via webservice on a remote exchange 2007 server. (C#)

Posted on 2009-05-20
4
665 Views
Last Modified: 2012-05-07
Hi!
I'm getting frustrated here, feeling a massive headache coming up and I am nearly at the point to throw my PC out of the window...

This is the issue, I want to create a new user in the Active Directory via Sharepoint, underneath the Sharepoint list runs a worflow which catches the variabeles and sends them to a webservice which creates the user in active directory. This works perfectly, but the user also needs a mailbox.
So, what do we do? We set the mail property to the needed e-mail address and find the property "Create Mailbox" .... say whut? where is it? NOOOOOESSS it does not exist anymore, MS decided that it needs to be more complicated and now the only way to do this is using the powershell crap...

So thats why I am here, I googled some info on this and found some code which should do the trick, but, and thats where I am stuck, the webservice does not run on the Exchange server but on a different server, the webservice needsw to connect to the exchange server to run the powershellshizzle... can't find any info on this, can't find any examples and so on...

See the code below and please help me fix it,...
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
using Microsoft.PowerShell.Commands;  
using System.Web.Services;
using System.DirectoryServices;
 
namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
 
        [WebMethod]
        public void CreateADUser(string domain, string domainPostFix, string firstName, string emailaddress, string lastName, string department, string loginName, string password)
        {
            DirectoryEntry AD = null;
            DirectoryEntry NewUser = null;
 
            AD = new DirectoryEntry("LDAP://OU=Users,DC=" + domain + ",DC=" + domainPostFix);
 
            NewUser = AD.Children.Add("CN=" + firstName + " " + lastName, "user");
            NewUser.Properties["samAccountName"].Add(loginName);
            NewUser.Properties["name"].Add(firstName + " " + lastName);
            NewUser.Properties["displayname"].Add(firstName + " " + lastName);
            NewUser.Properties["givenName"].Add(firstName);
            NewUser.Properties["sn"].Add(lastName);
            NewUser.Properties["userprincipalname"].Add(loginName + "@" + domain + "." + domainPostFix);
            NewUser.CommitChanges();
            NewUser.Properties["mail"].Add(emailaddress);
       
            //NewUser.Properties["OU"].Add(department);
            NewUser.Invoke("SetPassword", new object[] { password });
 
            NewUser.CommitChanges();
 
            // E-mail shizzle, don't understand it yet, hopefully it works, if not, don't blame me -Erik
            RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();
            PSSnapInException PSException = null;
            PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out PSException);
            Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            Command command = new Command("New-Mailbox");
            command.Parameters.Add("Name", "TestName");
 
            //Enabling user account
            int val = (int)NewUser.Properties["userAccountControl"].Value;
            NewUser.Properties["userAccountControl"].Value = val & ~0x2;
            NewUser.CommitChanges();
 
            NewUser.Close();
    
 
        }

Open in new window

0
Comment
Question by:erik404
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24439146

It'll need the Exchange System Tools, I guess it's dying adding those to the RunSpace here?

runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out PSException);

Is the web server a member of the domain? There are 32-bit versions of the tools available if the server isn't 64-bit. Those are here (be sure to grab the SP1 version):

http://www.microsoft.com/downloads/details.aspx?FamilyID=6be38633-7248-4532-929b-76e9c677e802&displaylang=en

Chris
0
 
LVL 1

Author Comment

by:erik404
ID: 24474548
ok, I nearly have it now... got an error message that it can't find the databasepath, I can't find the format for the string though... The service does not run on the server where the exchange server runs though so I think I have to use a UNC path.

"myserver1\\First Storage Group\\Mailbox Database.edb" doesn't work, any idea's?
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 250 total points
ID: 24474586

It's the logical path rather than disk path (so no .edb). Should be:

"Server\StorageGroup\DatabaseName"

It's the same string you get if you were to run:

Get-MailboxDatabase

Chris
0
 
LVL 1

Author Closing Comment

by:erik404
ID: 31583482
Dude, it worked! I owe u one!
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question