/// <summary> /// Gets the hash code /// </summary> /// <returns>Returns HashCode</returns> public override int GetHashCode() { return(String.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}|", this.ControlProperties.Aggregate(0, (acc, next) => acc += next.GetHashCode()), Type.GetHashCode(), CustomWebPartName?.GetHashCode() ?? 0, JsonControlData?.GetHashCode() ?? 0, ControlId.GetHashCode(), Order.GetHashCode(), Column.GetHashCode() ).GetHashCode()); }
/// <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) { if (!IsHeaderControl) { // Can this control be hosted in this section type? if (Section.Type == CanvasSectionTemplate.OneColumnFullWidth) { if (!SupportsFullBleed) { throw new ClientException(ErrorType.Unsupported, PnPCoreResources.Exception_Page_ControlNotAllowedInFullWidthSection); } } WebPartControlData controlData; if (UsingSpControlDataOnly) { controlData = new WebPartControlDataOnly(); } else { controlData = new WebPartControlData(); } // Obtain the json data controlData.ControlType = ControlType; controlData.Id = InstanceId.ToString("D"); controlData.WebPartId = WebPartId; controlData.Position = new CanvasControlPosition() { ZoneIndex = Section.Order, SectionIndex = Column.Order, SectionFactor = Column.ColumnFactor, LayoutIndex = Column.LayoutIndex, ControlIndex = controlIndex, }; if (section.Type == CanvasSectionTemplate.OneColumnVerticalSection) { if (section.Columns.First().Equals(Column)) { controlData.Position.SectionFactor = 12; } } controlData.Emphasis = new SectionEmphasis() { ZoneEmphasis = Column.VerticalSectionEmphasis.HasValue ? Column.VerticalSectionEmphasis.Value : 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 = Page.IdToDefaultWebPart(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 (DataVersion == "1.0") { if (webPartType == DefaultWebPart.Image) { dataVersion = "1.8"; } else if (webPartType == DefaultWebPart.ImageGallery) { dataVersion = "1.6"; } else if (webPartType == DefaultWebPart.People) { dataVersion = "1.2"; } else if (webPartType == DefaultWebPart.DocumentEmbed) { dataVersion = "1.1"; } else if (webPartType == DefaultWebPart.ContentRollup) { dataVersion = "2.1"; } else if (webPartType == DefaultWebPart.QuickLinks) { dataVersion = "2.0"; } } // Set the web part preview image url if (ServerProcessedContent.TryGetProperty("imageSources", out JsonElement imageSources)) { foreach (var property in imageSources.EnumerateObject()) { if (!string.IsNullOrEmpty(property.Value.ToString())) { WebPartPreviewImage = property.Value.ToString().ToLower(); break; } } } WebPartData webpartData = new WebPartData() { Id = controlData.WebPartId, InstanceId = controlData.Id, Title = Title, Description = Description, DataVersion = DataVersion, Properties = "jsonPropsToReplacePnPRules", DynamicDataPaths = "jsonDynamicDataPathsToReplacePnPRules", DynamicDataValues = "jsonDynamicDataValuesToReplacePnPRules", ServerProcessedContent = "jsonServerProcessedContentToReplacePnPRules" }; if (UsingSpControlDataOnly) { (controlData as WebPartControlDataOnly).WebPartData = "jsonWebPartDataToReplacePnPRules"; jsonControlData = JsonSerializer.Serialize(controlData); JsonWebPartData = JsonSerializer.Serialize(webpartData); JsonWebPartData = JsonWebPartData.Replace("\"jsonPropsToReplacePnPRules\"", Properties.ToString()); JsonWebPartData = JsonWebPartData.Replace("\"jsonServerProcessedContentToReplacePnPRules\"", ServerProcessedContent.ToString()); JsonWebPartData = JsonWebPartData.Replace("\"jsonDynamicDataPathsToReplacePnPRules\"", DynamicDataPaths.ToString()); JsonWebPartData = JsonWebPartData.Replace("\"jsonDynamicDataValuesToReplacePnPRules\"", DynamicDataValues.ToString()); jsonControlData = jsonControlData.Replace("\"jsonWebPartDataToReplacePnPRules\"", JsonWebPartData); } else { jsonControlData = JsonSerializer.Serialize(controlData); JsonWebPartData = JsonSerializer.Serialize(webpartData); JsonWebPartData = JsonWebPartData.Replace("\"jsonPropsToReplacePnPRules\"", Properties.ToString()); JsonWebPartData = JsonWebPartData.Replace("\"jsonServerProcessedContentToReplacePnPRules\"", ServerProcessedContent.ToString()); JsonWebPartData = JsonWebPartData.Replace("\"jsonDynamicDataPathsToReplacePnPRules\"", DynamicDataPaths.ToString()); JsonWebPartData = JsonWebPartData.Replace("\"jsonDynamicDataValuesToReplacePnPRules\"", DynamicDataValues.ToString()); } } else { HeaderControlData webpartData = new HeaderControlData() { Id = WebPartId, InstanceId = InstanceId.ToString("D"), Title = Title, Description = Description, DataVersion = DataVersion, Properties = "jsonPropsToReplacePnPRules", ServerProcessedContent = "jsonServerProcessedContentToReplacePnPRules" }; canvasDataVersion = DataVersion; JsonWebPartData = JsonSerializer.Serialize(webpartData); JsonWebPartData = JsonWebPartData.Replace("\"jsonPropsToReplacePnPRules\"", Properties.ToString()); JsonWebPartData = JsonWebPartData.Replace("\"jsonServerProcessedContentToReplacePnPRules\"", ServerProcessedContent.ToString()); jsonControlData = JsonWebPartData; } StringBuilder html = new StringBuilder(100); if (UsingSpControlDataOnly || IsHeaderControl) { html.Append($@"<div {CanvasControlAttribute}=""{CanvasControlData}"" {CanvasDataVersionAttribute}=""{DataVersion}"" {ControlDataAttribute}=""{JsonControlData.Replace("\"", """)}""></div>"); } else { html.Append($@"<div {CanvasControlAttribute}=""{CanvasControlData}"" {CanvasDataVersionAttribute}=""{DataVersion}"" {ControlDataAttribute}=""{JsonControlData.Replace("\"", """)}"">"); html.Append($@"<div {WebPartAttribute}=""{WebPartData}"" {WebPartDataVersionAttribute}=""{DataVersion}"" {WebPartDataAttribute}=""{JsonWebPartData.Replace("\"", """).Replace("<", "<").Replace(">", ">")}"">"); html.Append($@"<div {WebPartComponentIdAttribute}="""">"); html.Append(WebPartId); html.Append("</div>"); html.Append($@"<div {WebPartHtmlPropertiesAttribute}=""{HtmlProperties}"">"); RenderHtmlProperties(ref html); html.Append("</div>"); html.Append("</div>"); html.Append("</div>"); } return(html.ToString()); }