public async Task <IActionResult> Templates([FromBody] string url) { List <SharePointTemplate> results = new List <SharePointTemplate>(); try { Web web = cc.Web; // LCID: https://msdn.microsoft.com/en-us/library/ms912047%28v=winembedded.10%29.aspx?f=255&MSPPError=-2147217396 WebTemplateCollection templates = web.GetAvailableWebTemplates(1033, false); cc.Load(templates); //Execute the query to the server await cc.ExecuteQueryAsync(); // Loop through all the list templates foreach (WebTemplate template in templates) { SharePointTemplate item = new SharePointTemplate() { ID = template.Id, Title = template.Title, Name = template.Name, Description = template.Description }; results.Add(item); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex)); } return(new OkObjectResult(results)); }
public async Task <IActionResult> Templates(string url) { List <SharePointTemplate> results = new List <SharePointTemplate>(); try { // Starting with ClientContext, the constructor requires a URL to the // server running SharePoint. //string url = @"https://dddevops-my.sharepoint.com"; using (ClientContext context = new ClientContext(url)) { context.Credentials = new SharePointOnlineCredentials(_username, _password); Web web = context.Web; // LCID: https://msdn.microsoft.com/en-us/library/ms912047%28v=winembedded.10%29.aspx?f=255&MSPPError=-2147217396 WebTemplateCollection templates = web.GetAvailableWebTemplates(1033, false); context.Load(templates); //Execute the query to the server await context.ExecuteQueryAsync(); // Loop through all the list templates foreach (WebTemplate template in templates) { SharePointTemplate item = new SharePointTemplate() { ID = template.Id, Title = template.Title, Name = template.Name, Description = template.Description }; results.Add(item); } } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex)); } return(new OkObjectResult(results)); }
/// <summary> /// Get the available SharePoint site collection's web templates (solutions). /// </summary> /// <param name="context"> /// The SharePoint Host Web context. /// </param> /// <returns> /// The <see cref="List"/> of available Web Titles. /// </returns> internal static List <string> GetAvailableWebTemplates(ClientContext context) { WebTemplateCollection templates = context.Web.GetAvailableWebTemplates(1033, false); context.Load(templates); context.ExecuteQuery(); IEnumerable <string> list = from t in templates.ToList() where !t.IsHidden select t.Title; return(list.ToList()); }
/// <summary> /// Testing Method Only /// </summary> /// <returns></returns> public static List <SiteEntity> GetAllSites3() { try { using (ClientContext userContext = Helper.CreateAuthenticatedUserContext(Program.AdminDomain, Program.AdminUsername, Program.AdminPassword, Constants.PortalRootSiteUrl)) { // Lists all site collections across all web applications... //WebTemplateCollection wtc= userContext.Web.GetAvailableWebTemplates(1033, true); WebTemplateCollection wtc = userContext.Site.GetWebTemplates(1033, 0); userContext.Load(wtc); userContext.ExecuteQuery(); List <string> template = new List <string>(); foreach (WebTemplate wt in wtc) { template.Add(wt.Name); } List <SiteEntity> sites = new List <SiteEntity>(); foreach (string templateKeyword in template) { try { sites.AddRange(userContext.Web.SiteSearch(templateKeyword, true)); } catch (Exception ex) { Logger.LogErrorMessage(String.Format("GetAllSites() failed: Error={0}", ex.Message), false); } } List <string> siteurl = new List <string>(); foreach (SiteEntity site in sites) { if (!siteurl.Contains(site.Url)) { siteurl.Add(site.Url); } } //List<SiteEntity> sites = userContext.Web.SiteSearch(); return(sites); } } catch (Exception ex) { Logger.LogErrorMessage(String.Format("GetAllSites() failed: Error={0}", ex.Message), false); return(null); } }
//gavdcodeend 02 //gavdcodebegin 03 static void SpCsPnpcoreFindWebTemplates(ClientContext spCtx) { Site mySite = spCtx.Site; WebTemplateCollection myTemplates = mySite.GetWebTemplates(1033, 0); spCtx.Load(myTemplates); spCtx.ExecuteQuery(); foreach (WebTemplate oneTemplate in myTemplates) { Console.WriteLine(oneTemplate.Name + " - " + oneTemplate.Title); } }
protected void Page_Load(object sender, EventArgs e) { // define initial script, needed to render the chrome control string script = @" function chromeLoaded() { $('body').show(); } //function callback to render chrome after SP.UI.Controls.js loads function renderSPChrome() { //Set the chrome options for launching Help, Account, and Contact pages var options = { 'appTitle': document.title, 'onCssLoaded': 'chromeLoaded()' }; //Load the Chrome Control in the divSPChrome element of the page var chromeNavigation = new SP.UI.Controls.Navigation('divSPChrome', options); chromeNavigation.setVisible(true); }"; //register script in page Page.ClientScript.RegisterClientScriptBlock(typeof(Default), "BasePageScript", script, true); if (!Page.IsPostBack) { // Provide options for the template var spContext = SharePointContextProvider.Current.GetSharePointContext(Context); using (var ctx = spContext.CreateUserClientContextForSPHost()) { Web web = ctx.Web; ctx.Load(web); ctx.ExecuteQuery(); // Get templates to choose from WebTemplateCollection templates = ctx.Web.GetAvailableWebTemplates(ctx.Web.Language, false); ctx.Load(templates); ctx.ExecuteQuery(); drpContentTypes.Items.Clear(); foreach (var item in templates) { // Provide options for the template drpContentTypes.Items.Add(new System.Web.UI.WebControls.ListItem(item.Title, item.Name)); drpContentTypes.SelectedValue = "STS#0"; } } } }
public ActionResult Scenario1() { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); using (var cc = spContext.CreateAppOnlyClientContextForSPHost()) { cc.Load(cc.Web); cc.ExecuteQueryRetry(); // Get all of the WebTemplates so we can pass it to NewWebProperties to populate the SelectListItems WebTemplateCollection webTemplates = cc.Web.GetAvailableWebTemplates(cc.Web.Language, false); cc.Load(webTemplates); cc.ExecuteQueryRetry(); var props = new NewWebProperties(webTemplates); return(View(props)); } }
private static void ApplySiteTemplate(ClientContext cc) { WebTemplateCollection webTemps = cc.Web.GetAvailableWebTemplates(1033, false); cc.Load(webTemps); cc.ExecuteQuery(); foreach (WebTemplate webtemp in webTemps) { if (webtemp.Title.Equals("Contoso Template")) { Console.WriteLine(webtemp.Name + "|" + webtemp.Title + "|" + webtemp.Id); cc.Web.ApplyWebTemplate(webtemp.Name); cc.Web.Update(); cc.RequestTimeout = -1; // Can time out... so not necessarely good process cc.ExecuteQuery(); } } }
/// <summary> /// Create a sub site. /// </summary> /// <param name="siteUrl"> /// The site url. /// </param> /// <param name="templateName"> /// The SharePoint template name. /// </param> /// <param name="title"> /// The title. /// </param> /// <param name="description"> /// The description. /// </param> /// <param name="clientContext"> /// The client context. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public string CreateSubSite(string siteUrl, string templateName, string title, string description, ClientContext clientContext) { // Currently only english, could be extended to be configurable based on language pack usage // Lookup the web template, need the name from the title WebTemplateCollection templates = clientContext.Web.GetAvailableWebTemplates(1033, false); clientContext.Load(templates); clientContext.ExecuteQuery(); WebTemplate template = templates.FirstOrDefault(t => t.Title.Contains(templateName)); if (template == null) { throw new ArgumentException( string.Format( "The project site template '{0}' was not found in the available solutions.\r\n Check the App settings and the sites solutions for a matching template name.", templateName)); } // Create web creation configuration WebCreationInformation information = new WebCreationInformation { WebTemplate = template.Name, Description = description, Title = title, Url = siteUrl, Language = 1033 }; // Load host web and add new web to it. Web web = clientContext.Web; Web newWeb = web.Webs.Add(information); clientContext.ExecuteQuery(); clientContext.Load(newWeb); clientContext.ExecuteQuery(); // All done, let's return the URL of the newly created site return(newWeb.Url); }