【问题标题】:Create SharePoint Site Collection Web Service创建 SharePoint 网站集 Web 服务
【发布时间】:2014-08-11 19:13:32
【问题描述】:

我一直在尝试从我用 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 Web 服务,但如果我能让它工作起来会更容易,因为它允许我进行更多的自定义。

【问题讨论】:

标签: c# web-services sharepoint-2010


【解决方案1】:

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

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

【讨论】:

  • 我已经尝试了几次,但运气不佳。我得到了一些基于权限的错误,我现在真的没有太多时间来调试,但我想当我有更多时间时我可能不得不再次尝试这条路线。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多