public override void OnTermStoreRunningStart(TermStore termStore)
        {
            termStore.Context.Load(termStore,
                                   s => s.Name);
            termStore.Context.ExecuteQuery();

            TermInfo termInfo = new TermInfo()
            {
                Type = Type.TermStore,
                Name = termStore.Name
            };

            // Write CSV
            managedMetadataFileWriter.WriteRecord(termInfo);
        }
        public override void OnSiteCollectionRunningEnd(Site site, Web rootWeb)
        {
            // Crawl groups
            groupIds = groupIds.Distinct().ToList();

            rootWeb.Context.Load(rootWeb,
                                 w => w.Url,
                                 w => w.SiteGroups);


            foreach (int groupId in groupIds)
            {
                Group group = rootWeb.SiteGroups.GetById(groupId);

                rootWeb.Context.Load(group,
                                     g => g.Id,
                                     g => g.Title,
                                     g => g.Users.Include(
                                         u => u.LoginName));
                rootWeb.Context.ExecuteQuery();

                GroupInfo groupInfo = new GroupInfo(rootWeb.Url, group);

                // Write CSV
                permissionsGroupsFileWriter.WriteRecord(groupInfo);
            }
        }
        /// <summary>
        /// Export the properties of a securable objet to CSV
        /// </summary>
        /// <param name="securableObject">Securable object</param>
        private void WriteCsv(SecurableObject securableObject)
        {
            LoadSecurableObject(securableObject);

            SecurableObjectInfo securableObjectInfo = null;

            foreach (RoleAssignment roleAssignment in securableObject.RoleAssignments)
            {
                switch (securableObject)
                {
                case Web web:
                    securableObjectInfo = new SecurableObjectInfo(web, roleAssignment);
                    break;

                case List list:
                    securableObjectInfo = new SecurableObjectInfo(list, roleAssignment);
                    break;

                case ListItem listItem:
                    securableObjectInfo = new SecurableObjectInfo(listItem, roleAssignment);
                    break;
                }

                // Write CSV
                permissionsFileWriter.WriteRecord(securableObjectInfo);

                if (roleAssignment.Member.PrincipalType == PrincipalType.SharePointGroup && !groupIds.Contains(roleAssignment.Member.Id))
                {
                    groupIds.Add(roleAssignment.Member.Id);
                }
            }
        }
        public override void OnSiteRunningStart(Web web)
        {
            web.Context.Load(web,
                             w => w.Title,
                             w => w.Url,
                             w => w.Lists.Include(
                                 l => l.RootFolder.ServerRelativeUrl));
            web.Context.ExecuteQuery();

            List pages = web.Lists.FirstOrDefault(l => l.RootFolder.ServerRelativeUrl.Equals($"{web.ServerRelativeUrl}/Pages", StringComparison.InvariantCultureIgnoreCase));

            if (pages == null)
            {
                pages = web.Lists.FirstOrDefault(l => l.RootFolder.ServerRelativeUrl.Equals($"{web.ServerRelativeUrl}/SitePages", StringComparison.InvariantCultureIgnoreCase));
            }

            if (pages != null)
            {
                web.Context.Load(pages);
                web.Context.ExecuteQuery();

                string        viewXml    = "<View Scope='RecursiveAll'><Query><Where><And><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq><Eq><FieldRef Name='File_x0020_Type' /><Value Type='text'>aspx</Value></Eq></And></Where></Query></View>";
                List <string> viewFields = new List <string>()
                {
                    "FileRef",
                    "FileLeafRef"
                };

                IEnumerable <ListItem> items = LoadItems(pages, viewXml, viewFields, li => li.File.Exists, li => li.FileSystemObjectType);

                foreach (ListItem item in items)
                {
                    if (item.FileSystemObjectType == FileSystemObjectType.File && item.File.Exists)
                    {
                        LimitedWebPartManager webPartManager = item.File.GetLimitedWebPartManager(PersonalizationScope.Shared);
                        web.Context.Load(webPartManager,
                                         wpm => wpm.WebParts);
                        web.Context.ExecuteQuery();

                        PageInfo pageInfo = new PageInfo(web, item, webPartManager);

                        // Write CSV
                        webPartsFileWriter.WriteRecord(pageInfo);
                    }
                }
            }
        }
        public override void OnSiteCollectionRunningStart(Site site, Web rootWeb)
        {
            site.Context.Load(site,
                              s => s.Url);
            site.Context.Load(rootWeb,
                              w => w.Title,
                              w => w.Url,
                              w => w.SiteGroups.Include(
                                  g => g.Title));
            site.Context.ExecuteQuery();

            string groupsFileName = $"AuditGroups-{rootWeb.Title}-{DateTime.Now.ToString("yyyy-MM-dd HH,mm,ss")}.csv";
            CsvWriterWrapper <GroupUserInfo, GroupUserInfoMap> groupsFileWriter = new CsvWriterWrapper <GroupUserInfo, GroupUserInfoMap>(groupsFileName);

            foreach (string groupName in GroupNames)
            {
                Group group = rootWeb.SiteGroups.FirstOrDefault(g => g.Title.ToLowerInvariant().Contains(groupName.ToLowerInvariant()));

                if (group != null)
                {
                    site.Context.Load(group,
                                      g => g.Users.Include(
                                          u => u.Title,
                                          u => u.LoginName,
                                          u => u.Email));
                    site.Context.ExecuteQuery();

                    foreach (User user in group.Users)
                    {
                        GroupUserInfo groupUserInfo = new GroupUserInfo(site, rootWeb, group, user);

                        // Write CSV
                        groupsFileWriter.WriteRecord(groupUserInfo);
                    }
                }
            }
        }
 public override void OnSiteRunningEnd(Web web)
 {
     // Write CSV
     webUsageFileWriter.WriteRecord(webUsageInfo);
 }