public SharepointUpdateListItemActivity()
 {
     DisplayName               = "Sharepoint Update List Item";
     ReadListItems             = new List <SharepointReadListTo>();
     FilterCriteria            = new List <SharepointSearchTo>();
     RequireAllCriteriaToMatch = true;
     _sharepointUtils          = new SharepointUtils();
 }
#pragma warning disable S1541 // Methods and properties should not be too complex
#pragma warning disable S3776 // Cognitive Complexity of methods should not be too high
        void AddInputDebug(IExecutionEnvironment env, int update)
#pragma warning restore S3776 // Cognitive Complexity of methods should not be too high
#pragma warning restore S1541 // Methods and properties should not be too complex
        {
            var validItems = SharepointUtils.GetValidReadListItems(ReadListItems).ToList();

            foreach (var varDebug in validItems)
            {
                var debugItem = new DebugItem();
                AddDebugItem(new DebugItemStaticDataParams("", _indexCounter.ToString(CultureInfo.InvariantCulture)), debugItem);
                var variableName = varDebug.VariableName;
                if (!string.IsNullOrEmpty(variableName))
                {
                    AddDebugItem(new DebugItemStaticDataParams(varDebug.FieldName, "Field Name"), debugItem);
                    AddDebugItem(new DebugEvalResult(variableName, "Variable", env, update), debugItem);
                }
                _indexCounter++;
                _debugInputs.Add(debugItem);
            }
            if (FilterCriteria != null && FilterCriteria.Any())
            {
                var requireAllCriteriaToMatch = RequireAllCriteriaToMatch ? "Yes" : "No";

                foreach (var varDebug in FilterCriteria)
                {
                    if (string.IsNullOrEmpty(varDebug.FieldName))
                    {
                        return;
                    }

                    var debugItem = new DebugItem();
                    AddDebugItem(new DebugItemStaticDataParams("", _indexCounter.ToString(CultureInfo.InvariantCulture)), debugItem);
                    var fieldName = varDebug.FieldName;
                    if (!string.IsNullOrEmpty(fieldName))
                    {
                        AddDebugItem(new DebugEvalResult(fieldName, "Field Name", env, update), debugItem);
                    }
                    var searchType = varDebug.SearchType;
                    if (!string.IsNullOrEmpty(searchType))
                    {
                        AddDebugItem(new DebugEvalResult(searchType, "Search Type", env, update), debugItem);
                    }
                    var valueToMatch = varDebug.ValueToMatch;
                    if (!string.IsNullOrEmpty(valueToMatch))
                    {
                        AddDebugItem(new DebugEvalResult(valueToMatch, "Value", env, update), debugItem);
                    }

                    AddDebugItem(new DebugEvalResult(requireAllCriteriaToMatch, "Require All Criteria To Match", env, update), debugItem);

                    _indexCounter++;
                    _debugInputs.Add(debugItem);
                }
            }
        }
 public void SharepointUtils_GetValidReadListItems_NullList_EmptyList()
 {
     //------------Setup for test--------------------------
     var sharepointUtils = new SharepointUtils();
     
     //------------Execute Test---------------------------
     var validList = sharepointUtils.GetValidReadListItems(null);
     //------------Assert Results-------------------------
     Assert.IsNotNull(validList);
     Assert.AreEqual(0,validList.Count());
 }
        private void TryExecute(IDSFDataObject dataObject, int update, List <SharepointReadListTo> sharepointReadListTos)
        {
            var sharepointSource = ResourceCatalog.GetResource <SharepointSource>(dataObject.WorkspaceID, SharepointServerResourceId);
            var listOfIterators  = new Dictionary <string, IWarewolfIterator>();

            if (sharepointSource == null)
            {
                var contents = ResourceCatalog.GetResourceContents(dataObject.WorkspaceID, SharepointServerResourceId);
                sharepointSource = new SharepointSource(contents.ToXElement());
            }
            var env = dataObject.Environment;

            if (dataObject.IsDebugMode())
            {
                AddInputDebug(env, update);
            }
            var sharepointHelper = sharepointSource.CreateSharepointHelper();
            var fields           = sharepointHelper.LoadFieldsForList(SharepointList, true);

            using (var ctx = sharepointHelper.GetContext())
            {
                var list         = sharepointHelper.LoadFieldsForList(SharepointList, ctx, true);
                var iteratorList = new WarewolfListIterator();
                foreach (var sharepointReadListTo in sharepointReadListTos)
                {
                    var warewolfIterator = new WarewolfIterator(env.Eval(sharepointReadListTo.VariableName, update));
                    iteratorList.AddVariableToIterateOn(warewolfIterator);
                    listOfIterators.Add(sharepointReadListTo.FieldName, warewolfIterator);
                }
                while (iteratorList.HasMoreData())
                {
                    var itemCreateInfo = new ListItemCreationInformation();
                    var listItem       = list.AddItem(itemCreateInfo);
                    foreach (var warewolfIterator in listOfIterators)
                    {
                        var sharepointFieldTo = fields.FirstOrDefault(to => to.Name == warewolfIterator.Key);
                        if (sharepointFieldTo != null)
                        {
                            object value = warewolfIterator.Value.GetNextValue();
                            value = SharepointUtils.CastWarewolfValueToCorrectType(value, sharepointFieldTo.Type);
                            listItem[sharepointFieldTo.InternalName] = value;
                        }
                    }
                    listItem.Update();
                    ctx.ExecuteQuery();
                }
            }
            if (!string.IsNullOrEmpty(Result))
            {
                env.Assign(Result, "Success", update);
                AddOutputDebug(dataObject, env, update);
            }
        }
 public void SharepointUtils_GetValidReadListItems_WhereVariableNameEmpty_ListWithoutItem()
 {
     //------------Setup for test--------------------------
     var sharepointUtils = new SharepointUtils();
     
     //------------Execute Test---------------------------
     var validList = sharepointUtils.GetValidReadListItems(new List<SharepointReadListTo> { new SharepointReadListTo("Bob", "Title", "Title", ""), new SharepointReadListTo("", "Title", "Title", "") });
     //------------Assert Results-------------------------
     Assert.IsNotNull(validList);
     var tos = validList as IList<SharepointReadListTo> ?? validList.ToList();
     Assert.AreEqual(1,tos.Count());
     Assert.AreEqual("Bob",tos[0].VariableName);
 }
        private void ExecuteConcreteAction(IDSFDataObject dataObject, int update)
        {
            var sharepointReadListTos = SharepointUtils.GetValidReadListItems(ReadListItems).ToList();

            if (sharepointReadListTos.Any())
            {
                var sharepointSource = ResourceCatalog.GetResource <SharepointSource>(dataObject.WorkspaceID, SharepointServerResourceId);
                var listOfIterators  = new Dictionary <string, IWarewolfIterator>();
                if (sharepointSource == null)
                {
                    var contents = ResourceCatalog.GetResourceContents(dataObject.WorkspaceID, SharepointServerResourceId);
                    sharepointSource = new SharepointSource(contents.ToXElement());
                }
                var env = dataObject.Environment;
                if (dataObject.IsDebugMode())
                {
                    AddInputDebug(env, update);
                }
                var sharepointHelper = sharepointSource.CreateSharepointHelper();
                var fields           = sharepointHelper.LoadFieldsForList(SharepointList, true);
                using (var ctx = sharepointHelper.GetContext())
                {
                    var camlQuery = _sharepointUtils.BuildCamlQuery(env, FilterCriteria, fields, update, RequireAllCriteriaToMatch);
                    var list      = ctx.Web.Lists.GetByTitle(SharepointList);
                    var listItems = list.GetItems(camlQuery);
                    ctx.Load(listItems);
                    ctx.ExecuteQuery();
                    var iteratorList = new WarewolfListIterator();
                    foreach (var sharepointReadListTo in sharepointReadListTos)
                    {
                        var warewolfIterator = new WarewolfIterator(env.Eval(sharepointReadListTo.VariableName, update));
                        iteratorList.AddVariableToIterateOn(warewolfIterator);
                        listOfIterators.Add(sharepointReadListTo.InternalName, warewolfIterator);
                    }
                    foreach (var listItem in listItems)
                    {
                        foreach (var warewolfIterator in listOfIterators)
                        {
                            listItem[warewolfIterator.Key] = warewolfIterator.Value.GetNextValue();
                        }
                        listItem.Update();
                        ctx.ExecuteQuery();
                    }
                }
                if (!string.IsNullOrEmpty(Result))
                {
                    env.Assign(Result, "Success", update);
                    AddOutputDebug(dataObject, env, update);
                }
            }
        }
        void AddInputDebug(IExecutionEnvironment env, int update)
        {
            var validItems = SharepointUtils.GetValidReadListItems(ReadListItems).ToList();

            foreach (var varDebug in validItems)
            {
                var debugItem = new DebugItem();
                AddDebugItem(new DebugItemStaticDataParams("", _indexCounter.ToString(CultureInfo.InvariantCulture)), debugItem);
                var variableName = varDebug.VariableName;
                if (!string.IsNullOrEmpty(variableName))
                {
                    AddDebugItem(new DebugItemStaticDataParams(varDebug.FieldName, "Field Name"), debugItem);
                    AddDebugItem(new DebugEvalResult(variableName, "Variable", env, update), debugItem);
                }
                _indexCounter++;
                _debugInputs.Add(debugItem);
            }
        }
        protected override void ExecuteTool(IDSFDataObject dataObject, int update)
        {
            _debugInputs  = new List <DebugItem>();
            _debugOutputs = new List <DebugItem>();
            _indexCounter = 1;
            var allErrors = new ErrorResultTO();

            try
            {
                var sharepointReadListTos = SharepointUtils.GetValidReadListItems(ReadListItems).ToList();
                if (sharepointReadListTos.Any())
                {
                    TryExecute(dataObject, update, sharepointReadListTos);
                }
            }
            catch (Exception e)
            {
                Dev2Logger.Error("SharepointCreateListItemActivity", e, GlobalConstants.WarewolfError);
                allErrors.AddError(e.Message);
            }
            finally
            {
                var hasErrors = allErrors.HasErrors();
                if (hasErrors)
                {
                    dataObject.Environment.Assign(Result, "Failed", update);
                    var errorString = allErrors.MakeDisplayReady();
                    dataObject.Environment.AddError(errorString);
                    DisplayAndWriteError(dataObject, DisplayName, allErrors);
                }
                if (dataObject.IsDebugMode())
                {
                    DispatchDebugState(dataObject, StateType.Before, update);
                    DispatchDebugState(dataObject, StateType.After, update);
                }
            }
        }
 public void SharepointUtils_CastWarewolfValueToCorrectType_Currency_ShouldGiveDecimalValue()
 {
     //------------Setup for test--------------------------
     var sharepointUtils = new SharepointUtils();
     
     //------------Execute Test---------------------------
     var boolValue = sharepointUtils.CastWarewolfValueToCorrectType("2.01", SharepointFieldType.Currency);
     //------------Assert Results-------------------------
     Assert.IsInstanceOfType(boolValue,typeof(Decimal));
 }
 public SharepointCreateListItemActivity()
 {
     DisplayName      = "Sharepoint Create List Item";
     ReadListItems    = new List <SharepointReadListTo>();
     _sharepointUtils = new SharepointUtils();
 }
 public void SharepointUtils_BuildCamlQuery_NoFilters_ShouldBeCreateAllItemsQuery()
 {
     //------------Setup for test--------------------------
     var sharepointUtils = new SharepointUtils();
     
     //------------Execute Test---------------------------
     var camlQuery = sharepointUtils.BuildCamlQuery(new ExecutionEnvironment(), new List<SharepointSearchTo>(), new List<ISharepointFieldTo>(),0);
     //------------Assert Results-------------------------
     Assert.AreEqual(CamlQuery.CreateAllItemsQuery().ViewXml,camlQuery.ViewXml);
 }
 public void SharepointUtils_BuildCamlQuery_ValidFilter_In_RecSetResultCommaSeperated()
 {
     //------------Setup for test--------------------------
     var sharepointUtils = new SharepointUtils();
     var executionEnvironment = new ExecutionEnvironment();
     executionEnvironment.Assign("[[names().name]]", "bob,dora", 0);
     //------------Execute Test---------------------------
     var camlQuery = sharepointUtils.BuildCamlQuery(executionEnvironment, new List<SharepointSearchTo> { new SharepointSearchTo("Title", "In", "[[names(*).name]]", 1) { InternalName = "Title" } }, new List<ISharepointFieldTo> { new SharepointFieldTo { InternalName = "Title", Type = SharepointFieldType.Text } }, 0);
     //------------Assert Results-------------------------
     Assert.AreEqual("<View><Query><Where><In><FieldRef Name=\"Title\"></FieldRef><Values><Value Type=\"Text\">bob</Value><Value Type=\"Text\">dora</Value></Values></In>" + Environment.NewLine + "</Where></Query></View>", camlQuery.ViewXml);
 }
 public void SharepointUtils_BuildCamlQuery_ValidFilters_HasQueryWithAnd()
 {
     //------------Setup for test--------------------------
     var sharepointUtils = new SharepointUtils();
     
     //------------Execute Test---------------------------
     var camlQuery = sharepointUtils.BuildCamlQuery(new ExecutionEnvironment(), new List<SharepointSearchTo> { new SharepointSearchTo("Title", "Equal", "Bob", 1) { InternalName = "Title" }, new SharepointSearchTo("ID", "Equal", "1", 1) { InternalName = "ID" } }, new List<ISharepointFieldTo> { new SharepointFieldTo { InternalName = "Title", Type = SharepointFieldType.Text }, new SharepointFieldTo { InternalName = "ID", Type = SharepointFieldType.Number } }, 0);
     //------------Assert Results-------------------------
     Assert.AreEqual("<View><Query><Where><And><FieldRef Name=\"Title\"></FieldRef><Value Type=\"Text\">Bob</Value>" + Environment.NewLine + "<FieldRef Name=\"ID\"></FieldRef><Value Type=\"Integer\">1</Value>" + Environment.NewLine + "</And></Where></Query></View>", camlQuery.ViewXml);
 }
 public void SharepointUtils_CastWarewolfValueToCorrectType_Date_ShouldGiveDateValueValue()
 {
     //------------Setup for test--------------------------
     var sharepointUtils = new SharepointUtils();
     
     //------------Execute Test---------------------------
     var boolValue = sharepointUtils.CastWarewolfValueToCorrectType(DateTime.Now, SharepointFieldType.DateTime);
     //------------Assert Results-------------------------
     Assert.IsInstanceOfType(boolValue,typeof(DateTime));
 }
 public void SharepointUtils_CastWarewolfValueToCorrectType_Boolean_ShouldGiveBoolValue()
 {
     //------------Setup for test--------------------------
     var sharepointUtils = new SharepointUtils();
     
     //------------Execute Test---------------------------
     var boolValue = sharepointUtils.CastWarewolfValueToCorrectType("true", SharepointFieldType.Boolean);
     //------------Assert Results-------------------------
     Assert.IsInstanceOfType(boolValue,typeof(Boolean));
 } 
 public void SharepointUtils_CastWarewolfValueToCorrectType_Number_ShouldGiveIntValue()
 {
     //------------Setup for test--------------------------
     var sharepointUtils = new SharepointUtils();
     
     //------------Execute Test---------------------------
     var boolValue = sharepointUtils.CastWarewolfValueToCorrectType("2", SharepointFieldType.Number);
     //------------Assert Results-------------------------
     Assert.IsInstanceOfType(boolValue,typeof(Int32));
 }
 public void SharepointUtils_CastWarewolfValueToCorrectType_Note_ShouldGiveStringValue()
 {
     //------------Setup for test--------------------------
     var sharepointUtils = new SharepointUtils();
     
     //------------Execute Test---------------------------
     var boolValue = sharepointUtils.CastWarewolfValueToCorrectType("Bob", SharepointFieldType.Note);
     //------------Assert Results-------------------------
     Assert.IsInstanceOfType(boolValue,typeof(String));
 }