/// <summary>
        /// Returns the parent item ID
        /// </summary>
        /// <param name="cContext"></param>
        /// <param name="ItemName"></param>
        /// <param name="logger">diagnostics logger</param>
        /// <returns></returns>
        static int GetUserID(ClientContext cContext, dynamic ItemName, ITraceLogger logger)
        {
            int nReturn = -1;
            NativeFieldUserValue userValue = null;

            try
            {
                string itemJsonString = ItemName.ToString();
                Newtonsoft.Json.Linq.JObject jobject = Newtonsoft.Json.Linq.JObject.Parse(itemJsonString);
                userValue = jobject.ToObject <NativeFieldUserValue>();


                logger.LogInformation("Start GetUserID {0}", userValue.Email);
                Web wWeb  = cContext.Web;
                var iUser = cContext.Web.EnsureUser(userValue.Email);
                cContext.Load(iUser);
                cContext.ExecuteQueryRetry();
                if (iUser != null)
                {
                    return(iUser.Id);
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Failed to find {0} in web {1}", ItemName, ex.Message);
            }

            return(nReturn);
        }
        /// <summary>
        /// Will request the token, if the cache has expired, will throw an exception and request a new auth cache token and attempt to return it
        /// </summary>
        /// <param name="redirectUri">(OPTIONAL) a redirect to the resource URI</param>
        /// <returns>Return an Authentication Result which contains the Token/Refresh Token</returns>
        async public Task <AuthenticationResult> TryGetAccessTokenResultAsync(string redirectUri)
        {
            AuthenticationResult token = null; var cleanToken = false;

            try
            {
                token = await AccessTokenResultAsync();

                cleanToken = true;
            }
            catch (Exception ex)
            {
                _iLogger.LogError(ex, "AdalCacheException: {0}", ex.Message);
            }

            if (!cleanToken)
            {
                // Failed to retrieve, reup the token
                redirectUri = (string.IsNullOrEmpty(redirectUri) ? GetRedirectUri() : redirectUri);
                await RedeemAuthCodeForAadGraphAsync(string.Empty, redirectUri);

                token = await AccessTokenResultAsync();
            }

            return(token);
        }
Beispiel #3
0
        /// <summary>
        /// Provisions a site column based on the field definition specified
        /// </summary>
        /// <param name="hostWeb">The instantiated site/web to which the column will be retrieved and/or provisioned</param>
        /// <param name="fieldDefinition">The definition for the field</param>
        /// <param name="loggerVerbose">Provides a method for verbose logging</param>
        /// <param name="loggerError">Provides a method for exception logging</param>
        /// <param name="SiteGroups">(OPTIONAL) collection of group, required if this is a PeoplePicker field</param>
        /// <param name="provisionerChoices">(OPTIONAL) deserialized choices from JSON</param>
        /// <returns></returns>
        public static Field CreateColumn(this Web hostWeb, SPFieldDefinitionModel fieldDefinition, ITraceLogger logger, List <SPGroupDefinitionModel> SiteGroups, List <SiteProvisionerFieldChoiceModel> provisionerChoices = null)
        {
            if (fieldDefinition == null)
            {
                throw new ArgumentNullException("fieldDef", "Field definition is required.");
            }

            if (string.IsNullOrEmpty(fieldDefinition.InternalName))
            {
                throw new ArgumentNullException("InternalName");
            }

            if (fieldDefinition.LoadFromJSON && (provisionerChoices == null || !provisionerChoices.Any(pc => pc.FieldInternalName == fieldDefinition.InternalName)))
            {
                throw new ArgumentNullException("provisionerChoices", string.Format("You must specify a collection of field choices for the field {0}", fieldDefinition.Title));
            }

            var fields = hostWeb.Context.LoadQuery(hostWeb.Fields.Include(f => f.Id, f => f.InternalName, f => f.Title, f => f.JSLink, f => f.Indexed, f => f.CanBeDeleted, f => f.Required));

            hostWeb.Context.ExecuteQueryRetry();


            var returnField = fields.FirstOrDefault(f => f.Id == fieldDefinition.FieldGuid || f.InternalName == fieldDefinition.InternalName || f.Title == fieldDefinition.InternalName);

            if (returnField == null)
            {
                var finfoXml = hostWeb.CreateFieldDefinition(fieldDefinition, SiteGroups, provisionerChoices);
                logger.LogInformation("Provision Site field {0} with XML:{1}", fieldDefinition.InternalName, finfoXml);
                try
                {
                    var createdField = hostWeb.CreateField(finfoXml, executeQuery: false);
                    hostWeb.Context.ExecuteQueryRetry();
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "EXCEPTION: field {0} with message {1}", fieldDefinition.InternalName, ex.Message);
                }
                finally
                {
                    returnField = hostWeb.Fields.GetByInternalNameOrTitle(fieldDefinition.InternalName);
                    hostWeb.Context.Load(returnField, fd => fd.Id, fd => fd.Title, fd => fd.Indexed, fd => fd.InternalName, fd => fd.CanBeDeleted, fd => fd.Required);
                    hostWeb.Context.ExecuteQuery();
                }
            }

            return(returnField);
        }
        /// <summary>
        /// Provisions a column based on the field defintion to the host list
        /// </summary>
        /// <param name="hostList">The instantiated list/library to which the field will be added</param>
        /// <param name="fieldDefinition">The definition for the field</param>
        /// <param name="loggerVerbose">Provides a method for verbose logging</param>
        /// <param name="loggerError">Provides a method for exception logging</param>
        /// <param name="SiteGroups">(OPTIONAL) collection of group, required if this is a PeoplePicker field</param>
        /// <param name="provisionerChoices">(OPTIONAL) deserialized choices from JSON</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">For field definitions that do not contain all required data</exception>
        public static Field CreateListColumn(this List hostList, SPFieldDefinitionModel fieldDefinition, ITraceLogger logger, List <SPGroupDefinitionModel> SiteGroups, List <SiteProvisionerFieldChoiceModel> provisionerChoices = null)
        {
            if (fieldDefinition == null)
            {
                throw new ArgumentNullException("fieldDef", "Field definition is required.");
            }

            if (fieldDefinition.LoadFromJSON && (provisionerChoices == null || !provisionerChoices.Any(pc => pc.FieldInternalName == fieldDefinition.InternalName)))
            {
                throw new ArgumentNullException("provisionerChoices", string.Format("You must specify a collection of field choices for the field {0}", fieldDefinition.Title));
            }

            // load fields into memory
            hostList.Context.Load(hostList.Fields, fc => fc.Include(f => f.Id, f => f.InternalName, fctx => fctx.Title, f => f.Title, f => f.JSLink, f => f.Indexed, f => f.CanBeDeleted, f => f.Required));
            hostList.Context.ExecuteQueryRetry();

            var returnField = hostList.Fields.FirstOrDefault(f => f.Id == fieldDefinition.FieldGuid || f.InternalName == fieldDefinition.InternalName);

            if (returnField == null)
            {
                try
                {
                    var baseFieldXml = hostList.CreateFieldDefinition(fieldDefinition, SiteGroups, provisionerChoices);
                    logger.LogInformation("Provision List {0} field {1} with XML:{2}", hostList.Title, fieldDefinition.InternalName, baseFieldXml);

                    // Should throw an exception if the field ID or Name exist in the list
                    var baseField = hostList.CreateField(baseFieldXml, fieldDefinition.AddToDefaultView, executeQuery: false);
                    hostList.Context.ExecuteQueryRetry();
                }
                catch (Exception ex)
                {
                    var msg = ex.Message;
                    logger.LogError(ex, "EXCEPTION: field {0} with message {1}", fieldDefinition.InternalName, msg);
                }
                finally
                {
                    returnField = hostList.Fields.GetByInternalNameOrTitle(fieldDefinition.InternalName);
                    hostList.Context.Load(returnField, fd => fd.Id, fd => fd.Title, fd => fd.Indexed, fd => fd.InternalName, fd => fd.CanBeDeleted, fd => fd.Required);
                    hostList.Context.ExecuteQueryRetry();
                }
            }

            return(returnField);
        }
        /// <summary>
        /// Initiates a blocker and waites for a Async thread to complete
        /// </summary>
        /// <returns></returns>
        internal string GetTokenAsyncResult()
        {
            string bearerToken = null;

            try
            {
                bearerToken = AzureADCache.AccessTokenAsync(string.Empty).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                _iLogger.LogError(ex, Properties.Resources.TokenAsyncResultFailure, ex.Message);
            }

            if (string.IsNullOrEmpty(bearerToken))
            {
                throw new ArgumentException($"AzureAD Cache has no Bearer Token");
            }

            return(bearerToken);
        }
        /// <summary>
        /// Initiates a blocker and waites for a Async thread to complete
        /// </summary>
        /// <returns></returns>
        internal AuthenticationResult GetTokenAsyncResult()
        {
            AuthenticationResult authenticationResult = null;

            try
            {
                var asyncFunction = AzureADCache.TryGetAccessTokenResultAsync(string.Empty);

                asyncFunction.Wait();

                authenticationResult = asyncFunction.Result;
            }
            catch (Exception ex)
            {
                _iLogger.LogError(ex, $"Claiming Azure AD Token Failed {ex.Message}");
            }

            if (authenticationResult == null || string.IsNullOrEmpty(authenticationResult.AccessToken))
            {
                throw new ArgumentNullException("authenticationResult");
            }

            return(authenticationResult);
        }
Beispiel #7
0
        /// <summary>
        /// Returns Taxonomy store and set details
        /// </summary>
        /// <param name="clientContext"></param>
        /// <param name="logger"></param>
        /// <param name="termSetId"></param>
        /// <returns>NULL if an Exception is thrown</returns>
        public static SPOTaxonomyModel GetTaxonomyFieldInfo(this ClientContext clientContext, ITraceLogger logger, Guid termSetId)
        {
            TaxonomySession session   = TaxonomySession.GetTaxonomySession(clientContext);
            TermStore       termStore = session.GetDefaultSiteCollectionTermStore();
            TermSet         termSet   = termStore.GetTermSet(termSetId);

            SPOTaxonomyTermStoreModel modelTermStore = null;
            SPOTaxonomyTermSetModel   modelTermSet   = null;

            try
            {
                clientContext.Load(termSet,
                                   tctx => tctx.Id,
                                   tctx => tctx.CustomSortOrder,
                                   tctx => tctx.IsAvailableForTagging,
                                   tctx => tctx.Owner,
                                   tctx => tctx.CreatedDate,
                                   tctx => tctx.LastModifiedDate,
                                   tctx => tctx.Name,
                                   tctx => tctx.Description,
                                   tctx => tctx.TermStore,
                                   tctx => tctx.Group,
                                   tctx => tctx.IsOpenForTermCreation);

                clientContext.Load(termStore,
                                   tctx => tctx.Id,
                                   tctx => tctx.Name,
                                   tctx => tctx.IsOnline,
                                   tctx => tctx.DefaultLanguage,
                                   tctx => tctx.ContentTypePublishingHub,
                                   tctx => tctx.WorkingLanguage);
                clientContext.ExecuteQueryRetry();

                modelTermStore = new SPOTaxonomyTermStoreModel()
                {
                    Id                       = termStore.Id,
                    Name                     = termStore.Name,
                    IsOnline                 = termStore.IsOnline,
                    DefaultLanguage          = termStore.DefaultLanguage,
                    ContentTypePublishingHub = termStore.ContentTypePublishingHub,
                    WorkingLanguage          = termStore.WorkingLanguage
                };

                modelTermSet = new SPOTaxonomyTermSetModel()
                {
                    Id = termSet.Id,
                    IsAvailableForTagging = termSet.IsAvailableForTagging,
                    IsOpenForTermCreation = termSet.IsOpenForTermCreation,
                    CustomSortOrder       = termSet.CustomSortOrder,
                    Owner            = termSet.Owner,
                    CreatedDate      = termSet.CreatedDate,
                    LastModifiedDate = termSet.LastModifiedDate,
                    Name             = termSet.Name,
                    Description      = termSet.Description
                };

                if (termSet.TermStore != null &&
                    termSet.TermStore.Id != null)
                {
                    var tempStore = termSet.TermStore;
                    modelTermSet.TermStoreId = tempStore.Id;
                }

                if (termSet.Group != null)
                {
                    var termGroup = termSet.Group;
                    modelTermSet.Group = new SPOTaxonomyItemModel()
                    {
                        Id               = termGroup.Id,
                        Name             = termGroup.Name,
                        CreatedDate      = termGroup.CreatedDate,
                        LastModifiedDate = termGroup.LastModifiedDate
                    };
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Failed to retreive TermStore session {0} with message {1}", termSetId, ex.Message);
                return(null);
            }

            // Build model
            var termsetModel = new SPOTaxonomyModel()
            {
                TermSetName = termSet.Name,
                TermSet     = modelTermSet,
                TermStore   = modelTermStore
            };

            return(termsetModel);
        }
        /// <summary>
        /// Returns the parent item ID
        /// </summary>
        /// <param name="cContext"></param>
        /// <param name="ItemName"></param>
        /// <param name="ParentListColumn"></param>
        /// <param name="logger">diagnostics logger</param>
        /// <returns></returns>
        static int GetParentItemID(ClientContext cContext, dynamic ItemName, SPFieldDefinitionModel ParentListColumn, ITraceLogger logger)
        {
            int nReturn                        = -1;
            var parentListName                 = string.Empty;
            var parentListColumnName           = string.Empty;
            NativeFieldLookupValue lookupValue = null;

            try
            {
                string itemJsonString = ItemName.ToString();
                Newtonsoft.Json.Linq.JObject jobject = Newtonsoft.Json.Linq.JObject.Parse(itemJsonString);
                lookupValue = jobject.ToObject <NativeFieldLookupValue>();



                parentListName       = ParentListColumn.LookupListName;
                parentListColumnName = ParentListColumn.LookupListFieldName;
                logger.LogInformation("Start GetParentItemID {0} for column {1}", parentListName, parentListColumnName);

                Web wWeb = cContext.Web;

                var lParentList = cContext.Web.GetListByTitle(parentListName, lctx => lctx.Id, lctx => lctx.Title);
                var camlQuery   = new CamlQuery()
                {
                    ViewXml = CAML.ViewQuery(
                        CAML.Where(
                            CAML.Eq(
                                CAML.FieldValue(parentListColumnName, FieldType.Text.ToString("f"), lookupValue.LookupValue))
                            ),
                        string.Empty,
                        10
                        )
                };

                ListItemCollectionPosition itemPosition = null;
                while (true)
                {
                    var collListItem = lParentList.GetItems(camlQuery);
                    cContext.Load(collListItem, lictx => lictx.ListItemCollectionPosition);
                    cContext.ExecuteQueryRetry();
                    itemPosition = collListItem.ListItemCollectionPosition;

                    foreach (var oListItem in collListItem)
                    {
                        nReturn = oListItem.Id;
                        break;
                    }

                    // we drop out of the forloop above but if we are paging do we want to skip duplicate results
                    if (itemPosition == null)
                    {
                        break;
                    }
                }

                logger.LogInformation("Complete GetParentItemID {0} resulted in ID => {1}", parentListName, nReturn);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Failed to query lookup value {0}", ex.Message);
            }

            return(nReturn);
        }
Beispiel #9
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);
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            var objects = new List <SPWorkflowInstance>();


            var FieldDefinitions = new List <SPFieldDefinitionModel>
            {
                new SPFieldDefinitionModel(FieldType.Boolean)
                {
                    FieldGuid    = new Guid("da2872c4-e9b6-4804-9837-6e9dd85ecd7e"),
                    InternalName = FieldBoolean_RestartWorkflow,
                    Description  = "RestartWorkflow provides a way to identify items that should be restarted.",
                    Title        = FieldBoolean_RestartWorkflow,
                    MaxLength    = 255,
                    DefaultValue = "No"
                }
            };


            var SelectedWeb = this.ClientContext.Web;

            Ilogger = new DefaultUsageLogger(
                (string msg, object[] margs) =>
            {
                LogVerbose(msg, margs);
            },
                (string msg, object[] margs) =>
            {
                LogWarning(msg, margs);
            },
                (Exception ex, string msg, object[] margs) =>
            {
                LogError(ex, msg, margs);
            });

            var list = List.GetList(SelectedWeb, lctx => lctx.Id, lctx => lctx.Title);


            var workflowSubscriptionId = new List <Guid>();

            if (!string.IsNullOrEmpty(WorkflowName))
            {
                var servicesManager     = new WorkflowServicesManager(ClientContext, SelectedWeb);
                var subscriptionService = servicesManager.GetWorkflowSubscriptionService();
                var subscriptions       = subscriptionService.EnumerateSubscriptionsByList(list.Id);

                ClientContext.Load(subscriptions);
                ClientContext.ExecuteQueryRetry();

                subscriptions.Where(subs => subs.Name == WorkflowName).Select(s => s.Id).ToList().ForEach(subscriptionId =>
                {
                    workflowSubscriptionId.Add(subscriptionId);
                });
            }


            // Check if the field exists
            var viewFields     = new string[] { "Id", "Title", FieldBoolean_RestartWorkflow };
            var internalFields = new List <string>();

            internalFields.AddRange(FieldDefinitions.Select(s => s.InternalName));

            try
            {
                var checkFields = list.GetFields(internalFields.ToArray());
            }
            catch (Exception ex)
            {
                Ilogger.LogError(ex, "Failed to retreive the fields {0}", ex.Message);

                foreach (var field in FieldDefinitions)
                {
                    // provision the column
                    var provisionedColumn = list.CreateListColumn(field, Ilogger, null);
                    if (provisionedColumn != null)
                    {
                        internalFields.Add(provisionedColumn.InternalName);
                    }
                }
            }

            var view = View.GetView(list);
            var internalViewFieldXml = new List <string>()
            {
                CAML.FieldRef("Id")
            };

            foreach (var vfield in view.ViewFields.Where(w => !w.Equals("ID", StringComparison.CurrentCultureIgnoreCase)))
            {
                internalViewFieldXml.Add(CAML.FieldRef(vfield));
            }

            var itemIds  = new List <int>();
            var viewCaml = new CamlQuery()
            {
                ViewXml = CAML.ViewQuery(ViewScope.RecursiveAll, view.ViewQuery, string.Empty, CAML.ViewFields(internalViewFieldXml.ToArray()), view.RowLimit.ToString().ToInt32(5)),
                ListItemCollectionPosition = null
            };

            do
            {
                var items = list.GetItems(viewCaml);
                this.ClientContext.Load(items, ftx => ftx.ListItemCollectionPosition, ftx => ftx.Include(ftcx => ftcx.Id, ftcx => ftcx.ParentList.Id, ftcx => ftcx[FieldBoolean_RestartWorkflow]));
                this.ClientContext.ExecuteQueryRetry();
                viewCaml.ListItemCollectionPosition = items.ListItemCollectionPosition;

                foreach (var item in items)
                {
                    // Load ParentList ID to Pull Workflow Instances
                    var allinstances = SelectedWeb.GetWorkflowInstances(item);
                    if (allinstances.Any())
                    {
                        foreach (var instance in allinstances)
                        {
                            objects.Add(new SPWorkflowInstance(instance, item.Id));
                        }

                        itemIds.Add(item.Id);
                    }
                }
            }while (viewCaml.ListItemCollectionPosition != null);

            var rowprocessed = itemIds.Count();

            if (rowprocessed > 0 && this.ShouldProcess($"Setting Restart Flag for {rowprocessed} items"))
            {
                var rowdx = 0; var totaldx = rowprocessed;

                foreach (var itemId in itemIds)
                {
                    rowdx++;
                    totaldx--;

                    var wfItem = list.GetItemById(itemId);
                    list.Context.Load(wfItem);
                    wfItem[FieldBoolean_RestartWorkflow] = true;
                    wfItem.SystemUpdate();

                    if (rowdx >= 50 || totaldx <= 0)
                    {
                        list.Context.ExecuteQueryRetry();
                        Ilogger.LogInformation($"Processing {rowprocessed} rows; Persisted {rowdx} rows; {totaldx} remaining");
                        rowdx = 0;
                    }
                }

                var cancelDirty      = false;
                var viewFieldXml     = CAML.ViewFields(viewFields.Select(s => CAML.FieldRef(s)).ToArray());
                var terminatedWFCaml = new CamlQuery()
                {
                    ViewXml = CAML.ViewQuery(ViewScope.RecursiveAll, CAML.Where(CAML.Eq(CAML.FieldValue(FieldBoolean_RestartWorkflow, FieldType.Boolean.ToString("f"), 1.ToString()))), string.Empty, viewFieldXml, 100),
                    ListItemCollectionPosition = null
                };

                do
                {
                    var items = list.GetItems(terminatedWFCaml);
                    list.Context.Load(items, ftx => ftx.ListItemCollectionPosition, ftx => ftx.Include(ftcx => ftcx.Id, ftcx => ftcx.ParentList.Id, ftcx => ftcx[FieldBoolean_RestartWorkflow]));
                    list.Context.ExecuteQueryRetry();
                    terminatedWFCaml.ListItemCollectionPosition = items.ListItemCollectionPosition;

                    rowdx = 0; totaldx = items.Count();

                    foreach (var item in items)
                    {
                        rowdx++;
                        totaldx--;

                        var itemId       = item.Id;
                        var allinstances = SelectedWeb.GetWorkflowInstances(item);
                        foreach (var instance in allinstances)
                        {
                            var instanceId = instance.Id;

                            var msg = $"List Item {itemId} => Cancelling subscription {instance.WorkflowSubscriptionId} instance Id {instanceId}";
                            Ilogger.LogWarning(msg);
                            cancelDirty = true;
                            instance.CancelWorkFlow();
                        }


                        if (cancelDirty &&
                            (rowdx >= 50 || totaldx <= 0))
                        {
                            list.Context.ExecuteQueryRetry();
                            Ilogger.LogInformation($"Processing {rowprocessed} rows; Persisted {rowdx} rows; {totaldx} remaining");
                            rowdx = 0;
                        }
                    }
                }while (terminatedWFCaml.ListItemCollectionPosition != null);
            }


            WriteObject(objects, true);
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();


            var objects = new List <SPWorkflowInstance>();


            var FieldDefinitions = new List <SPFieldDefinitionModel>
            {
                new SPFieldDefinitionModel(FieldType.Boolean)
                {
                    FieldGuid    = new Guid("da2872c4-e9b6-4804-9837-6e9dd85ecd7e"),
                    InternalName = FieldBoolean_RestartWorkflow,
                    Description  = "RestartWorkflow provides a way to identify items that should be restarted.",
                    Title        = FieldBoolean_RestartWorkflow,
                    MaxLength    = 255,
                    DefaultValue = "No"
                }
            };


            var SelectedWeb = this.ClientContext.Web;

            Ilogger = new DefaultUsageLogger(
                (string msg, object[] margs) =>
            {
                LogVerbose(msg, margs);
            },
                (string msg, object[] margs) =>
            {
                LogWarning(msg, margs);
            },
                (Exception ex, string msg, object[] margs) =>
            {
                LogError(ex, msg, margs);
            });

            var list = List.GetList(SelectedWeb, lctx => lctx.Id, lctx => lctx.Title);

            var workflowSubscriptionId = new List <Guid>();

            if (!string.IsNullOrEmpty(WorkflowName))
            {
                var servicesManager     = new WorkflowServicesManager(ClientContext, SelectedWeb);
                var subscriptionService = servicesManager.GetWorkflowSubscriptionService();
                var subscriptions       = subscriptionService.EnumerateSubscriptionsByList(list.Id);

                ClientContext.Load(subscriptions);
                ClientContext.ExecuteQueryRetry();

                subscriptions.Where(subs => subs.Name == WorkflowName).Select(s => s.Id).ToList().ForEach(subscriptionId =>
                {
                    workflowSubscriptionId.Add(subscriptionId);
                });
            }


            // Check if the field exists
            var viewFields     = new string[] { "Id", "Title", FieldBoolean_RestartWorkflow };
            var internalFields = new List <string>();

            internalFields.AddRange(FieldDefinitions.Select(s => s.InternalName));

            try
            {
                var checkFields = list.GetFields(internalFields.ToArray());
            }
            catch (Exception ex)
            {
                Ilogger.LogError(ex, "Failed to retreive the fields {0}", ex.Message);

                foreach (var field in FieldDefinitions)
                {
                    // provision the column
                    var provisionedColumn = list.CreateListColumn(field, Ilogger, null);
                    if (provisionedColumn != null)
                    {
                        internalFields.Add(provisionedColumn.InternalName);
                    }
                }
            }

            var workflowStati = new List <Microsoft.SharePoint.Client.WorkflowServices.WorkflowStatus>()
            {
                Microsoft.SharePoint.Client.WorkflowServices.WorkflowStatus.Started
            };

            var rowprocessed = 0;
            var rowdx = 0; var totaldx = 0;
            var startedDirty   = false;
            var viewFieldXml   = CAML.ViewFields(viewFields.Select(s => CAML.FieldRef(s)).ToArray());
            var initiateWFCaml = new CamlQuery()
            {
                ViewXml = CAML.ViewQuery(ViewScope.RecursiveAll, CAML.Where(CAML.Eq(CAML.FieldValue(FieldBoolean_RestartWorkflow, FieldType.Boolean.ToString("f"), 1.ToString()))), string.Empty, viewFieldXml, 100),
                ListItemCollectionPosition = null
            };


            do
            {
                var items = list.GetItems(initiateWFCaml);
                list.Context.Load(items, ftx => ftx.ListItemCollectionPosition, ftx => ftx.Include(ftcx => ftcx.Id, ftcx => ftcx.ParentList.Id, ftcx => ftcx[FieldBoolean_RestartWorkflow]));
                list.Context.ExecuteQueryRetry();
                initiateWFCaml.ListItemCollectionPosition = items.ListItemCollectionPosition;

                rowprocessed = items.Count();
                rowdx        = 0; totaldx = rowprocessed;

                foreach (var item in items)
                {
                    rowdx++;
                    totaldx--;

                    var itemId       = item.Id;
                    var allinstances = SelectedWeb.GetWorkflowInstances(item);
                    if (!allinstances.Any(w => workflowStati.Any(wx => wx == w.Status)))
                    {
                        foreach (var subscriptionId in workflowSubscriptionId)
                        {
                            Ilogger.LogWarning($"List Item {itemId} => Restarting subscription {subscriptionId}");
                            var instanceId = item.StartWorkflowInstance(subscriptionId, new Dictionary <string, object>());
                            Ilogger.LogWarning($"List Item {itemId} => Successfully restarted subscription {subscriptionId} with new instance Id {instanceId}");
                            startedDirty = true;

                            objects.Add(new SPWorkflowInstance()
                            {
                                Id = instanceId,
                                WorkflowSubscriptionId = subscriptionId,
                                ListItemId             = itemId,
                                InstanceCreated        = DateTime.UtcNow
                            });
                        }

                        var wfItem = list.GetItemById(itemId);
                        list.Context.Load(wfItem);
                        wfItem[FieldBoolean_RestartWorkflow] = false;
                        wfItem.SystemUpdate();
                    }

                    if (startedDirty && (rowdx >= 50 || totaldx <= 0))
                    {
                        list.Context.ExecuteQueryRetry();
                        Ilogger.LogInformation($"Processing {rowprocessed} rows; Persisted {rowdx} rows; {totaldx} remaining");
                        rowdx = 0;
                    }
                }
            }while (initiateWFCaml.ListItemCollectionPosition != null);


            WriteObject(objects, true);
        }