internal CanvasSection(ClientSidePage page) { if (page == null) { throw new ArgumentNullException("Passed page cannot be null"); } this.page = page; Order = 0; }
internal CanvasZone(ClientSidePage page) { if (page == null) { throw new ArgumentNullException("Passed page cannot be null"); } this.page = page; Type = CanvasZoneTemplate.OneColumn; Order = 0; this.sections.Add(new CanvasSection(this)); }
public static ClientSidePage FromHtml(string html) { if (String.IsNullOrEmpty(html)) { throw new ArgumentException("Passed html cannot be null or empty"); } ClientSidePage page = new ClientSidePage(); page.LoadFromHtml(html); return(page); }
/// <summary> /// Creates a new canvas section /// </summary> /// <param name="page"><see cref="ClientSidePage"/> instance that holds this section</param> /// <param name="canvasSectionTemplate">Type of section to create</param> /// <param name="order">Order of this section in the collection of sections on the page</param> public CanvasSection(ClientSidePage page, CanvasSectionTemplate canvasSectionTemplate, float order) { if (page == null) { throw new ArgumentNullException("Passed page cannot be null"); } this.page = page; this.zoneEmphasis = 0; Type = canvasSectionTemplate; Order = order; switch (canvasSectionTemplate) { case CanvasSectionTemplate.OneColumn: goto default; case CanvasSectionTemplate.OneColumnFullWidth: this.columns.Add(new CanvasColumn(this, 1, 0)); break; case CanvasSectionTemplate.TwoColumn: this.columns.Add(new CanvasColumn(this, 1, 6)); this.columns.Add(new CanvasColumn(this, 2, 6)); break; case CanvasSectionTemplate.ThreeColumn: this.columns.Add(new CanvasColumn(this, 1, 4)); this.columns.Add(new CanvasColumn(this, 2, 4)); this.columns.Add(new CanvasColumn(this, 3, 4)); break; case CanvasSectionTemplate.TwoColumnLeft: this.columns.Add(new CanvasColumn(this, 1, 8)); this.columns.Add(new CanvasColumn(this, 2, 4)); break; case CanvasSectionTemplate.TwoColumnRight: this.columns.Add(new CanvasColumn(this, 1, 4)); this.columns.Add(new CanvasColumn(this, 2, 8)); break; default: this.columns.Add(new CanvasColumn(this, 1, 12)); break; } }
private ClientSideComponent GetComponent(ClientSidePage page,string cAlias) { var comps = page.AvailableClientSideComponents(); foreach (ClientSideComponent csc in comps) { dynamic data = JObject.Parse(csc.Manifest); string alias = data.alias; if (alias == cAlias) { return csc; } } return null; }
public static ClientSidePage Load(ClientContext cc, string pageName) { if (cc == null) { throw new ArgumentNullException("Passed ClientContext object cannot be null"); } if (String.IsNullOrEmpty(pageName)) { throw new ArgumentException("Passed pageName object cannot be null or empty"); } ClientSidePage page = new ClientSidePage(cc); page.pageName = pageName; var pagesLibrary = page.Context.Web.GetListByUrl(page.PagesLibrary, p => p.RootFolder); page.sitePagesServerRelativeUrl = pagesLibrary.RootFolder.ServerRelativeUrl; var file = page.Context.Web.GetFileByServerRelativeUrl($"{page.sitePagesServerRelativeUrl}/{page.pageName}"); page.Context.Web.Context.Load(file, f => f.ListItemAllFields, f => f.Exists); page.Context.Web.Context.ExecuteQueryRetry(); if (!file.Exists) { throw new ArgumentException($"Page {pageName} does not exist in current web"); } var item = file.ListItemAllFields; var html = item[ClientSidePage.CanvasField].ToString(); if (String.IsNullOrEmpty(html)) { throw new ArgumentException($"Page {pageName} is not a \"modern\" client side page"); } page.pageListItem = item; page.LoadFromHtml(html); return(page); }
/// <summary> /// Returns a HTML representation of the client side web part /// </summary> /// <param name="controlIndex">The sequence of the control inside the section</param> /// <returns>HTML representation of the client side web part</returns> public override string ToHtml(float controlIndex) { // Can this control be hosted in this section type? if (this.Section.Type == CanvasSectionTemplate.OneColumnFullWidth) { if (!this.SupportsFullBleed) { throw new Exception("You cannot host this web part inside a one column full width section, only webparts that support full bleed are allowed"); } } ClientSideWebPartControlData controlData = null; if (this.usingSpControlDataOnly) { controlData = new ClientSideWebPartControlDataOnly(); } else { controlData = new ClientSideWebPartControlData(); } // Obtain the json data controlData.ControlType = this.ControlType; controlData.Id = this.InstanceId.ToString("D"); controlData.WebPartId = this.WebPartId; controlData.Position = new ClientSideCanvasControlPosition() { ZoneIndex = this.Section.Order, SectionIndex = this.Column.Order, SectionFactor = this.Column.ColumnFactor, #if !SP2019 LayoutIndex = this.Column.LayoutIndex, #endif ControlIndex = controlIndex, }; #if !SP2019 if (this.section.Type == CanvasSectionTemplate.OneColumnVerticalSection) { if (this.section.Columns.First().Equals(this.Column)) { controlData.Position.SectionFactor = 12; } } #endif controlData.Emphasis = new ClientSideSectionEmphasis() { ZoneEmphasis = this.Column.VerticalSectionEmphasis.HasValue ? this.Column.VerticalSectionEmphasis.Value : this.Section.ZoneEmphasis, }; // Set the control's data version to the latest version...default was 1.0, but some controls use a higher version var webPartType = ClientSidePage.NameToClientSideWebPartEnum(controlData.WebPartId); // if we read the control from the page then the value might already be set to something different than 1.0...if so, leave as is if (this.DataVersion == "1.0") { if (webPartType == DefaultClientSideWebParts.Image) { this.dataVersion = "1.8"; } else if (webPartType == DefaultClientSideWebParts.ImageGallery) { this.dataVersion = "1.6"; } else if (webPartType == DefaultClientSideWebParts.People) { this.dataVersion = "1.2"; } else if (webPartType == DefaultClientSideWebParts.DocumentEmbed) { this.dataVersion = "1.1"; } else if (webPartType == DefaultClientSideWebParts.ContentRollup) { this.dataVersion = "2.1"; } else if (webPartType == DefaultClientSideWebParts.QuickLinks) { this.dataVersion = "2.0"; } } // Set the web part preview image url if (this.ServerProcessedContent != null && this.ServerProcessedContent["imageSources"] != null) { foreach (JProperty property in this.ServerProcessedContent["imageSources"]) { if (!string.IsNullOrEmpty(property.Value.ToString())) { this.webPartPreviewImage = property.Value.ToString().ToLower(); break; } } } ClientSideWebPartData webpartData = new ClientSideWebPartData() { Id = controlData.WebPartId, InstanceId = controlData.Id, Title = this.Title, Description = this.Description, DataVersion = this.DataVersion, Properties = "jsonPropsToReplacePnPRules", DynamicDataPaths = "jsonDynamicDataPathsToReplacePnPRules", DynamicDataValues = "jsonDynamicDataValuesToReplacePnPRules", ServerProcessedContent = "jsonServerProcessedContentToReplacePnPRules" }; if (this.usingSpControlDataOnly) { (controlData as ClientSideWebPartControlDataOnly).WebPartData = "jsonWebPartDataToReplacePnPRules"; this.jsonControlData = JsonConvert.SerializeObject(controlData); this.jsonWebPartData = JsonConvert.SerializeObject(webpartData); this.jsonWebPartData = this.jsonWebPartData.Replace("\"jsonPropsToReplacePnPRules\"", this.Properties.ToString(Formatting.None)); this.jsonWebPartData = this.jsonWebPartData.Replace("\"jsonServerProcessedContentToReplacePnPRules\"", this.ServerProcessedContent.ToString(Formatting.None)); this.jsonWebPartData = this.jsonWebPartData.Replace("\"jsonDynamicDataPathsToReplacePnPRules\"", this.DynamicDataPaths.ToString(Formatting.None)); this.jsonWebPartData = this.jsonWebPartData.Replace("\"jsonDynamicDataValuesToReplacePnPRules\"", this.DynamicDataValues.ToString(Formatting.None)); this.jsonControlData = this.jsonControlData.Replace("\"jsonWebPartDataToReplacePnPRules\"", this.jsonWebPartData); } else { this.jsonControlData = JsonConvert.SerializeObject(controlData); this.jsonWebPartData = JsonConvert.SerializeObject(webpartData); this.jsonWebPartData = this.jsonWebPartData.Replace("\"jsonPropsToReplacePnPRules\"", this.Properties.ToString(Formatting.None)); this.jsonWebPartData = this.jsonWebPartData.Replace("\"jsonServerProcessedContentToReplacePnPRules\"", this.ServerProcessedContent.ToString(Formatting.None)); this.jsonWebPartData = this.jsonWebPartData.Replace("\"jsonDynamicDataPathsToReplacePnPRules\"", this.DynamicDataPaths.ToString(Formatting.None)); this.jsonWebPartData = this.jsonWebPartData.Replace("\"jsonDynamicDataValuesToReplacePnPRules\"", this.DynamicDataValues.ToString(Formatting.None)); } StringBuilder html = new StringBuilder(100); #if NETSTANDARD2_0 html.Append($@"<div {CanvasControlAttribute}=""{this.CanvasControlData}"" {CanvasDataVersionAttribute}=""{this.DataVersion}"" {ControlDataAttribute}=""{this.JsonControlData.Replace("\"", """)}"">"); html.Append($@"<div {WebPartAttribute}=""{this.WebPartData}"" {WebPartDataVersionAttribute}=""{this.DataVersion}"" {WebPartDataAttribute}=""{this.JsonWebPartData.Replace("\"", """)}"">"); html.Append($@"<div {WebPartComponentIdAttribute}="""""); html.Append(this.WebPartId); html.Append("/div>"); html.Append($@"<div {WebPartHtmlPropertiesAttribute}=""{this.HtmlProperties}"">"); RenderHtmlProperties(ref html); html.Append("</div>"); html.Append("</div>"); html.Append("</div>"); #else var htmlWriter = new HtmlTextWriter(new System.IO.StringWriter(html), ""); try { if (this.usingSpControlDataOnly) { htmlWriter.NewLine = string.Empty; htmlWriter.AddAttribute(CanvasControlAttribute, this.CanvasControlData); htmlWriter.AddAttribute(CanvasDataVersionAttribute, this.CanvasDataVersion); htmlWriter.AddAttribute(ControlDataAttribute, this.JsonControlData); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.RenderEndTag(); } else { htmlWriter.NewLine = string.Empty; htmlWriter.AddAttribute(CanvasControlAttribute, this.CanvasControlData); htmlWriter.AddAttribute(CanvasDataVersionAttribute, this.CanvasDataVersion); htmlWriter.AddAttribute(ControlDataAttribute, this.JsonControlData); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.AddAttribute(WebPartAttribute, this.WebPartData); htmlWriter.AddAttribute(WebPartDataVersionAttribute, this.DataVersion); htmlWriter.AddAttribute(WebPartDataAttribute, this.JsonWebPartData); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.AddAttribute(WebPartComponentIdAttribute, ""); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.Write(this.WebPartId); htmlWriter.RenderEndTag(); htmlWriter.AddAttribute(WebPartHtmlPropertiesAttribute, this.HtmlProperties); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); // Allow for override of the HTML value rendering if this would be needed by controls RenderHtmlProperties(ref htmlWriter); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); } } finally { if (htmlWriter != null) { htmlWriter.Dispose(); } } #endif return(html.ToString()); }
/// <summary> /// Returns a HTML representation of the client side web part /// </summary> /// <param name="controlIndex">The sequence of the control inside the section</param> /// <returns>HTML representation of the client side web part</returns> public override string ToHtml(float controlIndex) { // Can this control be hosted in this section type? if (this.Section.Type == CanvasSectionTemplate.OneColumnFullWidth) { if (!(this.WebPartId.Equals(ClientSidePage.ClientSideWebPartEnumToName(DefaultClientSideWebParts.Image), StringComparison.InvariantCultureIgnoreCase) || this.WebPartId.Equals(ClientSidePage.ClientSideWebPartEnumToName(DefaultClientSideWebParts.Hero), StringComparison.InvariantCultureIgnoreCase))) { throw new Exception("You cannot host this web part inside a one column full width section, only an image web part or hero web part are allowed"); } } // Obtain the json data ClientSideWebPartControlData controlData = new ClientSideWebPartControlData() { ControlType = this.ControlType, Id = this.InstanceId.ToString("D"), WebPartId = this.WebPartId, Position = new ClientSideCanvasControlPosition() { ZoneIndex = this.Section.Order, SectionIndex = this.Column.Order, SectionFactor = this.Column.ColumnFactor, ControlIndex = controlIndex, }, }; ClientSideWebPartData webpartData = new ClientSideWebPartData() { Id = controlData.WebPartId, InstanceId = controlData.Id, Title = this.Title, Description = this.Description, DataVersion = this.DataVersion, Properties = "jsonPropsToReplacePnPRules" }; this.jsonControlData = JsonConvert.SerializeObject(controlData); this.jsonWebPartData = JsonConvert.SerializeObject(webpartData); this.jsonWebPartData = jsonWebPartData.Replace("\"jsonPropsToReplacePnPRules\"", this.Properties.ToString(Formatting.None)); StringBuilder html = new StringBuilder(100); var htmlWriter = new HtmlTextWriter(new System.IO.StringWriter(html), ""); try { htmlWriter.NewLine = string.Empty; htmlWriter.AddAttribute(CanvasControlAttribute, this.CanvasControlData); htmlWriter.AddAttribute(CanvasDataVersionAttribute, this.DataVersion); htmlWriter.AddAttribute(ControlDataAttribute, this.JsonControlData); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.AddAttribute(WebPartAttribute, this.WebPartData); htmlWriter.AddAttribute(WebPartDataVersionAttribute, this.DataVersion); htmlWriter.AddAttribute(WebPartDataAttribute, this.JsonWebPartData); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.AddAttribute(WebPartComponentIdAttribute, ""); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); htmlWriter.Write(this.WebPartId); htmlWriter.RenderEndTag(); htmlWriter.AddAttribute(WebPartHtmlPropertiesAttribute, this.HtmlProperties); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div); // Allow for override of the HTML value rendering if this would be needed by controls RenderHtmlProperties(ref htmlWriter); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); } finally { if (htmlWriter != null) { htmlWriter.Dispose(); } } return(html.ToString()); }
private void UpdateFile(string cFileName) { try { ClientSidePage page = ClientSidePage.Load(ctx, "Home.aspx"); ClientSidePage newPage = new ClientSidePage(ctx, ClientSidePageLayoutType.Home); foreach (CanvasControl ctrl in page.Controls) { System.Diagnostics.Trace.WriteLine(ctrl.ToString()); newPage.Controls.Add(ctrl); } newPage.Save("NewHome.aspx"); ctx.ExecuteQuery(); ClientSideComponent c = GetComponent(page, "EtcContentDisplayWebPart"); if (c!=null) { ClientSideWebPart csWebPart = new ClientSideWebPart(c); page.Controls.Add(csWebPart); page.Save(); ctx.ExecuteQuery(); } //foreach(ClientSideComponent csc in comps) //{ // dynamic data = JObject.Parse(csc.Manifest); // string alias = data.alias; // string componentType = data.componentType; // string isInternal = data.isInternal; // System.Diagnostics.Trace.WriteLine(componentType +" - "+isInternal+" - "+alias); // if (csc.Manifest.Contains("Content Display")) // { // System.Diagnostics.Trace.WriteLine(csc.Name+" -"+csc.Status+" - "+csc.ManifestType); // } //} System.Diagnostics.Trace.WriteLine(""); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } }