コード例 #1
0
ファイル: PageWebPart.cs プロジェクト: MondayCoffee/pnpcore
        /// <summary>
        /// Imports a <see cref="PageComponent"/> to use it as base for configuring the client side web part instance
        /// </summary>
        /// <param name="component"><see cref="PageComponent"/> to import</param>
        /// <param name="clientSideWebPartPropertiesUpdater">Function callback that allows you to manipulate the client side web part properties after import</param>
        public void Import(IPageComponent component, Func <string, string> clientSideWebPartPropertiesUpdater = null)
        {
            // Sometimes the id guid is encoded with curly brackets, so let's drop those
            WebPartId = new Guid(component.Id).ToString("D");

            // Parse the manifest json blob as we need some data from it
            var wpJObject = JsonSerializer.Deserialize <JsonElement>(component.Manifest);

            Title = wpJObject.GetProperty("preconfiguredEntries").EnumerateArray().First().GetProperty("title").GetProperty("default").GetString();

            Description = wpJObject.GetProperty("preconfiguredEntries").EnumerateArray().First().GetProperty("title").GetProperty("default").GetString();

            if (wpJObject.TryGetProperty("supportsFullBleed", out JsonElement supportsFullBleed))
            {
                SupportsFullBleed = supportsFullBleed.GetBoolean();
            }
            else
            {
                SupportsFullBleed = false;
            }

            SetPropertiesJson(wpJObject.GetProperty("preconfiguredEntries").EnumerateArray().First().GetProperty("properties"));

            if (clientSideWebPartPropertiesUpdater != null)
            {
                propertiesJson = clientSideWebPartPropertiesUpdater(propertiesJson);
            }
        }
コード例 #2
0
ファイル: PageWebPart.cs プロジェクト: wonderplayer/pnpcore
 /// <summary>
 /// Instantiates a client side web part based on the information that was obtain from calling the AvailableClientSideComponents methods on the <see cref="IPage"/> object.
 /// </summary>
 /// <param name="component">Component to create a ClientSideWebPart instance for</param>
 public PageWebPart(IPageComponent component) : this()
 {
     if (component == null)
     {
         throw new ArgumentNullException(nameof(component));
     }
     Import(component as PageComponent);
 }
コード例 #3
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;
            }
        }