private ProvisioningTemplate GetFileContents(Web web, ProvisioningTemplate template, string welcomePageUrl, ProvisioningTemplateCreationInformation creationInfo, PnPMonitoredScope scope)
        {
            var homepageUrl = web.RootFolder.WelcomePage;

            if (string.IsNullOrEmpty(homepageUrl))
            {
                homepageUrl = "Default.aspx";
            }

            var fullUri = new Uri(UrlUtility.Combine(web.Url, homepageUrl));

            var folderPath = fullUri.Segments.Take(fullUri.Segments.Count() - 1).ToArray().Aggregate((i, x) => i + x).TrimEnd('/');
            var fileName   = fullUri.Segments[fullUri.Segments.Count() - 1];

            var webParts = web.GetWebParts(welcomePageUrl);

            var file = web.GetFileByServerRelativeUrl(welcomePageUrl);

            var homeFile = new Model.File()
            {
                Folder    = Tokenize(folderPath, web.Url),
                Src       = fileName,
                Overwrite = true,
            };

            // Add field values to file

            RetrieveFieldValues(web, file, homeFile);

            // Add WebParts to file
            foreach (var webPart in webParts)
            {
                var webPartxml = TokenizeWebPartXml(web, web.GetWebPartXml(webPart.Id, welcomePageUrl));

                Model.WebPart newWp = new Model.WebPart()
                {
                    Title    = webPart.WebPart.Title,
                    Row      = (uint)webPart.WebPart.ZoneIndex,
                    Order    = (uint)webPart.WebPart.ZoneIndex,
                    Contents = webPartxml
                };
#if !SP2016
                // As long as we've no CSOM library that has the ZoneID we can't use the version check as things don't compile...
                if (web.Context.HasMinimalServerLibraryVersion(Constants.MINIMUMZONEIDREQUIREDSERVERVERSION))
                {
                    newWp.Zone = webPart.ZoneId;
                }
#endif
                homeFile.WebParts.Add(newWp);
            }
            template.Files.Add(homeFile);

            // Persist file using connector
            if (creationInfo.PersistBrandingFiles)
            {
                creationInfo.PersistFile(folderPath, fileName, web, scope);
            }
            return(template);
        }
        private void PersistWorkflowForm(Web web, ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo, PnPMonitoredScope scope, String formUrl)
        {
            var fullUri = new Uri(UrlUtility.Combine(web.Url, formUrl));

            var folderPath = fullUri.Segments.Take(fullUri.Segments.Count() - 1).ToArray().Aggregate((i, x) => i + x).TrimEnd('/');
            var fileName   = fullUri.Segments[fullUri.Segments.Count() - 1];

            var formFile = new Model.File()
            {
                Folder    = Tokenize(folderPath, web.Url),
                Src       = formUrl,
                Overwrite = true,
            };

            // Add the file to the template
            template.Files.Add(formFile);

            // Persist file using connector
            creationInfo.PersistFile(folderPath, fileName, web, scope);
        }
 public void PersistFile(Web web, ProvisioningTemplateCreationInformation creationInfo, PnPMonitoredScope scope, string folderPath, string fileName, Boolean decodeFileName = false)
 {
     creationInfo.PersistFile(folderPath, fileName, web, scope, decodeFileName);
 }
        private void ExtractMasterPagesAndPageLayouts(Web web, ProvisioningTemplate template, PnPMonitoredScope scope, ProvisioningTemplateCreationInformation creationInfo)
        {
            web.EnsureProperty(w => w.Url);
            String webApplicationUrl = GetWebApplicationUrl(web.Url);

            if (!String.IsNullOrEmpty(webApplicationUrl))
            {
                // Get the Publishing Feature reference template
                ProvisioningTemplate publishingFeatureTemplate = GetPublishingFeatureBaseTemplate();

                // Get a reference to the root folder of the master page gallery
                var gallery = web.GetCatalog(116);
                web.Context.Load(gallery, g => g.RootFolder);
                web.Context.ExecuteQueryRetry();

                var masterPageGalleryFolder = gallery.RootFolder;

                // Load the files in the master page gallery
                web.Context.Load(masterPageGalleryFolder.Files);
                web.Context.ExecuteQueryRetry();


                var sourceFiles = GetFiles(masterPageGalleryFolder).Where(
                    f => f.Name.EndsWith(".aspx", StringComparison.InvariantCultureIgnoreCase) ||
                    f.Name.EndsWith(".html", StringComparison.InvariantCultureIgnoreCase) ||
                    f.Name.EndsWith(".master", StringComparison.InvariantCultureIgnoreCase));

                /*var sourceFiles = masterPageGalleryFolder.Files.AsEnumerable().Where(
                 *  f => f.Name.EndsWith(".aspx", StringComparison.InvariantCultureIgnoreCase) ||
                 *  f.Name.EndsWith(".html", StringComparison.InvariantCultureIgnoreCase) ||
                 *  f.Name.EndsWith(".master", StringComparison.InvariantCultureIgnoreCase));
                 */

                foreach (var file in sourceFiles)
                {
                    var listItem = file.EnsureProperty(f => f.ListItemAllFields);

                    if (!listItem.ServerObjectIsNull())
                    {
                        listItem.ContentType.EnsureProperties(ct => ct.Id, ct => ct.StringId);

                        // Check if the content type is of type Master Page or Page Layout
                        if (listItem.ContentType.StringId.StartsWith(MASTER_PAGE_CONTENT_TYPE_ID) ||
                            listItem.ContentType.StringId.StartsWith(PAGE_LAYOUT_CONTENT_TYPE_ID) ||
                            listItem.ContentType.StringId.StartsWith(ASP_NET_MASTER_PAGE_CONTENT_TYPE_ID) ||
                            listItem.ContentType.StringId.StartsWith(HTML_PAGE_LAYOUT_CONTENT_TYPE_ID))
                        {
                            // Skip any .ASPX or .MASTER file related to an .HTML designer file
                            if ((file.Name.EndsWith(".aspx", StringComparison.InvariantCultureIgnoreCase) &&
                                 sourceFiles.Any(f => f.Name.Equals(file.Name.ToLower().Replace(".aspx", ".html"),
                                                                    StringComparison.InvariantCultureIgnoreCase))) ||
                                (file.Name.EndsWith(".master", StringComparison.InvariantCultureIgnoreCase) &&
                                 sourceFiles.Any(f => f.Name.Equals(file.Name.ToLower().Replace(".master", ".html"),
                                                                    StringComparison.InvariantCultureIgnoreCase))))
                            {
                                continue;
                            }

                            // If the file is a custom one, and not one native
                            // and coming out from the publishing feature
                            if (creationInfo.IncludeNativePublishingFiles ||
                                !IsPublishingFeatureNativeFile(publishingFeatureTemplate, file.Name))
                            {
                                var fullUri = new Uri(UrlUtility.Combine(webApplicationUrl, file.ServerRelativeUrl));

                                var folderPath = fullUri.Segments.Take(fullUri.Segments.Count() - 1).ToArray().Aggregate((i, x) => i + x).TrimEnd('/');
                                var fileName   = fullUri.Segments[fullUri.Segments.Count() - 1];

                                var publishingFile = new Model.File()
                                {
                                    Folder    = Tokenize(folderPath, web.Url),
                                    Src       = HttpUtility.UrlDecode(fileName),
                                    Overwrite = true,
                                };

                                // Add field values to file
                                RetrieveFieldValues(web, file, publishingFile);

                                // Add the file to the template
                                template.Files.Add(publishingFile);

                                // Persist file using connector, if needed
                                if (creationInfo.PersistPublishingFiles)
                                {
                                    creationInfo.PersistFile(folderPath, fileName, web, scope, true);
                                }

                                if (listItem.ContentType.StringId.StartsWith(MASTER_PAGE_CONTENT_TYPE_ID))
                                {
                                    scope.LogWarning(String.Format("The file \"{0}\" is a custom MasterPage. Accordingly to the PnP Guidance (http://aka.ms/o365pnpguidancemasterpages) you should try to avoid using custom MasterPages.", file.Name));
                                }
                            }
                            else
                            {
                                scope.LogWarning(String.Format("Skipping file \"{0}\" because it is native in the publishing feature.", file.Name));
                            }
                        }
                    }
                }
            }
        }