Bobcat88
Bobcat88

Reputation: 758

Create SharePoint Site Collection Web Service

I've been attempting to create a site collection from a custom web service I have built in C#. I already had some methods in this that ran some powershell commands so I figured I would just create the site using powershell commands like the code below. This code runs fine without error but does not create a site collection, seems as if it is doing nothing. Is there a better way or can someone see what may be wrong below?

public string CreateSiteCollection(string urlroot, string urlname, string database, string primaryadmin, string secondadmin, string language, string description, string title, string template)
    {

        //Find language and template code 
        string lang_code = get_lang_code(language);
        string temp_code = get_temp_code(template);

        Call the PowerShell.Create() method to create an empty pipeline
        PowerShell ps = PowerShell.Create();

                // Place our script in the string myscript
        string myscript = string.Format(@"
                        Add-PSSnapin Microsoft.SharePoint.Powershell -erroraction 'silentlycontinue'
                        $template = Get-SPWebTemplate ""{0}""
                        New-SPSite {1}{2} -OwnerAlias '{3}' -SecondaryOwnerAlias '{4}' -Language {5} -Description '{6}' -ContentDatabase {7} -Template $template -Name '{8}'
                        ", temp_code, urlroot, urlname, primaryadmin, secondadmin, lang_code, description, database, title);


        // Create PowerShell runspace
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();    
        Pipeline pipeline = runspace.CreatePipeline(); // create pipepline then feed it myscript
        pipeline.Commands.AddScript(myscript);
        pipeline.Commands.Add("Out-String");

        Collection<PSObject> results;
        try
        {
            // Executing the script
            results = pipeline.Invoke();
        }
        catch (Exception e)
        {
            // An error occurred, return the exception
            return string.Format("Exception caught: {0}", e);
        }

        runspace.Close();

        StringBuilder stringBuilder = new StringBuilder();
        foreach (PSObject obj in results)
        {
            stringBuilder.AppendLine(obj.ToString());
        }

        string output = stringBuilder.ToString().Trim();
        if (output == "")
        {
            return "PowerShell Commands ran sucessfully with no output";
        }
        else
        {
            return String.Format("PowerShell Commands ran sucessfully and returned the following: {0}", output);
        }
    } // End of CreateSiteColleciton

I may use the Admin.asmx web service instead, but it would be easier if I could get this working because it allows me more customization.

Upvotes: 0

Views: 822

Answers (2)

Bobcat88
Bobcat88

Reputation: 758

I decided to go the with the object model just because it makes more sense. Below is the code for creating a new site collection:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
     using (SPSite site = new SPSite("http://servername:port/"))
     {
          using (SPWeb web = site.OpenWeb())
          {
               HttpContext.Current = null;
               site.AllowUnsafeUpdates = true;
               web.AllowUnsafeUpdates = true;

               var newSite = site.WebApplication.Sites.Add(....);
          }
     }
});

Upvotes: 0

Ola Ekdahl
Ola Ekdahl

Reputation: 1514

Is there a reason why you can't call the SharePoint server side object model directly from your web service like this, http://msdn.microsoft.com/en-us/library/office/ms411953(v=office.14).aspx.

Or is there a requirement to go through a set of CmdLets?

Upvotes: 1

Related Questions