创建SharePoint网站集Web服务

时间:2014-08-11 19:13:32

标签: c# web-services sharepoint-2010

我一直在尝试使用C#内置的自定义Web服务创建网站集。我已经有了一些运行一些powershell命令的方法,所以我想我会使用PowerShell命令创建网站,如下面的代码。此代码运行正常,没有错误,但不创建网站集,似乎它什么都不做。有没有更好的方法,或者有人可以看到下面可能出现的问题?

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

我可能会使用Admin.asmx网络服务,但如果能让我的工作变得更加容易,那会更容易,因为它可以让我进行更多自定义。

2 个答案:

答案 0 :(得分:1)

有没有理由不能直接从您的Web服务http://msdn.microsoft.com/en-us/library/office/ms411953(v=office.14).aspx调用SharePoint服务器端对象模型。

或者是否要求通过一组CmdLets?

答案 1 :(得分:0)

我决定使用对象模型只是因为它更有意义。以下是创建新网站集的代码:

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(....);
          }
     }
});