private void GetPiwikAdminProperties()
        {
            try
            {
                PropertyValues adminBag = ctx.Site.RootWeb.AllProperties;
                ctx.Load(adminBag);
                ctx.ExecuteQueryRetry();
                PropertyBagList.Add(new PropertyBagEntity("PiwikPro Site Directory Listname", ConfigValues.PiwikPro_PropertyBag_PiwikListName, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_PiwikListName]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("EnforceSslComunication", ConfigValues.PiwikPro_PropertyBag_EnforceSslComunication, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_EnforceSslComunication]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("Url", ConfigValues.PiwikPro_PropertyBag_ServiceUrl, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_ServiceUrl]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("ContainersUrl", ConfigValues.PiwikPro_PropertyBag_PiwikContainersUrl, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_PiwikContainersUrl]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("TrackerJSScriptURL", ConfigValues.PiwikPro_PropertyBag_TrackerJSScriptUrl, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_TrackerJSScriptUrl]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("UseSha3", ConfigValues.PiwikPro_PropertyBag_Sha3, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_Sha3]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));

                PropertyBagList.Add(new PropertyBagEntity("TemplateSendUserIdEncoded", ConfigValues.PiwikPro_PropertyBag_TemplateSendUserIdEncoded, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_TemplateSendUserIdEncoded]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("TemplateShouldTrackDocumentAddedGoal", ConfigValues.PiwikPro_PropertyBag_TemplateShouldTrackDocumentAddedGoal, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_TemplateShouldTrackDocumentAddedGoal]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("TemplateShouldTrackPageAddedGoal", ConfigValues.PiwikPro_PropertyBag_TemplateShouldTrackPageAddedGoal, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_TemplateShouldTrackPageAddedGoal]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("TemplateShouldTrackPageEditedGoal", ConfigValues.PiwikPro_PropertyBag_TemplateShouldTrackPageEditedGoal, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_TemplateShouldTrackPageEditedGoal]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("TemplateDepartment", ConfigValues.PiwikPro_PropertyBag_TemplateDepartment, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_TemplateDepartment]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("TemplateJobTitle", ConfigValues.PiwikPro_PropertyBag_TemplateJobTitle, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_TemplateJobTitle]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("TemplateUserName", ConfigValues.PiwikPro_PropertyBag_TemplateUsername, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_TemplateUsername]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("TemplateOffice", ConfigValues.PiwikPro_PropertyBag_TemplateOffice, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_TemplateOffice]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
                PropertyBagList.Add(new PropertyBagEntity("TemplateSendExtendedUserinfo", ConfigValues.PiwikPro_PropertyBag_TemplateSendExtendedUserinfo, Convert.ToString(adminBag[ConfigValues.PiwikPro_PropertyBag_TemplateSendExtendedUserinfo]), PropertyBagEntity.PropertyLevelEnum.PiwikAdmin));
            }
            catch (Exception ex)
            {
                logger.WriteLog(Category.Unexpected, "Piwik GetPiwikAdminProperties", ex.Message);
            }
        }
        private void SetEnablePropertyBagChange(string adminTenantUrl, string propertyBagContextUrl, string userLogin, string userPassword, ISPLogger logger)
        {
            try
            {
                // using (ClientContext tenantCtx = new ClientContext(adminTenantUrl))
                OfficeDevPnP.Core.AuthenticationManager authMan = new OfficeDevPnP.Core.AuthenticationManager();
                using (ClientContext tenantCtx = authMan.GetAppOnlyAuthenticatedContext(adminTenantUrl, userLogin, userPassword))
                {
                    var tenant         = new Tenant(tenantCtx);
                    var siteProperties = tenant.GetSitePropertiesByUrl(propertyBagContextUrl, true);
                    tenant.Context.Load(siteProperties);
                    tenant.Context.ExecuteQueryRetry();

                    siteProperties.DenyAddAndCustomizePages = DenyAddAndCustomizePagesStatus.Disabled;
                    var operation = siteProperties.Update();
                    tenant.Context.Load(operation, i => i.IsComplete, i => i.PollingInterval);
                    tenant.Context.ExecuteQueryRetry();

                    // this is necessary, because the setting is not immediately reflected after ExecuteQuery
                    while (!operation.IsComplete)
                    {
                        Thread.Sleep(operation.PollingInterval);
                        operation.RefreshLoad();
                        if (!operation.IsComplete)
                        {
                            try
                            {
                                tenant.Context.ExecuteQueryRetry();
                            }
                            catch (WebException webEx)
                            {
                                logger.WriteLog(Category.Unexpected, "Piwik Set DenyAddAndCustomizePagesStatus inside site", webEx.Message);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.WriteLog(Category.Unexpected, "Piwik Set DenyAddAndCustomizePagesStatus inside site Full method", ex.Message);
            }
        }
Beispiel #3
0
        public void SetSetGdprOffInPiwik(string siteID)
        {
            try
            {
                //JSON should look like: "{\n\"data\": {\n\"attributes\":{\n\n\"gdpr\":false\n},\n\"type\":\"ppms/app\",\n\"id\":\"" + siteID + "\"\n}\n}\n";
                JObject jobj = new JObject(
                    new JProperty("data",
                                  new JObject(
                                      new JProperty("attributes",
                                                    new JObject(
                                                        new JProperty("gdpr", false))),
                                      new JProperty("type", "ppms/app"),
                                      new JProperty("id", siteID))));

                var callCommandUrl = new Uri(String.Format("https://{0}{1}\\{2}", piwik_serviceUrl, _apiAppsV2, siteID));

                string returnerXml = MakeRequest(jobj.ToString(), callCommandUrl, "PATCH");
            }
            catch (Exception ex)
            {
                logger.WriteLog(Category.Unexpected, "Piwik SetSetGdprOffInPiwik", ex.Message);
            }
        }
        public bool AddOrUpdateElementInList(string title, string status, string url, string errorlog, string relativeUrl, string siteId)
        {
            bool statusNotChangedEarlier = true;
            bool itemToDelete            = false;

            try
            {
                List     oList     = context.Web.Lists.GetByTitle(configuration.ListName);
                ListItem itemToAdd = CheckIfElementIsAlreadyOnList(relativeUrl);
                if (itemToAdd != null)
                {
                    if (Convert.ToString(itemToAdd[ConfigValues.PiwikPro_SiteDirectory_Column_Status]) == ConfigValues.PiwikPro_SiteDirectory_Column_Status_New && status == ConfigValues.PiwikPro_SiteDirectory_Column_Status_Deactivating && string.IsNullOrEmpty(Convert.ToString(itemToAdd[ConfigValues.PiwikPro_SiteDirectory_Column_SiteID])))
                    {
                        itemToDelete = true;
                    }
                    else
                    {
                        if (Convert.ToString(itemToAdd[ConfigValues.PiwikPro_SiteDirectory_Column_Status]) == ConfigValues.PiwikPro_SiteDirectory_Column_Status_Deactivating && status == ConfigValues.PiwikPro_SiteDirectory_Column_Status_New)
                        {
                            itemToAdd[ConfigValues.PiwikPro_SiteDirectory_Column_Status] = ConfigValues.PiwikPro_SiteDirectory_Column_Status_Active;
                            statusNotChangedEarlier = false;
                        }

                        if (Convert.ToString(itemToAdd[ConfigValues.PiwikPro_SiteDirectory_Column_Status]) == ConfigValues.PiwikPro_SiteDirectory_Column_Status_New && status == ConfigValues.PiwikPro_SiteDirectory_Column_Status_Deactivating)
                        {
                            itemToAdd[ConfigValues.PiwikPro_SiteDirectory_Column_Status] = ConfigValues.PiwikPro_SiteDirectory_Column_Status_NoActive;
                            statusNotChangedEarlier = false;
                        }

                        if (statusNotChangedEarlier)
                        {
                            itemToAdd[ConfigValues.PiwikPro_SiteDirectory_Column_Status] = status;
                        }

                        itemToAdd[ConfigValues.PiwikPro_SiteDirectory_Column_Title] = title;

                        itemToAdd[ConfigValues.PiwikPro_SiteDirectory_Column_Url]      = url;
                        itemToAdd[ConfigValues.PiwikPro_SiteDirectory_Column_ErrorLog] = errorlog;
                        if (!string.IsNullOrEmpty(siteId))
                        {
                            itemToAdd[ConfigValues.PiwikPro_SiteDirectory_Column_SiteID] = siteId;
                        }
                    }
                    if (!itemToDelete)
                    {
                        itemToAdd.Update();
                        context.ExecuteQueryRetry();
                    }
                    else
                    {
                        itemToAdd.DeleteObject();
                        context.ExecuteQueryRetry();
                    }
                }
                else
                {
                    ListItem newItem = oList.AddItem(new ListItemCreationInformation());
                    newItem[ConfigValues.PiwikPro_SiteDirectory_Column_Title]    = title;
                    newItem[ConfigValues.PiwikPro_SiteDirectory_Column_Status]   = status;
                    newItem[ConfigValues.PiwikPro_SiteDirectory_Column_Url]      = url;
                    newItem[ConfigValues.PiwikPro_SiteDirectory_Column_ErrorLog] = errorlog;
                    if (!string.IsNullOrEmpty(siteId))
                    {
                        newItem[ConfigValues.PiwikPro_SiteDirectory_Column_SiteID] = siteId;
                    }
                    newItem.Update();
                    context.ExecuteQueryRetry();
                }
            }
            catch (Exception ex)
            {
                logger.WriteLog(Category.Unexpected, "Piwik AddOrUpdateElementInList", ex.Message);
            }
            return(statusNotChangedEarlier);
        }
        public void GetAllNewSitesAndOperate(ClientContext clientContext, string spOnlineUserLogin, string spOnlineUserPassword, string adminTenantUrl)
        {
            try
            {
                ListProcessor sdlo = new ListProcessor(clientContext, cfg, logger);

                //Get all Sites with status "New" and put it to Piwik
                foreach (ListItem item in sdlo.GetAllNewSites())
                {
                    //connect to service and create new site
                    PiwikPROServiceOperations pso = new PiwikPROServiceOperations(cfg.PiwikClientID, cfg.PiwikClientSecret, cfg.PiwikServiceUrl, cfg.PiwikOldApiToken, logger);

                    FieldUrlValue valueUrl = (FieldUrlValue)(item[ConfigValues.PiwikPro_SiteDirectory_Column_Url]);

                    string idSite = string.Empty;
                    bool   isSiteAlreadyOnPiwik = false;
                    if (!string.IsNullOrEmpty(Convert.ToString(item[ConfigValues.PiwikPro_SiteDirectory_Column_SiteID])))
                    {
                        isSiteAlreadyOnPiwik = pso.CheckIfPageIsAlreadyOnPiwik(Convert.ToString(item[ConfigValues.PiwikPro_SiteDirectory_Column_SiteID]));
                    }
                    if (isSiteAlreadyOnPiwik)
                    {
                        idSite = Convert.ToString(item[ConfigValues.PiwikPro_SiteDirectory_Column_SiteID]);
                        pso.ChangeNameSiteInPiwik(Convert.ToString(item[ConfigValues.PiwikPro_SiteDirectory_Column_Title]), idSite);
                    }
                    else
                    {
                        idSite = pso.AddSiteToPiwik(Convert.ToString(item[ConfigValues.PiwikPro_SiteDirectory_Column_Title]), valueUrl.Url);
                    }

                    if (idSite.Contains("Error: "))
                    {
                        item[ConfigValues.PiwikPro_SiteDirectory_Column_Status]   = ConfigValues.PiwikPro_SiteDirectory_Column_Status_Error;
                        item[ConfigValues.PiwikPro_SiteDirectory_Column_ErrorLog] = idSite;
                    }
                    else
                    {
                        try
                        {
                            bool?         ShouldTrackDocumentAddedGoal = null;
                            bool?         ShouldTrackPageAddedGoal     = null;
                            bool?         ShouldTrackPageEditedGoal    = null;
                            ClientContext contextToPropBag;

                            if (!string.IsNullOrEmpty(spOnlineUserLogin))
                            {
                                OfficeDevPnP.Core.AuthenticationManager authMan = new OfficeDevPnP.Core.AuthenticationManager();
                                contextToPropBag = authMan.GetAppOnlyAuthenticatedContext(valueUrl.Url, spOnlineUserLogin, spOnlineUserPassword);
                                SetEnablePropertyBagChange(adminTenantUrl, valueUrl.Url, spOnlineUserLogin, spOnlineUserPassword, logger);
                            }
                            else
                            {
                                contextToPropBag = new ClientContext(valueUrl.Url);
                            }


                            if (isSiteAlreadyOnPiwik)
                            {
                                //operations if site was active before
                            }
                            else
                            {
                                //copy template values from piwikadmin
                                foreach (PropertyBagEntity pbe in cfg.PropertyBagList)
                                {
                                    if (pbe.PropertyTitle.StartsWith("Template"))
                                    {
                                        CreateOrUpdateValueInPropertyBag(pbe.PropertyValue, contextToPropBag, pbe.PropertyName.Replace("template", ""));
                                    }
                                }
                            }

                            CreateOrUpdateValueInPropertyBag(idSite, contextToPropBag, ConfigValues.PiwikPro_PropertyBag_SiteId);
                            CreateOrUpdateValueInPropertyBag("true", contextToPropBag, ConfigValues.PiwikPro_PropertyBag_PiwikIsTrackingActive);

                            //set gdpr off
                            pso.SetSetGdprOffInPiwik(idSite);

                            //create/update values in propbag
                            PropertyValues currentBag = contextToPropBag.Site.RootWeb.AllProperties;
                            contextToPropBag.Load(currentBag);
                            contextToPropBag.ExecuteQueryRetry();

                            //Prepare goals
                            ShouldTrackDocumentAddedGoal = CheckIfValuesAreIntOrBoolAndReturn(Convert.ToString(currentBag[ConfigValues.PiwikPro_PropertyBag_ShouldTrackDocumentAddedGoal]));
                            ShouldTrackPageAddedGoal     = CheckIfValuesAreIntOrBoolAndReturn(Convert.ToString(currentBag[ConfigValues.PiwikPro_PropertyBag_ShouldTrackPageAddedGoal]));
                            ShouldTrackPageEditedGoal    = CheckIfValuesAreIntOrBoolAndReturn(Convert.ToString(currentBag[ConfigValues.PiwikPro_PropertyBag_ShouldTrackPageEditedGoal]));


                            //Add goals to Piwik site
                            AddGoalToPiwikAndWriteToPropertyBag(pso, ShouldTrackDocumentAddedGoal, idSite, ConfigValues.PiwikPro_PropertyBag_DocumentAddedGoalId, "Document added", currentBag, contextToPropBag);
                            AddGoalToPiwikAndWriteToPropertyBag(pso, ShouldTrackPageAddedGoal, idSite, ConfigValues.PiwikPro_PropertyBag_PageAddedGoalId, "Page added", currentBag, contextToPropBag);
                            AddGoalToPiwikAndWriteToPropertyBag(pso, ShouldTrackPageEditedGoal, idSite, ConfigValues.PiwikPro_PropertyBag_PageEditedGoalId, "Page edited", currentBag, contextToPropBag);

                            AddPropBagValuesToIndexedProperties(contextToPropBag);
                        }
                        catch (Exception exp)
                        {
                            item[ConfigValues.PiwikPro_SiteDirectory_Column_ErrorLog] = exp.Message;
                            logger.WriteLog(Category.Unexpected, "Piwik GetAllNewSitesAndOperate", exp.Message);
                        }

                        item[ConfigValues.PiwikPro_SiteDirectory_Column_Status] = ConfigValues.PiwikPro_SiteDirectory_Column_Status_Active;
                        item[ConfigValues.PiwikPro_SiteDirectory_Column_SiteID] = idSite;
                    }

                    item.Update();
                    clientContext.ExecuteQueryRetry();
                }
            }
            catch (Exception expcc)
            {
                logger.WriteLog(Category.Unexpected, "Piwik GetAllNewSitesAndOperateOnFinish", expcc.Message);
            }
        }