private string ProvisionSharepointPageWithScriptEditor(string pageName, string html, string wikiHtmlContent) { //get page library List wikiLibrary = _clientContext.Web.Lists.GetByTitle("Site Pages"); //make sure your developer site is in english, or change this title!!! _clientContext.Load(wikiLibrary); _clientContext.Load(wikiLibrary.RootFolder); _clientContext.ExecuteQuery(); //get page string serverrelativeurl = string.Format("{0}/{1}", wikiLibrary.RootFolder.ServerRelativeUrl, pageName); Microsoft.SharePoint.Client.File page; try { page = _clientContext.Web.GetFileByServerRelativeUrl(serverrelativeurl); _clientContext.Load(page); _clientContext.ExecuteQuery(); } catch (Exception) { //if page does not exist, create it var newWikiPage = new WikiPageCreationInformation(); newWikiPage.WikiHtmlContent = wikiHtmlContent; newWikiPage.ServerRelativeUrl = serverrelativeurl; Utility.CreateWikiPageInContextWeb(_clientContext, newWikiPage); _clientContext.ExecuteQuery(); page = _clientContext.Web.GetFileByServerRelativeUrl(newWikiPage.ServerRelativeUrl); //add script editor with the javascript that will do the magic string webPartXml = ""; using (StreamReader sr = new StreamReader(_server.MapPath("~/ScriptEditorTemplate.webpart"))) { webPartXml = sr.ReadToEnd(); } html = html.Replace("<", "<").Replace(">", ">"); //replace illegal xml tokens webPartXml = webPartXml.Replace("#content#", html); //put javascript in the content of script editor webpart var wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared); WebPartDefinition wpd = wpm.ImportWebPart(webPartXml); wpm.AddWebPart(wpd.WebPart, "Right", 1); _clientContext.ExecuteQuery(); } return(string.Format("{0}/{1}", wikiLibrary.RootFolder.ServerRelativeUrl, pageName)); }
private string ProvisionSharepointPageWithScriptEditor(string pageName, string html, string wikiHtmlContent) { //get page library List wikiLibrary = _clientContext.Web.Lists.GetByTitle("Site Pages"); //make sure your developer site is in english, or change this title!!! _clientContext.Load(wikiLibrary); _clientContext.Load(wikiLibrary.RootFolder); _clientContext.ExecuteQuery(); //get page string serverrelativeurl = string.Format("{0}/{1}", wikiLibrary.RootFolder.ServerRelativeUrl, pageName); Microsoft.SharePoint.Client.File page; try { page = _clientContext.Web.GetFileByServerRelativeUrl(serverrelativeurl); _clientContext.Load(page); _clientContext.ExecuteQuery(); } catch (Exception) { //if page does not exist, create it var newWikiPage = new WikiPageCreationInformation(); newWikiPage.WikiHtmlContent = wikiHtmlContent; newWikiPage.ServerRelativeUrl = serverrelativeurl; Utility.CreateWikiPageInContextWeb(_clientContext, newWikiPage); _clientContext.ExecuteQuery(); page = _clientContext.Web.GetFileByServerRelativeUrl(newWikiPage.ServerRelativeUrl); //add script editor with the javascript that will do the magic string webPartXml = ""; using (StreamReader sr = new StreamReader(_server.MapPath("~/ScriptEditorTemplate.webpart"))) { webPartXml = sr.ReadToEnd(); } html = html.Replace("<", "<").Replace(">", ">"); //replace illegal xml tokens webPartXml = webPartXml.Replace("#content#", html); //put javascript in the content of script editor webpart var wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared); WebPartDefinition wpd = wpm.ImportWebPart(webPartXml); wpm.AddWebPart(wpd.WebPart, "Right", 1); _clientContext.ExecuteQuery(); } return string.Format("{0}/{1}", wikiLibrary.RootFolder.ServerRelativeUrl, pageName); }