コード例 #1
0
        /// <summary>
        /// Generate a portable JSON object from the List Template
        /// </summary>
        /// <param name="context">Client Context web</param>
        /// <param name="hostWeb">Client Context web</param>
        /// <param name="list">Hydrated SharePoint list Object</param>
        /// <param name="ExpandObjects">true - enumerate fields, views, content types</param>
        /// <param name="logger">Logger implementation for Verbose/Exception handling</param>
        /// <param name="skiptypes">Collection of field types to be used as a filter statement</param>
        /// <param name="siteGroups">Collection of hostWeb groups</param>
        /// <returns></returns>
        public static SPListDefinition GetListDefinition(this ClientContext context, Web hostWeb, List list, bool ExpandObjects, ITraceLogger logger, IEnumerable <FieldType> skiptypes, IEnumerable <Microsoft.SharePoint.Client.Group> siteGroups = null)
        {
            logger.LogInformation("Processing Web list {0}", list.Title);

            if (!hostWeb.IsPropertyAvailable(ctx => ctx.ServerRelativeUrl))
            {
                hostWeb.Context.Load(hostWeb, ctx => ctx.ServerRelativeUrl);
                hostWeb.Context.ExecuteQueryRetry();
            }

            list.EnsureProperties(
                lctx => lctx.Id,
                lctx => lctx.Title,
                lctx => lctx.Description,
                lctx => lctx.DefaultViewUrl,
                lctx => lctx.OnQuickLaunch,
                lctx => lctx.BaseTemplate,
                lctx => lctx.BaseType,
                lctx => lctx.CrawlNonDefaultViews,
                lctx => lctx.Created,
                lctx => lctx.ContentTypesEnabled,
                lctx => lctx.CreatablesInfo,
                lctx => lctx.EnableFolderCreation,
                lctx => lctx.EnableModeration,
                lctx => lctx.EnableVersioning,
                lctx => lctx.Hidden,
                lctx => lctx.IsApplicationList,
                lctx => lctx.IsCatalog,
                lctx => lctx.IsSiteAssetsLibrary,
                lctx => lctx.IsPrivate,
                lctx => lctx.IsSystemList,
                lctx => lctx.RootFolder.ServerRelativeUrl,
                lctx => lctx.SchemaXml,
                lctx => lctx.LastItemModifiedDate,
                lctx => lctx.LastItemUserModifiedDate,
                lctx => lctx.ListExperienceOptions,
                lctx => lctx.TemplateFeatureId);

            var weburl           = hostWeb.ServerRelativeUrl.EnsureTrailingSlashLowered();
            var listTemplateType = list.GetListTemplateType();

            var listdefinition = new SPListDefinition()
            {
                Id                       = list.Id,
                ListName                 = list.Title,
                ListDescription          = list.Description,
                ServerRelativeUrl        = list.DefaultViewUrl,
                BaseTemplate             = list.BaseTemplate,
                ListTemplate             = listTemplateType,
                Created                  = list.Created,
                LastItemModifiedDate     = list.LastItemModifiedDate,
                LastItemUserModifiedDate = list.LastItemUserModifiedDate,
                QuickLaunch              = list.OnQuickLaunch ? QuickLaunchOptions.On : QuickLaunchOptions.Off,
                ContentTypeEnabled       = list.ContentTypesEnabled,
                EnableFolderCreation     = list.EnableFolderCreation,
                Hidden                   = list.Hidden,
                IsApplicationList        = list.IsApplicationList,
                IsCatalog                = list.IsCatalog,
                IsSiteAssetsLibrary      = list.IsSiteAssetsLibrary,
                IsPrivate                = list.IsPrivate,
                IsSystemList             = list.IsSystemList
            };

            if (ExpandObjects)
            {
                var contentTypesFieldset = new List <dynamic>();
                var definitionListFields = new List <SPFieldDefinitionModel>();
                var listurl = TokenHelper.EnsureTrailingSlash(list.RootFolder.ServerRelativeUrl);

                // content types
                var listContentType = list.Context.LoadQuery(list.ContentTypes
                                                             .Include(
                                                                 ict => ict.Id,
                                                                 ict => ict.Group,
                                                                 ict => ict.Description,
                                                                 ict => ict.Name,
                                                                 ict => ict.Hidden,
                                                                 ict => ict.JSLink,
                                                                 ict => ict.FieldLinks,
                                                                 ict => ict.Fields));

                // list fields
                var listFields = list.Context.LoadQuery(list.Fields.Where(wf => wf.ReadOnlyField == false && wf.Hidden == false)
                                                        .Include(
                                                            fctx => fctx.Id,
                                                            fctx => fctx.AutoIndexed,
                                                            fctx => fctx.CanBeDeleted,
                                                            fctx => fctx.DefaultFormula,
                                                            fctx => fctx.DefaultValue,
                                                            fctx => fctx.Group,
                                                            fctx => fctx.Description,
                                                            fctx => fctx.EnforceUniqueValues,
                                                            fctx => fctx.FieldTypeKind,
                                                            fctx => fctx.Filterable,
                                                            fctx => fctx.FromBaseType,
                                                            fctx => fctx.Hidden,
                                                            fctx => fctx.Indexed,
                                                            fctx => fctx.InternalName,
                                                            fctx => fctx.JSLink,
                                                            fctx => fctx.NoCrawl,
                                                            fctx => fctx.ReadOnlyField,
                                                            fctx => fctx.Required,
                                                            fctx => fctx.SchemaXml,
                                                            fctx => fctx.Scope,
                                                            fctx => fctx.Title
                                                            ));

                // list views
                var listViews = list.Context.LoadQuery(list.Views
                                                       .Include(
                                                           lvt => lvt.Title,
                                                           lvt => lvt.DefaultView,
                                                           lvt => lvt.ServerRelativeUrl,
                                                           lvt => lvt.Id,
                                                           lvt => lvt.Aggregations,
                                                           lvt => lvt.AggregationsStatus,
                                                           lvt => lvt.BaseViewId,
                                                           lvt => lvt.Hidden,
                                                           lvt => lvt.ImageUrl,
                                                           lvt => lvt.JSLink,
                                                           lvt => lvt.HtmlSchemaXml,
                                                           lvt => lvt.ListViewXml,
                                                           lvt => lvt.MobileDefaultView,
                                                           lvt => lvt.ModerationType,
                                                           lvt => lvt.OrderedView,
                                                           lvt => lvt.Paged,
                                                           lvt => lvt.PageRenderType,
                                                           lvt => lvt.PersonalView,
                                                           lvt => lvt.ReadOnlyView,
                                                           lvt => lvt.Scope,
                                                           lvt => lvt.RowLimit,
                                                           lvt => lvt.StyleId,
                                                           lvt => lvt.TabularView,
                                                           lvt => lvt.Threaded,
                                                           lvt => lvt.Toolbar,
                                                           lvt => lvt.ToolbarTemplateName,
                                                           lvt => lvt.ViewFields,
                                                           lvt => lvt.ViewJoins,
                                                           lvt => lvt.ViewQuery,
                                                           lvt => lvt.ViewType,
                                                           lvt => lvt.ViewProjectedFields,
                                                           lvt => lvt.Method
                                                           ));

                list.Context.ExecuteQueryRetry();


                if (listContentType != null && listContentType.Any())
                {
                    listdefinition.ContentTypes = new List <SPContentTypeDefinition>();
                    foreach (var contenttype in listContentType)
                    {
                        logger.LogInformation("Processing list {0} content type {1}", list.Title, contenttype.Name);

                        var ctypemodel = new SPContentTypeDefinition()
                        {
                            Inherits         = true,
                            ContentTypeId    = contenttype.Id.StringValue,
                            ContentTypeGroup = contenttype.Group,
                            Description      = contenttype.Description,
                            Name             = contenttype.Name,
                            Hidden           = contenttype.Hidden,
                            JSLink           = contenttype.JSLink
                        };

                        if (contenttype.FieldLinks.Any())
                        {
                            ctypemodel.FieldLinks = new List <SPFieldLinkDefinitionModel>();
                            foreach (var cfieldlink in contenttype.FieldLinks)
                            {
                                ctypemodel.FieldLinks.Add(new SPFieldLinkDefinitionModel()
                                {
                                    Id       = cfieldlink.Id,
                                    Name     = cfieldlink.Name,
                                    Hidden   = cfieldlink.Hidden,
                                    Required = cfieldlink.Required
                                });

                                contentTypesFieldset.Add(new { ctypeid = contenttype.Id.StringValue, name = cfieldlink.Name });
                            }
                        }

                        if (contenttype.Fields.Any())
                        {
                            foreach (var cfield in contenttype.Fields.Where(cf => !ctypemodel.FieldLinks.Any(fl => fl.Name == cf.InternalName)))
                            {
                                ctypemodel.FieldLinks.Add(new SPFieldLinkDefinitionModel()
                                {
                                    Id       = cfield.Id,
                                    Name     = cfield.InternalName,
                                    Hidden   = cfield.Hidden,
                                    Required = cfield.Required
                                });
                            }
                        }

                        listdefinition.ContentTypes.Add(ctypemodel);
                    }
                }


                if (listFields != null && listFields.Any())
                {
                    var filteredListFields = listFields.Where(lf => !skiptypes.Any(st => lf.FieldTypeKind == st)).ToList();
                    logger.LogWarning("Processing list {0} found {1} fields to be processed", list.Title, filteredListFields.Count());

                    foreach (Field listField in listFields)
                    {
                        logger.LogInformation("Processing list {0} field {1}", list.Title, listField.InternalName);

                        try
                        {
                            var fieldXml = listField.SchemaXml;
                            if (!string.IsNullOrEmpty(fieldXml))
                            {
                                var xdoc      = XDocument.Parse(fieldXml, LoadOptions.PreserveWhitespace);
                                var xField    = xdoc.Element("Field");
                                var xSourceID = xField.Attribute("SourceID");
                                //if (xSourceID != null && xSourceID.Value.IndexOf(ConstantsXmlNamespaces.SharePointNS.NamespaceName, StringComparison.CurrentCultureIgnoreCase) < 0)
                                //{
                                //    continue; // skip processing an OOTB field
                                //}
                                var customField = context.RetrieveField(listField, logger, siteGroups, xField);
                                if (xSourceID != null)
                                {
                                    customField.SourceID = xSourceID.Value;
                                }
                                definitionListFields.Add(customField);

                                if (customField.FieldTypeKind == FieldType.Lookup)
                                {
                                    listdefinition.ListDependency.Add(customField.LookupListName);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.LogError(ex, "Failed to parse field {0} MSG:{1}", listField.InternalName, ex.Message);
                        }
                    }

                    listdefinition.FieldDefinitions = definitionListFields;
                }


                if (listViews != null && listViews.Any())
                {
                    listdefinition.InternalViews = new List <SPViewDefinitionModel>();
                    listdefinition.Views         = new List <SPViewDefinitionModel>();

                    foreach (var view in listViews)
                    {
                        logger.LogInformation("Processing list {0} view {1}", list.Title, view.Title);

                        var vinternal = (view.ServerRelativeUrl.IndexOf(listurl, StringComparison.CurrentCultureIgnoreCase) == -1);

                        ViewType viewCamlType = InfrastructureAsCode.Core.Extensions.ListExtensions.TryGetViewType(view.ViewType);

                        var viewmodel = new SPViewDefinitionModel()
                        {
                            Id                 = view.Id,
                            Title              = view.Title,
                            DefaultView        = view.DefaultView,
                            FieldRefName       = new List <string>(),
                            Aggregations       = view.Aggregations,
                            AggregationsStatus = view.AggregationsStatus,
                            BaseViewId         = view.BaseViewId,
                            Hidden             = view.Hidden,
                            ImageUrl           = view.ImageUrl,
                            Toolbar            = view.Toolbar,
                            ListViewXml        = view.ListViewXml,
                            MobileDefaultView  = view.MobileDefaultView,
                            ModerationType     = view.ModerationType,
                            OrderedView        = view.OrderedView,
                            Paged              = view.Paged,
                            PageRenderType     = view.PageRenderType,
                            PersonalView       = view.PersonalView,
                            ReadOnlyView       = view.ReadOnlyView,
                            Scope              = view.Scope,
                            RowLimit           = view.RowLimit,
                            StyleId            = view.StyleId,
                            TabularView        = view.TabularView,
                            Threaded           = view.Threaded,
                            ViewJoins          = view.ViewJoins,
                            ViewQuery          = view.ViewQuery,
                            ViewCamlType       = viewCamlType
                        };

                        if (vinternal)
                        {
                            viewmodel.SitePage     = view.ServerRelativeUrl.Replace(weburl, "");
                            viewmodel.InternalView = true;
                        }
                        else
                        {
                            viewmodel.InternalName = view.ServerRelativeUrl.Replace(listurl, "").Replace(".aspx", "");
                        }

                        if (view.ViewFields != null && view.ViewFields.Any())
                        {
                            foreach (var vfields in view.ViewFields)
                            {
                                viewmodel.FieldRefName.Add(vfields);
                            }
                        }

                        if (view.JSLink != null && view.JSLink.Any())
                        {
                            var vjslinks = view.JSLink.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                            if (vjslinks != null && !vjslinks.Any(jl => jl == "clienttemplates.js"))
                            {
                                viewmodel.JsLinkFiles = new List <string>();
                                foreach (var vjslink in vjslinks)
                                {
                                    viewmodel.JsLinkFiles.Add(vjslink);
                                }
                            }
                        }

                        if (view.Hidden)
                        {
                            listdefinition.InternalViews.Add(viewmodel);
                        }
                        else
                        {
                            listdefinition.Views.Add(viewmodel);
                        }
                    }
                }
            }

            return(listdefinition);
        }
コード例 #2
0
        private SPSiteModel ProcessSite(Web _web)
        {
            var hasListFound = false;
            var model        = new SPSiteModel();

            _web.EnsureProperties(wssp => wssp.Id,
                                  wspp => wspp.ServerRelativeUrl,
                                  wspp => wspp.Title,
                                  wssp => wssp.HasUniqueRoleAssignments,
                                  wssp => wssp.SiteUsers,
                                  wssp => wssp.Url,
                                  wssp => wssp.Lists,
                                  wssp => wssp.ContentTypes.Include(
                                      lcnt => lcnt.Id,
                                      lcnt => lcnt.Name,
                                      lcnt => lcnt.StringId,
                                      lcnt => lcnt.Description,
                                      lcnt => lcnt.DocumentTemplate,
                                      lcnt => lcnt.Group,
                                      lcnt => lcnt.Hidden,
                                      lcnt => lcnt.JSLink,
                                      lcnt => lcnt.SchemaXml,
                                      lcnt => lcnt.Scope,
                                      lcnt => lcnt.FieldLinks.Include(
                                          lcntlnk => lcntlnk.Id,
                                          lcntlnk => lcntlnk.Name,
                                          lcntlnk => lcntlnk.Hidden,
                                          lcntlnk => lcntlnk.Required
                                          ),
                                      lcnt => lcnt.Fields.Include(
                                          lcntfld => lcntfld.FieldTypeKind,
                                          lcntfld => lcntfld.InternalName,
                                          lcntfld => lcntfld.Id,
                                          lcntfld => lcntfld.Group,
                                          lcntfld => lcntfld.Title,
                                          lcntfld => lcntfld.Hidden,
                                          lcntfld => lcntfld.Description,
                                          lcntfld => lcntfld.JSLink,
                                          lcntfld => lcntfld.Indexed,
                                          lcntfld => lcntfld.Required,
                                          lcntfld => lcntfld.SchemaXml)),
                                  wssp => wssp.Fields.Include(
                                      lcntfld => lcntfld.FieldTypeKind,
                                      lcntfld => lcntfld.InternalName,
                                      lcntfld => lcntfld.Id,
                                      lcntfld => lcntfld.Group,
                                      lcntfld => lcntfld.Title,
                                      lcntfld => lcntfld.Hidden,
                                      lcntfld => lcntfld.Description,
                                      lcntfld => lcntfld.JSLink,
                                      lcntfld => lcntfld.Indexed,
                                      lcntfld => lcntfld.Required,
                                      lcntfld => lcntfld.SchemaXml));
            model.Url   = _web.Url;
            model.title = _web.Title;
            LogVerbose("Processing: {0}", _web.Url);


            /* Process Fields */
            try
            {
                foreach (var _fields in _web.Fields)
                {
                    if (string.IsNullOrEmpty(FieldColumnName) ||
                        _fields.Title.Equals(FieldColumnName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        hasListFound = true;

                        model.FieldDefinitions.Add(new SPFieldDefinitionModel()
                        {
                            FieldTypeKind = _fields.FieldTypeKind,
                            InternalName  = _fields.InternalName,
                            FieldGuid     = _fields.Id,
                            GroupName     = _fields.Group,
                            Title         = _fields.Title,
                            HiddenField   = _fields.Hidden,
                            Description   = _fields.Description,
                            JSLink        = _fields.JSLink,
                            FieldIndexed  = _fields.Indexed,
                            Required      = _fields.Required,
                            SchemaXml     = _fields.SchemaXml
                        });
                    }
                }
                ;
            }
            catch (Exception e)
            {
                LogError(e, "Failed to retrieve site owners {0}", _web.Url);
            }

            /* Process Content Type */
            try
            {
                foreach (var _ctypes in _web.ContentTypes)
                {
                    var cmodel = new SPContentTypeDefinition()
                    {
                        ContentTypeId    = _ctypes.StringId,
                        Name             = _ctypes.Name,
                        Description      = _ctypes.Description,
                        DocumentTemplate = _ctypes.DocumentTemplate,
                        ContentTypeGroup = _ctypes.Group,
                        Hidden           = _ctypes.Hidden,
                        JSLink           = _ctypes.JSLink,
                        Scope            = _ctypes.Scope
                    };

                    foreach (var _ctypeFields in _ctypes.FieldLinks)
                    {
                        if (string.IsNullOrEmpty(FieldColumnName) ||
                            _ctypeFields.Name.Equals(FieldColumnName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            cmodel.FieldLinks.Add(new SPFieldLinkDefinitionModel()
                            {
                                Name     = _ctypeFields.Name,
                                Id       = _ctypeFields.Id,
                                Hidden   = _ctypeFields.Hidden,
                                Required = _ctypeFields.Required
                            });
                        }
                    }

                    if (cmodel.FieldLinks.Any())
                    {
                        hasListFound = true;
                        model.ContentTypes.Add(cmodel);
                    }
                }
                ;
            }
            catch (Exception e)
            {
                LogError(e, "Failed to retrieve site owners {0}", _web.Url);
            }

            // ********** Process List
            try
            {
                var lists = ProcessList(_web);
                if (lists.Any())
                {
                    hasListFound = true;
                    model.Lists.AddRange(lists);
                }
            }
            catch (Exception e)
            {
                LogError(e, "Exception occurred in processSite");
            }

            if (hasListFound)
            {
                // setting ID to indicate to parent consumer that this entity has unique permissions in the TREE
                model.Id = _web.Id;
            }

            return(model);
        }
コード例 #3
0
        private IList <SPListDefinition> ProcessList(Web _web)
        {
            var model = new List <SPListDefinition>();

            // ********** Process Lists
            ListCollection _lists = _web.Lists;

            _web.Context.Load(_lists,
                              spp => spp.Include(
                                  sppi => sppi.Id,
                                  sppi => sppi.Title,
                                  sppi => sppi.RootFolder.ServerRelativeUrl,
                                  sppi => sppi.HasUniqueRoleAssignments,
                                  sppi => sppi.BaseTemplate,
                                  sppi => sppi.Hidden,
                                  sppi => sppi.IsSystemList,
                                  sppi => sppi.IsPrivate,
                                  sppi => sppi.IsApplicationList,
                                  sppi => sppi.IsCatalog,
                                  sppi => sppi.IsSiteAssetsLibrary));

            _web.Context.ExecuteQueryRetry();

            var docLibEnumValue = Convert.ToInt32(Microsoft.SharePoint.Client.ListTemplateType.DocumentLibrary);

            // Restrict to natural lists or custom lists
            foreach (List _list in _lists.Where(sppi
                                                => !sppi.IsSystemList &&
                                                !sppi.IsApplicationList &&
                                                sppi.BaseTemplate != docLibEnumValue))
            {
                var hasListFound = false;
                var listContext  = _list.Context;
                LogVerbose("Enumerating List {0} URL:{1}", _list.Title, _list.RootFolder.ServerRelativeUrl);

                try
                {
                    listContext.Load(_list,
                                     lssp => lssp.Id,
                                     lssp => lssp.Title,
                                     lssp => lssp.HasUniqueRoleAssignments,
                                     lssp => lssp.Title,
                                     lssp => lssp.Hidden,
                                     lssp => lssp.IsSystemList,
                                     lssp => lssp.IsPrivate,
                                     lssp => lssp.IsApplicationList,
                                     lssp => lssp.IsCatalog,
                                     lssp => lssp.IsSiteAssetsLibrary,
                                     lssp => lssp.RootFolder.ServerRelativeUrl,
                                     lssp => lssp.ContentTypes.Include(
                                         lcnt => lcnt.Id,
                                         lcnt => lcnt.Name,
                                         lcnt => lcnt.StringId,
                                         lcnt => lcnt.Description,
                                         lcnt => lcnt.DocumentTemplate,
                                         lcnt => lcnt.Group,
                                         lcnt => lcnt.Hidden,
                                         lcnt => lcnt.JSLink,
                                         lcnt => lcnt.SchemaXml,
                                         lcnt => lcnt.Scope,
                                         lcnt => lcnt.FieldLinks.Include(
                                             lcntlnk => lcntlnk.Id,
                                             lcntlnk => lcntlnk.Name,
                                             lcntlnk => lcntlnk.Hidden,
                                             lcntlnk => lcntlnk.Required
                                             ),
                                         lcnt => lcnt.Fields.Include(
                                             lcntfld => lcntfld.FieldTypeKind,
                                             lcntfld => lcntfld.InternalName,
                                             lcntfld => lcntfld.Id,
                                             lcntfld => lcntfld.Group,
                                             lcntfld => lcntfld.Title,
                                             lcntfld => lcntfld.Hidden,
                                             lcntfld => lcntfld.Description,
                                             lcntfld => lcntfld.JSLink,
                                             lcntfld => lcntfld.Indexed,
                                             lcntfld => lcntfld.Required,
                                             lcntfld => lcntfld.SchemaXml)),
                                     lssp => lssp.Fields.Include(
                                         lcntfld => lcntfld.FieldTypeKind,
                                         lcntfld => lcntfld.InternalName,
                                         lcntfld => lcntfld.Id,
                                         lcntfld => lcntfld.Group,
                                         lcntfld => lcntfld.Title,
                                         lcntfld => lcntfld.Hidden,
                                         lcntfld => lcntfld.Description,
                                         lcntfld => lcntfld.JSLink,
                                         lcntfld => lcntfld.Indexed,
                                         lcntfld => lcntfld.Required,
                                         lcntfld => lcntfld.SchemaXml));
                    listContext.ExecuteQueryRetry();

                    var listModel = new SPListDefinition()
                    {
                        Id = _list.Id,
                        HasUniquePermission = _list.HasUniqueRoleAssignments,
                        ListName            = _list.Title,
                        ServerRelativeUrl   = _list.RootFolder.ServerRelativeUrl,
                        Hidden              = _list.Hidden,
                        IsSystemList        = _list.IsSystemList,
                        IsPrivate           = _list.IsPrivate,
                        IsApplicationList   = _list.IsApplicationList,
                        IsCatalog           = _list.IsCatalog,
                        IsSiteAssetsLibrary = _list.IsSiteAssetsLibrary
                    };

                    /* Process Fields */
                    try
                    {
                        foreach (var _fields in _list.Fields)
                        {
                            if (string.IsNullOrEmpty(FieldColumnName) ||
                                _fields.Title.Equals(FieldColumnName, StringComparison.InvariantCultureIgnoreCase))
                            {
                                hasListFound = true;

                                listModel.FieldDefinitions.Add(new SPFieldDefinitionModel()
                                {
                                    FieldTypeKind = _fields.FieldTypeKind,
                                    InternalName  = _fields.InternalName,
                                    FieldGuid     = _fields.Id,
                                    GroupName     = _fields.Group,
                                    Title         = _fields.Title,
                                    HiddenField   = _fields.Hidden,
                                    Description   = _fields.Description,
                                    JSLink        = _fields.JSLink,
                                    FieldIndexed  = _fields.Indexed,
                                    Required      = _fields.Required,
                                    SchemaXml     = _fields.SchemaXml
                                });
                            }
                        }
                        ;
                    }
                    catch (Exception e)
                    {
                        LogError(e, "Failed to retrieve site owners {0}", _web.Url);
                    }

                    foreach (var _ctypes in _list.ContentTypes)
                    {
                        var cmodel = new SPContentTypeDefinition()
                        {
                            ContentTypeId    = _ctypes.StringId,
                            Name             = _ctypes.Name,
                            Description      = _ctypes.Description,
                            DocumentTemplate = _ctypes.DocumentTemplate,
                            ContentTypeGroup = _ctypes.Group,
                            Hidden           = _ctypes.Hidden,
                            JSLink           = _ctypes.JSLink,
                            Scope            = _ctypes.Scope
                        };

                        foreach (var _ctypeFields in _ctypes.FieldLinks)
                        {
                            if (string.IsNullOrEmpty(FieldColumnName) ||
                                _ctypeFields.Name.Equals(FieldColumnName, StringComparison.InvariantCultureIgnoreCase))
                            {
                                cmodel.FieldLinks.Add(new SPFieldLinkDefinitionModel()
                                {
                                    Name     = _ctypeFields.Name,
                                    Id       = _ctypeFields.Id,
                                    Hidden   = _ctypeFields.Hidden,
                                    Required = _ctypeFields.Required
                                });
                            }
                        }

                        if (cmodel.FieldLinks.Any())
                        {
                            hasListFound = true;
                            listModel.ContentTypes.Add(cmodel);
                        }
                    }

                    if (hasListFound)
                    {
                        model.Add(listModel);
                    }
                }
                catch (Exception e)
                {
                    LogError(e, "Failed in ProcessList");
                }
            }
            return(model);
        }
        /// <summary>
        /// Process the request
        /// </summary>
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            // File Info
            var fileInfo = new System.IO.FileInfo(this.ProvisionerFilePath);

            // Initialize logging instance with Powershell logger
            ITraceLogger logger = new DefaultUsageLogger(LogVerbose, LogWarning, LogError);

            // Skip these specific fields
            var skiptypes = new FieldType[]
            {
                FieldType.Computed,
                FieldType.ContentTypeId,
                FieldType.Invalid,
                FieldType.WorkflowStatus,
                FieldType.WorkflowEventType,
                FieldType.Threading,
                FieldType.ThreadIndex,
                FieldType.Recurrence,
                FieldType.PageSeparator,
                FieldType.OutcomeChoice,
                FieldType.CrossProjectLink,
                FieldType.ModStat,
                FieldType.Error,
                FieldType.MaxItems,
                FieldType.Attachments
            };

            var skipcolumns = new string[]
            {
                "_Hidden",
                "Base Columns",
                "Content Feedback",
                "Core Contact and Calendar Columns",
                "Core Document Columns",
                "Core Task and Issue Columns",
                "Display Template Columns",
                "Document and Record Management Columns",
                "Enterprise Keywords Group",
                "Extended Columns",
                "JavaScript Display Template Columns",
                "Page Layout Columns",
                "Publishing Columns",
                "Reports",
                "Status Indicators",
                "Translation Columns"
            };

            var skipcontenttypes = new string[]
            {
                "_Hidden",
                "Business Intelligence",
                "Community Content Types",
                "Digital Asset Content Types",
                "Display Template Content Types",
                "Document Content Types",
                "Document Set Content Types",
                "Folder Content Types",
                "Content Feedback",
                "Publishing Content Types",
                "Page Layout Content Types",
                "Special Content Types",
                "Group Work Content Types",
                "List Content Types"
            };

            // Construct the model
            var SiteComponents = new SiteProvisionerModel();

            // Load the Context
            var contextWeb = this.ClientContext.Web;

            this.ClientContext.Load(contextWeb, ctxw => ctxw.ServerRelativeUrl, ctxw => ctxw.Id);

            // Get Site/Web fields
            var fields = this.ClientContext.LoadQuery(contextWeb.Fields
                                                      .Include(
                                                          fctx => fctx.Id,
                                                          fctx => fctx.FieldTypeKind,
                                                          fctx => fctx.Group,
                                                          lft => lft.AutoIndexed,
                                                          lft => lft.CanBeDeleted,
                                                          lft => lft.DefaultFormula,
                                                          lft => lft.DefaultValue,
                                                          lft => lft.Description,
                                                          lft => lft.EnforceUniqueValues,
                                                          lft => lft.Filterable,
                                                          lft => lft.FromBaseType,
                                                          lft => lft.Hidden,
                                                          lft => lft.Indexed,
                                                          lft => lft.InternalName,
                                                          lft => lft.JSLink,
                                                          lft => lft.NoCrawl,
                                                          lft => lft.ReadOnlyField,
                                                          lft => lft.Required,
                                                          lft => lft.SchemaXml,
                                                          lft => lft.Scope,
                                                          lft => lft.Title));

            var groupQuery = this.ClientContext.LoadQuery(contextWeb.SiteGroups
                                                          .Include(group => group.Id,
                                                                   group => group.Title,
                                                                   group => group.Description,
                                                                   group => group.AllowRequestToJoinLeave,
                                                                   group => group.AllowMembersEditMembership,
                                                                   group => group.AutoAcceptRequestToJoinLeave,
                                                                   group => group.OnlyAllowMembersViewMembership,
                                                                   group => group.RequestToJoinLeaveEmailSetting));

            var contentTypes = this.ClientContext.LoadQuery(contextWeb.ContentTypes
                                                            .Include(
                                                                ict => ict.Id,
                                                                ict => ict.Group,
                                                                ict => ict.Description,
                                                                ict => ict.Name,
                                                                ict => ict.Hidden,
                                                                ict => ict.JSLink,
                                                                ict => ict.FieldLinks,
                                                                ict => ict.Fields));


            var lists = this.ClientContext.LoadQuery(contextWeb.Lists
                                                     .Include(
                                                         linc => linc.Title,
                                                         linc => linc.Id,
                                                         linc => linc.Description,
                                                         linc => linc.RootFolder.ServerRelativeUrl,
                                                         linc => linc.Hidden,
                                                         linc => linc.OnQuickLaunch,
                                                         linc => linc.BaseTemplate,
                                                         linc => linc.ContentTypesEnabled,
                                                         linc => linc.AllowContentTypes,
                                                         linc => linc.EnableFolderCreation,
                                                         linc => linc.IsApplicationList,
                                                         linc => linc.IsCatalog,
                                                         linc => linc.IsSiteAssetsLibrary,
                                                         linc => linc.IsPrivate,
                                                         linc => linc.IsSystemList,
                                                         lctx => lctx.SchemaXml).Where(w => !w.IsSystemList && !w.IsSiteAssetsLibrary));

            this.ClientContext.ExecuteQueryRetry();

            var weburl = TokenHelper.EnsureTrailingSlash(contextWeb.ServerRelativeUrl);


            if (groupQuery.Any())
            {
                SiteComponents.Groups = new List <SPGroupDefinitionModel>();

                foreach (var group in groupQuery)
                {
                    var model = new SPGroupDefinitionModel()
                    {
                        Id                             = group.Id,
                        Title                          = group.Title,
                        Description                    = group.Description,
                        AllowRequestToJoinLeave        = group.AllowRequestToJoinLeave,
                        AllowMembersEditMembership     = group.AllowMembersEditMembership,
                        AutoAcceptRequestToJoinLeave   = group.AutoAcceptRequestToJoinLeave,
                        OnlyAllowMembersViewMembership = group.OnlyAllowMembersViewMembership,
                        RequestToJoinLeaveEmailSetting = group.RequestToJoinLeaveEmailSetting
                    };

                    SiteComponents.Groups.Add(model);
                }
            }


            if (fields.Any())
            {
                var webfields = new List <SPFieldDefinitionModel>();
                foreach (var field in fields)
                {
                    if (skiptypes.Any(st => field.FieldTypeKind == st) ||
                        skipcolumns.Any(sg => field.Group.Equals(sg, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        continue;
                    }

                    try
                    {
                        var fieldModel = ClientContext.RetrieveField(field, logger, groupQuery);
                        webfields.Add(fieldModel);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Trace.TraceError("Failed to parse field {0} MSG:{1}", field.InternalName, ex.Message);
                    }
                }

                SiteComponents.FieldDefinitions = webfields;
            }


            var contentTypesFieldset = new List <dynamic>();

            if (contentTypes.Any())
            {
                SiteComponents.ContentTypes = new List <SPContentTypeDefinition>();
                foreach (ContentType contenttype in contentTypes)
                {
                    // skip core content types
                    if (skipcontenttypes.Any(sg => contenttype.Group.Equals(sg, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        continue;
                    }

                    contenttype.EnsureProperties(ctp => ctp.Id, ctp => ctp.Group, ctp => ctp.Hidden, ctp => ctp.Description, ctp => ctp.Name, ctp => ctp.FieldLinks);

                    var ctypemodel = new SPContentTypeDefinition()
                    {
                        ContentTypeId    = contenttype.Id.StringValue,
                        ContentTypeGroup = contenttype.Group,
                        Hidden           = contenttype.Hidden,
                        Description      = contenttype.Description,
                        Name             = contenttype.Name
                    };

                    if (contenttype.FieldLinks.Any())
                    {
                        ctypemodel.FieldLinks = new List <SPFieldLinkDefinitionModel>();
                        foreach (FieldLink fieldlink in contenttype.FieldLinks)
                        {
                            ctypemodel.FieldLinks.Add(new SPFieldLinkDefinitionModel()
                            {
                                Id       = fieldlink.Id,
                                Name     = fieldlink.Name,
                                Required = fieldlink.Required,
                                Hidden   = fieldlink.Hidden
                            });

                            contentTypesFieldset.Add(new { ctypeid = contenttype.Id.StringValue, name = fieldlink.Name });
                        }
                    }

                    SiteComponents.ContentTypes.Add(ctypemodel);
                }
            }


            if (lists.Any())
            {
                var sitelists = new List <SPListDefinition>();

                foreach (List list in lists.Where(lwt =>
                                                  (!_filterLists ||
                                                   (_filterLists && SpecificLists.Any(sl => lwt.Title.Equals(sl, StringComparison.InvariantCultureIgnoreCase))))))
                {
                    var listdefinition = ClientContext.GetListDefinition(contextWeb, list, true, logger, skiptypes, groupQuery);
                    sitelists.Add(listdefinition);
                }

                if (sitelists.Any())
                {
                    var idx = 0;
                    SiteComponents.Lists = new List <SPListDefinition>();

                    // lets add any list with NO lookups first
                    var nolookups = sitelists.Where(sl => !sl.ListDependency.Any());
                    nolookups.ToList().ForEach(nolookup =>
                    {
                        logger.LogInformation("adding list {0}", nolookup.ListName);
                        nolookup.ProvisionOrder = idx++;
                        SiteComponents.Lists.Add(nolookup);
                        sitelists.Remove(nolookup);
                    });

                    // order with first in stack
                    var haslookups = sitelists.Where(sl => sl.ListDependency.Any()).OrderBy(ob => ob.ListDependency.Count()).ToList();
                    while (haslookups.Count() > 0)
                    {
                        var listPopped = haslookups.FirstOrDefault();
                        haslookups.Remove(listPopped);
                        logger.LogInformation("adding list {0}", listPopped.ListName);

                        if (listPopped.ListDependency.Any(listField => listPopped.ListName.Equals(listField, StringComparison.InvariantCultureIgnoreCase) ||
                                                          listPopped.InternalName.Equals(listField, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            // the list has a dependency on itself
                            logger.LogInformation("Adding list {0} with dependency on itself", listPopped.ListName);
                            listPopped.ProvisionOrder = idx++;
                            SiteComponents.Lists.Add(listPopped);
                        }
                        else if (listPopped.ListDependency.Any(listField =>
                                                               !SiteComponents.Lists.Any(sc => sc.ListName.Equals(listField, StringComparison.InvariantCultureIgnoreCase) ||
                                                                                         sc.InternalName.Equals(listField, StringComparison.InvariantCultureIgnoreCase))))
                        {
                            // no list definition exists in the collection with the dependent lookup lists
                            logger.LogWarning("List {0} depends on {1} which do not exist current collection", listPopped.ListName, string.Join(",", listPopped.ListDependency));
                            foreach (var dependent in listPopped.ListDependency.Where(dlist => !haslookups.Any(hl => hl.ListName == dlist)))
                            {
                                var sitelist       = contextWeb.GetListByTitle(dependent, lctx => lctx.Id, lctx => lctx.Title, lctx => lctx.RootFolder.ServerRelativeUrl);
                                var listDefinition = ClientContext.GetListDefinition(contextWeb, sitelist, true, logger, skiptypes, groupQuery);
                                haslookups.Add(listDefinition);
                            }

                            haslookups.Add(listPopped); // add back to collection
                            listPopped = null;
                        }
                        else
                        {
                            logger.LogInformation("Adding list {0} to collection", listPopped.ListName);
                            listPopped.ProvisionOrder = idx++;
                            SiteComponents.Lists.Add(listPopped);
                        }
                    }
                }
            }

            // Write the JSON to disc
            var jsonsettings = new JsonSerializerSettings()
            {
                Formatting         = Formatting.Indented,
                Culture            = System.Globalization.CultureInfo.CurrentUICulture,
                DateFormatHandling = DateFormatHandling.IsoDateFormat,
                NullValueHandling  = NullValueHandling.Ignore
            };

            var json = JsonConvert.SerializeObject(SiteComponents, jsonsettings);

            System.IO.File.WriteAllText(fileInfo.FullName, json);
        }