public TransactionTable UpdateItems(WebProperties WebProps, DataTable Items, IntegrationLog Log)
        {
            CheckArgumentForNull(WebProps, nameof(WebProps));
            CheckArgumentForNull(Items, nameof(Items));
            CheckArgumentForNull(Log, nameof(Log));

            const string queryTemplate = "SELECT * FROM {0} WHERE {1}=@id";
            var          tableName     = WebProps.Properties[PropKeyTable];
            var          idColumn      = WebProps.Properties[PropKeyIdColumn];
            var          trans         = new TransactionTable();

            using (var connection = GetConnection(WebProps.Properties))
            {
                connection.Open();

                foreach (DataRow drItem in Items.Rows)
                {
                    var currentId = drItem[ColNameId].ToString();
                    var spId      = drItem[ColNameSpid].ToString();

                    try
                    {
                        if (currentId == string.Empty)
                        {
                            trans.AddRow(spId, InsertRow(WebProps, drItem, Log, connection), TransactionType.INSERT);
                        }
                        else
                        {
                            var dataSet = new DataSet();
                            using (var command = new SqlCommand(string.Format(queryTemplate, tableName, idColumn), connection))
                            {
                                command.Parameters.AddWithValue("@id", currentId);

                                using (var dataAdapter = new SqlDataAdapter(command))
                                {
                                    dataAdapter.Fill(dataSet);
                                }
                            }

                            if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
                            {
                                trans.AddRow(spId, UpdateRow(WebProps, drItem, Log, connection), TransactionType.UPDATE);
                            }
                            else
                            {
                                trans.AddRow(spId, InsertRow(WebProps, drItem, Log, connection), TransactionType.INSERT);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError(ex.ToString());
                        Log.LogMessage(ex.Message, IntegrationLogType.Error);
                        trans.AddRow(spId, currentId, TransactionType.FAILED);
                    }
                }
            }

            return(trans);
        }
        // Public Methods (9) 

        public TransactionTable DeleteItems(WebProperties webProps, DataTable items, IntegrationLog log)
        {
            var transactionTable = new TransactionTable();

            try
            {
                if (!bool.Parse((string)webProps.Properties["AllowDeleteInt"]))
                {
                    throw new Exception("Office 365 delete is not allowed.");
                }

                O365Service o365Service = GetO365Service(webProps);

                List <string> ids;
                Dictionary <string, string> idMap = BuildIdMap(items, out ids);

                int index = 0;

                foreach (
                    O365Result result in
                    o365Service.DeleteListItemsById(ids.ToArray(), (string)webProps.Properties["List"]))
                {
                    string o365Id = result.ItemId.ToString();
                    string spid;

                    try
                    {
                        spid = idMap[o365Id];
                    }
                    catch
                    {
                        spid = items.Rows[index]["SPID"].ToString();
                    }

                    if (result.Success)
                    {
                        transactionTable.AddRow(spid, o365Id, TransactionType.DELETE);
                    }
                    else
                    {
                        transactionTable.AddRow(spid, o365Id, TransactionType.FAILED);
                        log.LogMessage(string.Format(
                                           "Could not delete record with Office 365 ID: {0}, SharePoint ID: {1}. Message: {2}",
                                           o365Id, spid, result.Error), IntegrationLogType.Warning);
                    }

                    index++;
                }
            }
            catch (Exception e)
            {
                log.LogMessage(e.Message, IntegrationLogType.Error);
            }

            return(transactionTable);
        }
        public TransactionTable UpdateItems(WebProperties webProps, DataTable items, IntegrationLog log)
        {
            var transactionTable = new TransactionTable();

            try
            {
                O365Service o365Service = GetO365Service(webProps);

                List <string> ids;
                Dictionary <string, string> idMap = BuildIdMap(items, out ids);

                int index = 0;

                foreach (
                    O365Result result in
                    o365Service.UpsertItems((string)webProps.Properties["List"], webProps.IntegrationId, items))
                {
                    string o365Id = result.ItemId.ToString();

                    string spId;

                    try
                    {
                        spId = idMap[o365Id];
                    }
                    catch
                    {
                        spId = items.Rows[index]["SPID"].ToString();
                    }

                    if (result.Success)
                    {
                        transactionTable.AddRow(spId, o365Id, result.TransactionType);
                    }
                    else
                    {
                        transactionTable.AddRow(spId, o365Id, TransactionType.FAILED);

                        log.LogMessage(string.Format(
                                           "Could not insert / update record with Office 365 ID: {0}, SharePoint ID: {1}. Message: {2}",
                                           o365Id, spId, result.Error), IntegrationLogType.Warning);
                    }

                    index++;
                }
            }
            catch (Exception e)
            {
                log.LogMessage(e.Message, IntegrationLogType.Error);
            }

            return(transactionTable);
        }
        public TransactionTable DeleteItems(WebProperties WebProps, DataTable Items, IntegrationLog Log)
        {
            CheckArgumentForNull(WebProps, nameof(WebProps));
            CheckArgumentForNull(Items, nameof(Items));

            const string deleteCommandTemplate = "DELETE FROM {0} WHERE {1}=@id";
            var          tableName             = WebProps.Properties[PropKeyTable];
            var          idColumn = WebProps.Properties[PropKeyIdColumn];

            var table = new TransactionTable();

            using (var connection = GetConnection(WebProps.Properties))
            {
                connection.Open();

                foreach (DataRow dataRow in Items.Rows)
                {
                    try
                    {
                        using (var command = new SqlCommand(string.Format(deleteCommandTemplate, tableName, idColumn), connection))
                        {
                            command.Parameters.AddWithValue("@id", dataRow[ColNameId].ToString());
                            command.ExecuteNonQuery();
                        }

                        table.AddRow(dataRow[ColNameSpid].ToString(), dataRow[ColNameId].ToString(), TransactionType.DELETE);
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError(ex.ToString());
                        table.AddRow(dataRow[ColNameSpid].ToString(), dataRow[ColNameId].ToString(), TransactionType.FAILED);
                    }
                }
            }

            return(table);
        }
        public TransactionTable UpdateItems(WebProperties webProps, DataTable items, IntegrationLog log)
        {
            var transactionTable = new TransactionTable();

            try
            {
                SfService sfService = GetSfService(webProps);

                if (!sfService.IsSyncEnabled())
                {
                    throw new Exception("All synchronizations are currently disabled.");
                }

                List <string> ids;
                Dictionary <string, string> idMap = BuildIdMap(items, out ids);

                int index = 0;

                foreach (var result in sfService.UpsertItems((string)webProps.Properties["Object"], items))
                {
                    foreach (var pair in result)
                    {
                        SaveResult upsertResult = pair.Value;

                        string sfId = upsertResult.id;
                        string spid;

                        try
                        {
                            spid = idMap[sfId];
                        }
                        catch
                        {
                            spid = items.Rows[index]["SPID"].ToString();
                        }

                        if (upsertResult.success)
                        {
                            transactionTable.AddRow(spid, sfId,
                                                    pair.Key == UpsertKind.INSERT
                                                        ? TransactionType.INSERT
                                                        : TransactionType.UPDATE);
                        }
                        else
                        {
                            transactionTable.AddRow(spid, sfId, TransactionType.FAILED);
                            if (upsertResult.errors.Any())
                            {
                                Error[] errors = upsertResult.errors;

                                for (int i = 0; i < errors.Count(); i++)
                                {
                                    var error = errors[i];

                                    string fields = string.Empty;

                                    if (error.fields != null)
                                    {
                                        try
                                        {
                                            fields = "Fields: " + string.Join(",", error.fields) + ".";
                                        }
                                        catch { }
                                    }

                                    log.LogMessage(
                                        string.Format(
                                            "Could not insert / update record with Salesforce ID: {0}, SharePoint ID: {1}. Status code: {2}. Message: {3} {4}",
                                            sfId, spid, error.statusCode, error.message, fields), IntegrationLogType.Warning);
                                }
                            }
                        }
                    }

                    index++;
                }
            }
            catch (Exception e)
            {
                log.LogMessage(e.Message, IntegrationLogType.Error);
            }

            return(transactionTable);
        }
        // Public Methods (9) 

        public TransactionTable DeleteItems(WebProperties webProps, DataTable items, IntegrationLog log)
        {
            var transactionTable = new TransactionTable();

            try
            {
                if (!bool.Parse((string)webProps.Properties["AllowDeleteInt"]))
                {
                    throw new Exception("Salesforce delete is not allowed.");
                }

                SfService sfService = GetSfService(webProps);

                if (!sfService.IsSyncEnabled())
                {
                    throw new Exception("All synchronizations are currently disabled.");
                }

                List <string> ids;
                Dictionary <string, string> idMap = BuildIdMap(items, out ids);

                int index = 0;

                foreach (DeleteResult deleteResult in sfService.DeleteObjectItemsById(ids.ToArray()))
                {
                    string sfId = deleteResult.id;
                    string spid;

                    try
                    {
                        spid = idMap[sfId];
                    }
                    catch
                    {
                        spid = items.Rows[index]["SPID"].ToString();
                    }

                    if (deleteResult.success)
                    {
                        transactionTable.AddRow(spid, sfId, TransactionType.DELETE);
                    }
                    else
                    {
                        transactionTable.AddRow(spid, sfId, TransactionType.FAILED);
                        if (deleteResult.errors.Any())
                        {
                            Error[] errors = deleteResult.errors;

                            for (int i = 0; i < errors.Count(); i++)
                            {
                                log.LogMessage(
                                    string.Format(
                                        "Could not delete record with Salesforce ID: {0}, SharePoint ID: {1}. Status code: {2}. Message: {3}. Fields: {4}",
                                        sfId, spid, errors[i].statusCode, errors[i].message,
                                        string.Join(",", errors[i].fields)), IntegrationLogType.Warning);
                            }
                        }
                    }

                    index++;
                }
            }
            catch (Exception e)
            {
                log.LogMessage(e.Message, IntegrationLogType.Error);
            }

            return(transactionTable);
        }