2014-01-21 25 views
1

我需要以編程方式從Office 365中的應用程序中創建新的網站集。我的意思是一個新的網站集,是在創建後,它應該出現在Office 365的管理 - > Sharepoint選項卡下的網站集列表中。我嘗試在一個共享點託管的應用程序中使用下面的類似代碼,從APP中創建Office 365中的新網站集

//create sp context and get root 
var clientContext = new SP.ClientContext.get_current(); 
var rootWeb = clientContext.site.rootWeb(); 
this.clientContext.load(rootWeb); 
this.clientContext.executeQUery(); 

//set web info 
var webInfo = new SP.WebCreationInformation(); 
webInfo.set_webTemplate('YourTemplateName'); 
webInfo.set_description('Your site description'); 
webInfo.set_title('Your site tittle'); 
webInfo.set_url(siteUrl); 
webInfo.set_language(yourLangCode); 
this.rootWeb.get_webs().add(webInfo); 
this.rootWeb.update(); 

// save site and set callbacks 
this.clientContext.load(this.rootWeb); 
this.clientContext.executeQueryAsync(
Function.createDelegate(this, this.OnSiteCreationSuccess), 
Function.createDelegate(this, this.Error)); 

但是,這只是在承載我的應用程序的網站集下創建一個子網站。

關於如何實現這一點的任何建議將不勝感激。

回答

4

它可以使用SharePoint對象模型2013完成,您需要的功能是在程序集內:Microsoft.Online.SharePoint.Client.Tenant.dll,它位於C:\ Program Files \ SharePoint Client Components \ Assemblies在安裝SharePoint Client對象模型2013之後。

這方面沒有太多文檔,但SharePoint Online Management Shell有創建網站集的命令,所以我認爲可以使用C#完成並找出它。代碼片段展示瞭如何做到這一點。

using System; 
using Microsoft.Online.SharePoint.TenantAdministration; 
using Microsoft.SharePoint.Client; 
using System.Security; 

namespace SharePoint123 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //please change the value of user name, password, and admin portal URL 
      string username = "[email protected]"; 
      String pwd = "xxxx"; 
      ClientContext context = new ClientContext("https://xxxx-admin.sharepoint.com"); 
      SecureString password = new SecureString(); 
      foreach (char c in pwd.ToCharArray()) 
      { 
       password.AppendChar(c); 
      } 
      context.Credentials = new SharePointOnlineCredentials(username, password); 

      Tenant t = new Tenant(context); 
      context.ExecuteQuery();//login into SharePoint online 


      //code to create a new site collection 
      var newsite = new SiteCreationProperties() 
      { 
       Url = "https://xxxxx.sharepoint.com/sites/createdbyProgram1", 
       Owner = "[email protected]", 
       Template = "STS#0", //using the team site template, check the MSDN if you want to use other template 
       StorageMaximumLevel = 100, 
       UserCodeMaximumLevel = 100, 
       UserCodeWarningLevel = 100, 
       StorageWarningLevel = 300, 
       Title = "CreatedbyPrgram", 
       CompatibilityLevel = 15, //15 means Shapoint online 2013, 14 means Sharepoint online 2010 

      }; 
      t.CreateSite(newsite); 

      context.ExecuteQuery(); 
      //end 

     } 
    } 

} 
+0

非常感謝馬特。這正是我所期待的。它令人難以置信的是,如何獲得文檔。你知道是否有可能以編程方式爲用戶或組分配權限給創建的新網站集? – RAHUL

+0

我認爲這是可能的,因爲它已經在powershell命令中:http://technet.microsoft.com/en-us/library/fp161371(v = office.15)。它應該與這個類donw:http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.user.aspx。但我沒有這方面的例子。 – Matt