Example #1
0
        public void PageLayoutAnalyse_GetListOfWebPartsAssemblyReferenceWithNameSpace()
        {
            var result = WebParts.GetListOfWebParts("Microsoft.SharePoint.WebPartPages");

            result.ForEach(o =>
            {
                Console.WriteLine(o);
            });

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count > 0);
        }
Example #2
0
        public void PageLayoutAnalyse_GetListOfWebPartsAssemblyReference()
        {
            var result = WebParts.GetListOfWebParts();

            result.ForEach(o =>
            {
                Console.WriteLine(o);
            });

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count > 0);
        }
        /// <summary>
        /// Extract the web parts from the page layout HTML outside of web part zones
        /// </summary>
        internal ExtractedHtmlBlocksEntity ExtractControlsFromPageLayoutHtml(ListItem pageLayout)
        {
            /*Plan
             * Scan through the file to find the web parts by the tags
             * Extract and convert to definition
             */

            ExtractedHtmlBlocksEntity extractedHtmlBlocks = new ExtractedHtmlBlocksEntity();

            // Data from SharePoint
            pageLayout.EnsureProperties(o => o.File, o => o.File.ServerRelativeUrl);
            var fileUrl  = pageLayout.File.ServerRelativeUrl;
            var fileHtml = LoadPageLayoutFile(fileUrl);

            // replace cdata tags to 'fool' AngleSharp
            fileHtml = fileHtml.Replace("<![CDATA[", "<encodeddata>");
            fileHtml = fileHtml.Replace("]]>", "</encodeddata>");

            using (var document = this.parser.Parse(fileHtml))
            {
                // Item 1 - WebPart Name, Item 2 - Full assembly reference
                List <Tuple <string, string> > possibleWebPartsUsed = new List <Tuple <string, string> >();
                List <IEnumerable <IElement> > multipleTagFinds     = new List <IEnumerable <IElement> >();

                //List of all the assembly references and prefixes in the page
                List <Tuple <string, string> > prefixesAndNameSpaces = ExtractWebPartPrefixesFromNamespaces(pageLayout);

                // Determine the possible web parts from the page from the namespaces used in the aspx header
                prefixesAndNameSpaces.ForEach(p =>
                {
                    var possibleParts = WebParts.GetListOfWebParts(p.Item2);
                    foreach (var part in possibleParts)
                    {
                        var webPartName = part.Substring(0, part.IndexOf(",")).Replace($"{p.Item2}.", "");
                        possibleWebPartsUsed.Add(new Tuple <string, string>(webPartName, part));
                    }
                });

                // Cycle through all the nodes in the document
                foreach (var docNode in document.All)
                {
                    foreach (var prefixAndNameSpace in prefixesAndNameSpaces)
                    {
                        if (docNode.TagName.Contains(prefixAndNameSpace.Item1.ToUpper()))
                        {
                            // Expand, as this may contain many elements
                            //foreach (var control in tagFind)
                            //{

                            var attributes = docNode.Attributes;

                            if (attributes.Any(o => o.Name == "fieldname"))
                            {
                                var fieldName = attributes["fieldname"].Value;

                                //DeDup - Some controls can be inside an edit panel
                                if (!extractedHtmlBlocks.WebPartFields.Any(o => o.Name == fieldName))
                                {
                                    List <WebPartProperty> webPartProperties = new List <WebPartProperty>();

                                    foreach (var attr in attributes)
                                    {
                                        // This might need a filter

                                        webPartProperties.Add(new WebPartProperty()
                                        {
                                            Name      = attr.Name,
                                            Type      = WebPartProperyType.@string,
                                            Functions = "" // Need defaults here
                                        });
                                    }

                                    extractedHtmlBlocks.WebPartFields.Add(new WebPartField()
                                    {
                                        Name          = fieldName,
                                        TargetWebPart = "",
                                        Row           = 1,
                                        //RowSpecified = true,
                                        Column = 1,
                                        //ColumnSpecified = true,
                                        Property = webPartProperties.ToArray()
                                    });
                                }
                            }

                            if (docNode.TagName.Contains("WEBPARTZONE"))
                            {
                                extractedHtmlBlocks.WebPartZones.Add(new WebPartZone()
                                {
                                    ZoneId = docNode.Id,
                                    Column = 1,
                                    Row    = 1,
                                    //ZoneIndex = control. // TODO: Is this used?
                                });
                            }

                            //Fixed web part zone
                            //This should only find one match
                            var matchedParts = possibleWebPartsUsed.Where(o => o.Item1.ToUpper() == docNode.TagName.Replace($"{prefixAndNameSpace.Item1.ToUpper()}:", ""));

                            if (matchedParts.Any())
                            {
                                var match = matchedParts.FirstOrDefault();
                                if (match != default(Tuple <string, string>))
                                {
                                    //Process Child properties
                                    List <FixedWebPartProperty> fixedProperties = new List <FixedWebPartProperty>();
                                    if (docNode.HasChildNodes && docNode.FirstElementChild != null && docNode.FirstElementChild.HasChildNodes)
                                    {
                                        var childProperties = docNode.FirstElementChild.ChildNodes;
                                        foreach (var childProp in childProperties)
                                        {
                                            if (childProp.NodeName != "#text")
                                            {
                                                var stronglyTypedChild = (IElement)childProp;
                                                //var content = !string.IsNullOrEmpty(childProp.TextContent) ? childProp.TextContent : stronglyTypedChild.InnerHtml;
                                                var content = stronglyTypedChild.InnerHtml;

                                                fixedProperties.Add(new FixedWebPartProperty()
                                                {
                                                    Name  = stronglyTypedChild.NodeName,
                                                    Type  = WebPartProperyType.@string,
                                                    Value = EncodingAndCleanUpContent(content)
                                                });
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // Another scenario where there are no child nodes, just attributes
                                        foreach (var attr in attributes)
                                        {
                                            // This might need a filter

                                            fixedProperties.Add(new FixedWebPartProperty()
                                            {
                                                Name  = attr.Name,
                                                Type  = WebPartProperyType.@string,
                                                Value = attr.Value
                                            });
                                        }
                                    }

                                    extractedHtmlBlocks.FixedWebParts.Add(new FixedWebPart()
                                    {
                                        Column = 1,
                                        //ColumnSpecified = true,
                                        Row = 1,
                                        //RowSpecified = true,
                                        Type     = match.Item2,
                                        Property = fixedProperties.ToArray()
                                    });
                                }
                            }
                        }
                    }
                }
            }

            return(extractedHtmlBlocks);
        }