public override string ToHtml() { // Obtain the json data ClientSideWebPartControlData controlData = new ClientSideWebPartControlData() { ControlType = this.ControlType, Id = this.InstanceId.ToString("D"), WebPartId = this.WebPartId }; ClientSideWebPartData webpartData = new ClientSideWebPartData() { Id = controlData.WebPartId, InstanceId = controlData.Id, Title = this.Title, Description = this.Description, DataVersion = this.DataVersion, Properties = "jsonPropsToReplacePnPRules" }; jsonControlData = JsonConvert.SerializeObject(controlData); jsonWebPartData = JsonConvert.SerializeObject(webpartData); jsonWebPartData = jsonWebPartData.Replace("\"jsonPropsToReplacePnPRules\"", this.Properties.ToString(Formatting.None)); StringBuilder html = new StringBuilder(100); using (var htmlWriter = new HtmlTextWriter(new System.IO.StringWriter(html), "")) { 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); htmlWriter.Write(this.HtmlPropertiesData); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); } 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.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()); }
/// <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"); } } // 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); #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 { 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(); } } #endif return(html.ToString()); }