Пример #1
0
        private void GenerateTargetCanvasControl(IPage targetPage, List <PageComponent> componentsToAdd, int controlOrder, ICanvasColumn targetColumn, Model.CanvasControl control, Dictionary <string, string> globalTokens, Guid taskId)
        {
            // Prepare a web part control container
            IPageComponent baseControl = null;

            switch (control.ControlType)
            {
            case Model.CanvasControlType.ClientSideText:
                // Here we add a text control
                var text = targetPage.NewTextPart();
                text.Text = control["Text"] as string;

                // Add the actual text control to the page
                targetPage.AddControl(text, targetColumn, controlOrder);

                // Log the just executed action
                logger.LogInformation(
                    TransformationResources.Info_CreatedTextControl
                    .CorrelateString(taskId));

                break;

            case Model.CanvasControlType.CustomClientSideWebPart:
                // Parse the control ID to support generic web part placement scenarios
                var ControlId = control["ControlId"] as string;
                // Check if this web part belongs to the list of "usable" web parts for this site
                baseControl = componentsToAdd.FirstOrDefault(p => p.Id.Equals($"{{{ControlId}}}", StringComparison.InvariantCultureIgnoreCase));

                logger.LogInformation(
                    TransformationResources.Info_UsingCustomModernWebPart
                    .CorrelateString(taskId));

                break;

            case Model.CanvasControlType.DefaultWebPart:
                // Determine the actual default web part
                var webPartType       = (DefaultWebPart)Enum.Parse(typeof(DefaultWebPart), control["WebPartType"] as string);
                var webPartName       = targetPage.DefaultWebPartToWebPartId(webPartType);
                var webPartTitle      = control["Title"] as string;
                var webPartProperties = control["Properties"] as Dictionary <string, string>;
                var jsonControlData   = control["JsonControlData"] as string;

                if (webPartType == DefaultWebPart.ClientWebPart)
                {
                    var addinComponents = componentsToAdd.Where(p => p.Name.Equals(webPartName, StringComparison.InvariantCultureIgnoreCase));
                    foreach (var addin in addinComponents)
                    {
                        // Find the right add-in web part via title matching...maybe not bullet proof but did find anything better for now
                        JObject wpJObject = JObject.Parse(addin.Manifest);

                        // As there can be multiple classic web parts (via provider hosted add ins or SharePoint hosted add ins) we're looping to find the first one with a matching title
                        foreach (var addinEntry in wpJObject["preconfiguredEntries"])
                        {
                            if (addinEntry["title"]["default"].Value <string>() == webPartTitle)
                            {
                                baseControl = addin;

                                var jsonProperties = addinEntry;

                                // Fill custom web part properties in this json. Custom properties are listed as child elements under clientWebPartProperties,
                                // replace their "default" value with the value we got from the web part's properties
                                jsonProperties = PopulateAddInProperties(jsonProperties, webPartProperties);

                                // Override the JSON data we read from the model as this is fully dynamic due to the nature of the add-in client part
                                jsonControlData = jsonProperties.ToString(Newtonsoft.Json.Formatting.None);

                                logger.LogInformation(
                                    TransformationResources.Info_ContentUsingAddinWebPart
                                    .CorrelateString(taskId), baseControl.Name);

                                break;
                            }
                        }
                    }
                }
                else
                {
                    baseControl = componentsToAdd.FirstOrDefault(p => p.Name.Equals(webPartName, StringComparison.InvariantCultureIgnoreCase));

                    logger.LogInformation(
                        TransformationResources.Info_ContentUsingModernWebPart
                        .CorrelateString(taskId), webPartType);
                }

                // If we found the web part as a possible candidate to use then add it
                if (baseControl != null)
                {
                    var jsonDecoded = WebUtility.HtmlDecode(this.tokenParser.ReplaceTargetTokens(targetPage.PnPContext, jsonControlData, webPartProperties, globalTokens));

                    var myWebPart = targetPage.NewWebPart(baseControl);
                    myWebPart.Order          = controlOrder;
                    myWebPart.PropertiesJson = jsonDecoded;

                    // Add the actual text control to the page
                    targetPage.AddControl(myWebPart, targetColumn, controlOrder);

                    logger.LogInformation(
                        TransformationResources.Info_AddedClientSideWebPartToPage
                        .CorrelateString(taskId), webPartTitle);
                }
                else
                {
                    logger.LogWarning(
                        TransformationResources.Warning_ContentWarnModernNotFound
                        .CorrelateString(taskId));
                }

                break;

            default:
                break;
            }
        }