public static void Run([QueueTrigger("projectdesignqueue")] SiteInformation myQueueItem, TraceWriter log, ExecutionContext functionContext)
        {
            try
            {
                var clientID     = ConfigurationManager.AppSettings["SPO_AppId"];
                var clientSecret = ConfigurationManager.AppSettings["SPO_AppSecret"];

                log.Info($"Fetched client ID '{clientID}' from AppSettings.'");

                ClientCredentials provisioningCreds = new ClientCredentials {
                    ClientID = clientID, ClientSecret = clientSecret
                };
                applyTemplate(myQueueItem, functionContext, provisioningCreds, log);
            }
            catch (Exception e)
            {
                log.Error($"Error when running ApplyPnPTemplate function. Exception: {e}");
                throw;
            }
        }
        private static void applyTemplate(SiteInformation siteInformation, ExecutionContext functionContext, ClientCredentials credentials, TraceWriter log)
        {
            try
            {
                using (var ctx = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteInformation.SiteUrl, credentials.ClientID, credentials.ClientSecret))
                {
                    Web web = ctx.Web;
                    ctx.Load(web, w => w.Title, w => w.Navigation.QuickLaunch);
                    ctx.ExecuteQueryRetry();

                    string groupID = GetSiteGroupID(ctx);
                    // UpdateSubscriptionItemProperties(credentials, siteInformation, web.Title ,log);
                    var rootSiteUrl = ConfigurationManager.AppSettings["RootSiteUrl"];
                    log.Info($"Successfully connected to site: {web.Title}");

                    var navigationcolls = web.Navigation.QuickLaunch;
                    foreach (var nav in navigationcolls)
                    {
                        if (nav.Title == "Key Dates & Deliverables")
                        {
                            string url = nav.Url.Split('?')[0] + "?groupId=" + groupID + "&planId=" + siteInformation.PlanId;
                            nav.Url = url;
                            nav.Update();
                            ctx.ExecuteQueryRetry();
                        }
                    }

                    string              currentDirectory = functionContext.FunctionDirectory;
                    DirectoryInfo       dInfo            = new DirectoryInfo(currentDirectory);
                    var                 schemaDir        = dInfo.Parent.FullName + "\\Templates";
                    XMLTemplateProvider sitesProvider    = new XMLFileSystemTemplateProvider(schemaDir, "");

                    log.Info($"About to get template with with filename '{PNP_TEMPLATE_FILE}'");

                    ProvisioningTemplate template = sitesProvider.GetTemplate(PNP_TEMPLATE_FILE);

                    var getHomeClientPage = template.ClientSidePages.Find(i => i.Title == "Home");
                    if (getHomeClientPage != null)
                    {
                        UpdateControlsDataDynamic(siteInformation, getHomeClientPage);
                    }

                    log.Info($"Successfully found template with ID '{template.Id}'");

                    ProvisioningTemplateApplyingInformation ptai = new ProvisioningTemplateApplyingInformation
                    {
                        ProgressDelegate = (message, progress, total) =>
                        {
                            log.Info(string.Format("{0:00}/{1:00} - {2}", progress, total, message));
                        }
                    };

                    // Associate file connector for assets..
                    FileSystemConnector connector = new FileSystemConnector(Path.Combine(currentDirectory, "Files"), "");
                    template.Connector = connector;

                    web.ApplyProvisioningTemplate(template, ptai);

                    if (siteInformation.IsTopNavigation)
                    {
                        // Add top navigation bar.
                        web.AddNavigationNode("SharePoint Main Menu", new Uri(rootSiteUrl + "/SitePages/Home.aspx"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar);
                        web.AddNavigationNode("Document Centre", new Uri(rootSiteUrl + "/Document%20Centre/SitePages/Home.aspx"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar);
                        web.AddNavigationNode("Project Centre", new Uri(rootSiteUrl + "/Project%20Centre/SitePages/Home.aspx"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar);
                        web.AddNavigationNode("WHS Centre", new Uri(rootSiteUrl + "/WHS%20Centre/"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar);
                        web.AddNavigationNode("Training Centre", new Uri(rootSiteUrl + "/Training%20Centre"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar);
                        web.AddNavigationNode("Proposal Hub", new Uri(rootSiteUrl + "/Proposal%20Hub"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar);
                    }

                    UpdateListTitle(ctx, web, "01. Project Management");
                }
            }
            catch (Exception e)
            {
                log.Error("Error when applying PnP template!", e);
                throw;
            }
        }
        public static void UpdateSubscriptionItemProperties(ClientCredentials credentials, SiteInformation siteInformation, string webTitle, TraceWriter log)
        {
            try
            {
                var listTitle      = ConfigurationManager.AppSettings["ProjectListTitle"];
                var projectSiteUrl = ConfigurationManager.AppSettings["ProjectSiteUrl"];
                using (var ctxProjectSite = new AuthenticationManager().GetAppOnlyAuthenticatedContext(projectSiteUrl, credentials.ClientID, credentials.ClientSecret))
                {
                    Web projectWeb = ctxProjectSite.Web;
                    ctxProjectSite.Load(projectWeb, w => w.Title);
                    ctxProjectSite.ExecuteQueryRetry();

                    List      oList     = projectWeb.Lists.GetByTitle(listTitle);
                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/>" +
                                        "<Value Type='Text'>" + webTitle + "</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>";
                    ListItemCollection collListItem = oList.GetItems(camlQuery);

                    ctxProjectSite.Load(collListItem);
                    ctxProjectSite.ExecuteQueryRetry();

                    foreach (ListItem oListItem in collListItem)
                    {
                        siteInformation.Description            = oListItem["Description"]?.ToString();
                        siteInformation.ProposalDirector       = oListItem["PD_x002f_PM"]?.ToString();
                        siteInformation.ProposalDeadLineDate   = oListItem["ProposalDeadline"]?.ToString();
                        siteInformation.ProposalStartDate      = oListItem["ProjectStartDate"]?.ToString();
                        siteInformation.ProposalManager        = oListItem["ProposalManager"]?.ToString();
                        siteInformation.ProjectLocationName    = oListItem["ProjectLocationName"]?.ToString();
                        siteInformation.ProjectLocationAddress = oListItem["ProjectLocationAddress"]?.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("Error when UpdateSubscriptionItemProperties", ex);
            }
        }
        public static void UpdateControlsDataDynamic(SiteInformation siteInformation, ClientSidePage getHomeClientPage)
        {
            string proposalDateTime  = string.Empty;
            string proposalStartDate = string.Empty;

            if (!string.IsNullOrWhiteSpace(siteInformation.ProposalDeadLineDate))
            {
                var proposalDate = Convert.ToDateTime(siteInformation.ProposalDeadLineDate);
                proposalDateTime = string.Format("{0} of {1}, {2} {3}", proposalDate.Day.Ordinal(), proposalDate.ToString("MMMM"), proposalDate.ToString("yyyy"), proposalDate.ToString("hh:mm tt"));
            }
            if (!string.IsNullOrWhiteSpace(siteInformation.ProposalStartDate))
            {
                var proposalDate = Convert.ToDateTime(siteInformation.ProposalStartDate);
                proposalStartDate = string.Format("{0} of {1}, {2} {3}", proposalDate.Day.Ordinal(), proposalDate.ToString("MMMM"), proposalDate.ToString("yyyy"), proposalDate.ToString("hh:mm tt"));
            }

            siteInformation.ProposalStartDate    = proposalStartDate;
            siteInformation.ProposalDeadLineDate = proposalDateTime;

            var allSectionControls = getHomeClientPage.Sections?[0].Controls;

            allSectionControls?[0].ControlProperties.Remove("Text");
            allSectionControls?[0].ControlProperties.Add("Text", "<h4>Project Description -</h4><h4><span><span><span>" + siteInformation.Description + "</span></span></span></h4>");

            allSectionControls?[1].ControlProperties.Remove("Text");
            allSectionControls?[1].ControlProperties.Add("Text", "<h4>Project Start Date -&nbsp; " + siteInformation.ProposalStartDate + "&nbsp;</h4>" +
                                                         "                                        <br/> <h4>Delivery Date -&nbsp; " + siteInformation.ProposalDeadLineDate + "&nbsp;</h4>");

            var proposalmanagercontrol = getHomeClientPage.Sections?[2].Controls?[1];

            if (proposalmanagercontrol != null)
            {
                string managerEmailId     = "";
                string managerDisplayName = "";
                if (!string.IsNullOrWhiteSpace(siteInformation.ProposalManager))
                {
                    var managercoll = siteInformation.ProposalManager.Split(';');
                    managerEmailId     = managercoll[1];
                    managerDisplayName = managercoll[0];
                }
                var jsondata = proposalmanagercontrol.JsonControlData?.
                               Replace("*****@*****.**", managerEmailId).
                               Replace("kirsty davies", managerDisplayName);
                proposalmanagercontrol.JsonControlData = jsondata;
            }

            var proposaldirectorcontrol = getHomeClientPage.Sections?[2].Controls?[0];

            if (proposaldirectorcontrol != null)
            {
                string directorEmailId     = "";
                string directorDisplayName = "";
                if (!string.IsNullOrWhiteSpace(siteInformation.ProposalDirector))
                {
                    var proposalcoll = siteInformation.ProposalDirector.Split(';');
                    directorEmailId     = proposalcoll[1];
                    directorDisplayName = proposalcoll[0];
                }
                var jsondata = proposaldirectorcontrol.JsonControlData?.
                               Replace("*****@*****.**", directorEmailId).
                               Replace("John Merrell", directorDisplayName);
                proposaldirectorcontrol.JsonControlData = jsondata;
            }

            var projectLocationControl = getHomeClientPage.Sections?[2].Controls?[2];

            if (projectLocationControl != null && !string.IsNullOrWhiteSpace(siteInformation.ProjectLocationName))
            {
                string jsondata = projectLocationControl.JsonControlData?.
                                  Replace("\"title\":\"\"", "\"title\": \"" + siteInformation.ProjectLocationName + "\"").
                                  Replace("\"defaultTitle\":\"\"", "\"defaultTitle\": \"" + siteInformation.ProjectLocationName + "\"").
                                  Replace("\"defaultAddress\":\"\"", "\"defaultAddress\": \"" + siteInformation.ProjectLocationAddress + "\"").
                                  Replace("\"address\":\"\"", "\"address\": \"" + siteInformation.ProjectLocationAddress + "\"");

                Point mapPoint = GetLocationGoogleAPI(!string.IsNullOrWhiteSpace(siteInformation.ProjectLocationAddress) ? siteInformation.ProjectLocationAddress : siteInformation.ProjectLocationName);
                if (mapPoint != null)
                {
                    jsondata = jsondata.Replace("\"latitude\":-32.54438", "\"latitude\":" + mapPoint.Coordinates[0] + "")
                               .Replace("\"longitude\":150.99636", "\"longitude\":" + mapPoint.Coordinates[1] + "");
                }
                projectLocationControl.JsonControlData = jsondata;
            }
        }