Exemplo n.º 1
0
        public override PublishResponse Publish(PublishRequest request, IDataTransport dataTransport)
        {
            Logger.LogInfo("Calling Publish");
            var input = (string)_settings["input"];

            var dataPoints = new List <DataPoint>();

            var shape = _shapes.FirstOrDefault(s => s.Name.Equals(request.ShapeName, StringComparison.OrdinalIgnoreCase));

            using (var sr = new StreamReader(File.OpenRead(input)))
                using (var csvReader = new CsvReader(sr))
                {
                    // read past header row
                    csvReader.Read();

                    // loop over data fields
                    while (csvReader.Read())
                    {
                        var dp = new DataPoint
                        {
                            TenantID = "mytenantid",
                            Action   = DataPointAction.Upsert,
                            Entity   = shape.Name,
                            Source   = "CRM",
                            KeyNames = shape.Keys,
                            Data     = new Dictionary <string, object>()
                        };

                        for (var i = 0; i < shape.Properties.Count; i++)
                        {
                            var prop = shape.Properties[i];

                            if (prop.Type == "number")
                            {
                                dp.Data[prop.Name] = csvReader.GetField <int>(i);
                            }
                            else
                            {
                                dp.Data[prop.Name] = csvReader.GetField <string>(i);
                            }
                        }

                        dataPoints.Add(dp);
                    }
                }

            dataTransport.Send(dataPoints);
            dataTransport.Done();

            return(new PublishResponse());
        }
        public override PublishResponse Publish(PublishRequest request, IDataTransport dataTransport)
        {
            var shapeToPublish = _shapes.FirstOrDefault(s => s.Name == request.ShapeName);

            ClientContext clientContext = new ClientContext(_siteUrl);

            clientContext.Credentials = new System.Net.NetworkCredential(_username, _password, _domain);
            Web site = clientContext.Web;

            clientContext.Load(site);
            clientContext.ExecuteQuery();

            var list = site.Lists.GetByTitle(request.ShapeName);

            CamlQuery query = new CamlQuery();

            query.ViewXml = "<View><Query></Query></View>";
            var items = list.GetItems(query);

            clientContext.Load(items);
            clientContext.ExecuteQuery();

            foreach (var item in items)
            {
                var dataPoint = new DataPoint();
                dataPoint.Action = DataPointAction.Upsert;
                dataPoint.Entity = request.ShapeName;
                dataPoint.Data   = new Dictionary <string, object>();

                foreach (var prop in shapeToPublish.Properties)
                {
                    var fieldName = prepareFieldName(prop.Name);
                    if (item.FieldValues.ContainsKey(fieldName))
                    {
                        dataPoint.Data[prop.Name] = item.FieldValues[fieldName];
                    }
                    else
                    {
                        dataPoint.Data[prop.Name] = null;
                    }
                }

                dataTransport.Send(new List <DataPoint>(new [] { dataPoint }));
            }

            return(new PublishResponse {
                Success = true
            });
        }