public async Task AddOrUpdateItem(string connectionId, ExternalItem item)
        {
            try
            {
                // The SDK's auto-generated request builder uses POST here,
                // which isn't correct. For now, get the HTTP request and change it
                // to PUT manually.
                var putItemRequest = _graphClient.External.Connections[connectionId]
                                     .Items[item.Id].Request().GetHttpRequestMessage();

                putItemRequest.Method  = HttpMethod.Put;
                putItemRequest.Content = _graphClient.HttpProvider.Serializer.SerializeAsJsonContent(item);

                var response = await _graphClient.HttpProvider.SendAsync(putItemRequest);

                if (!response.IsSuccessStatusCode)
                {
                    throw new ServiceException(
                              new Error
                    {
                        Code    = response.StatusCode.ToString(),
                        Message = "Error indexing item."
                    }
                              );
                }
            }
            catch (Exception exception)
            {
                throw new Exception("Error: ", exception);
            }
        }
        public void ItemMarkedForFuturePublishing_IsPublished_WhenPublishingTimeIsReached()
        {
            var item = new ExternalItem {
                Title = "Original", State = ContentState.Published, Published = DateTime.Now.AddSeconds(-10)
            };

            using (engine.SecurityManager.Disable())
            {
                engine.Persister.Save(item);

                var version = versionManager.AddVersion(item, asPreviousVersion: false);
                version.Title = "ToBePublished";
                action.MarkForFuturePublishing(version, DateTime.Now.AddSeconds(-5));
                versionManager.UpdateVersion(version);
            }

            action.Execute();

            var published   = engine.Persister.Get(item.ID);
            var allVersions = versionManager.GetVersionsOf(published);
            var unpublished = allVersions.Single(v => v.State == ContentState.Unpublished);

            allVersions.Count.ShouldBe(2);
            published.Title.ShouldBe("ToBePublished");
            unpublished.Title.ShouldBe("Original");
        }
        private static async Task UploadItemsFromCsvAsync(string filePath)
        {
            if (_currentConnection == null)
            {
                Output.WriteLine(Output.Warning, "No connection selected. Please create a new connection or select an existing connection.");
                return;
            }

            var records = CsvDataLoader.LoadDataFromCsv(filePath);

            foreach (var part in records)
            {
                var newItem = new ExternalItem
                {
                    Id      = part.PartNumber.ToString(),
                    Content = new ExternalItemContent
                    {
                        // Need to set to null, service returns 400
                        // if @odata.type property is sent
                        ODataType = null,
                        Type      = ExternalItemContentType.Text,
                        Value     = part.Description
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType     = AccessType.Grant,
                            Type           = AclType.Everyone,
                            Value          = _tenantId,
                            IdentitySource = "Azure Active Directory"
                        }
                    },
                    Properties = part.AsExternalItemProperties()
                };

                try
                {
                    Output.Write(Output.Info, $"Uploading part number {part.PartNumber}...");
                    await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem);

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    Output.WriteLine(Output.Error, "FAILED");
                    Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.PartNumber}");
                    Output.WriteLine(Output.Error, serviceException.Message);
                }
            }
        }
        public void ItemBecomeExpired_ByDateChange_ChangesStateToUnpublished()
        {
            var item = new ExternalItem {
                State = ContentState.Published, Published = DateTime.Now.AddSeconds(-10), Expires = DateTime.Now.AddSeconds(-5)
            };

            using (engine.SecurityManager.Disable())
            {
                engine.Persister.Save(item);
            }
            action.Execute();

            item.State.ShouldBe(ContentState.Unpublished);
        }
Esempio n. 5
0
        //
        // Summary:
        //     Indexes data for custom search conenctor
        //
        // Parameters:
        //     listItems: Data that needs to be updated
        //     connectionId: Connection Id for which crawl needs to be done
        //     tenantId: Tenant Id
        private static async Task UpdateSearchIndex(dynamic listItems, string connectionId, string tenantId)
        {
            _log.LogInformation("Updating custom search connector index...");
            foreach (var listItem in listItems)
            {
                int lid = Convert.ToInt32(listItem.Id.ToString());
                _log.LogInformation("Updating SharePoint list item: {0}", lid);

                string[]      parts     = listItem.Fields.AdditionalData["Appliances"].ToString().Split(',');
                List <string> partsList = new List <string>(parts);

                AppliancePart part = new AppliancePart()
                {
                    PartNumber  = lid,
                    Description = listItem.Fields.AdditionalData["Description"].ToString(),
                    Name        = listItem.Fields.AdditionalData["Title"].ToString(),
                    Price       = Convert.ToDouble(listItem.Fields.AdditionalData["Price"].ToString()),
                    Inventory   = Convert.ToInt32(listItem.Fields.AdditionalData["Inventory"].ToString()),
                    Appliances  = partsList
                };

                var newItem = new ExternalItem
                {
                    Id      = part.PartNumber.ToString(),
                    Content = new ExternalItemContent
                    {
                        // Need to set to null, service returns 400
                        // if @odata.type property is sent
                        ODataType = null,
                        Type      = ExternalItemContentType.Text,
                        Value     = part.Description
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType     = AccessType.Grant,
                            Type           = AclType.Everyone,
                            Value          = tenantId,
                            IdentitySource = "Azure Active Directory"
                        }
                    },
                    Properties = part.AsExternalItemProperties()
                };

                await _graphHelper.AddOrUpdateItem(connectionId, newItem);

                _log.LogInformation("Item updated!");
            }
            _log.LogInformation("Custom search connector index update completed...");
        }
Esempio n. 6
0
        public void ItemBecomePublished_ByDateChange_ChangesStateToPublished()
        {
            var item = new ExternalItem {
                State = ContentState.Waiting, Published = N2.Utility.CurrentTime().AddSeconds(-10)
            };

            using (engine.SecurityManager.Disable())
            {
                engine.Persister.Save(item);
            }

            action.Execute();

            item.State.ShouldBe(ContentState.Published);
        }
Esempio n. 7
0
        public async Task AddOrUpdateItem(string connectionId, ExternalItem item)
        {
            var payload = JsonConvert.SerializeObject(item, _serializerSettings);

            // First attempt to add an item using PUT
            var request = new HttpRequestMessage(HttpMethod.Put, $"external/connections/{connectionId}/items/{item.Id}");

            request.Content = new StringContent(payload);
            request.Content.Headers.ContentType.MediaType = "application/json";

            var response = await _graphClient.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            throw await ExceptionFromResponseAsync(response);
        }
        /// <summary>
        /// Create new navigation property to items for connections
        /// <param name="body"></param>
        /// <param name="requestConfiguration">Configuration for the request such as headers, query parameters, and middleware options.</param>
        /// </summary>
        public RequestInformation CreatePostRequestInformation(ExternalItem body, Action <ItemsRequestBuilderPostRequestConfiguration> requestConfiguration = default)
        {
            _ = body ?? throw new ArgumentNullException(nameof(body));
            var requestInfo = new RequestInformation {
                HttpMethod     = Method.POST,
                UrlTemplate    = UrlTemplate,
                PathParameters = PathParameters,
            };

            requestInfo.SetContentFromParsable(RequestAdapter, "application/json", body);
            if (requestConfiguration != null)
            {
                var requestConfig = new ItemsRequestBuilderPostRequestConfiguration();
                requestConfiguration.Invoke(requestConfig);
                requestInfo.AddRequestOptions(requestConfig.Options);
                requestInfo.AddHeaders(requestConfig.Headers);
            }
            return(requestInfo);
        }
        public async Async.Task AddExternalItemWithPutPartialTest()
        {
            var externalItem = new ExternalItem
            {
                Acl = new List <Acl>()
                {
                    new Acl
                    {
                        Type           = AclType.User,
                        Value          = "e811976d-83df-4cbd-8b9b-5215b18aa874",
                        AccessType     = AccessType.Grant,
                        IdentitySource = "azureActiveDirectory"
                    },
                    new Acl
                    {
                        Type           = AclType.Group,
                        Value          = "14m1b9c38qe647f6a",
                        AccessType     = AccessType.Deny,
                        IdentitySource = "external"
                    }
                },
                Properties = new Microsoft.Graph.Properties
                {
                    AdditionalData = new Dictionary <string, object>()
                    {
                        { "title", "Error in the payment gateway" },
                        { "priority", "1" },
                        { "assignee", "*****@*****.**" }
                    }
                },
                Content = new ExternalItemContent
                {
                    Value = "Error in payment gateway...",
                    Type  = ExternalItemContentType.Text
                }
            };


            await graphClient.External.Connections["contosohr"].Items["TSP228082938"].Request().PutAsync(externalItem);
        }
Esempio n. 10
0
        public void VersionToBePublished_IsNotStoredInVersionList()
        {
            using (engine.SecurityManager.Disable())
            {
                var item = new ExternalItem {
                    Title = "Original", State = ContentState.Published, Published = DateTime.Now.AddSeconds(-10)
                };
                engine.Persister.Save(item);

                var version = new ExternalItem {
                    Title = "ToBePublished", State = ContentState.Published, Published = DateTime.Now.AddSeconds(-10), VersionOf = item
                };
                action.MarkForFuturePublishing(version, DateTime.Now.AddSeconds(-5));
                engine.Persister.Save(version);
            }

            action.Execute();

            var all = engine.Persister.Repository.Find();

            all.Count().ShouldBe(2);
        }
Esempio n. 11
0
        private static async Task UploadItemsFromCsvAsync(string filePath)
        {
            var records = CsvDataLoader.LoadDataFromCsv(filePath);

            foreach (var part in records)
            {
                var newItem = new ExternalItem
                {
                    Id      = part.RowID.ToString(),
                    Content = new PayloadContent
                    {
                        Value = "<html>" + part.FileName + "</html>"
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType = "grant",
                            Type       = "everyone",
                            Value      = _tenantId
                        }
                    },
                    Properties = part
                };

                try
                {
                    Output.Write(Output.Info, $"Uploading part number {part.RowID}...");
                    await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem);

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    Output.WriteLine(Output.Error, "FAILED");
                    Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.RowID}");
                    Output.WriteLine(Output.Error, serviceException.Message);
                }
            }
        }
Esempio n. 12
0
        private static async Task UpdateItemsFromDatabase(bool uploadModifiedOnly)
        {
            if (_currentConnection == null)
            {
                Output.WriteLine(Output.Warning, "No connection selected. Please create a new connection or select an existing connection.");
                return;
            }

            List <AppliancePart> partsToUpload = null;
            List <AppliancePart> partsToDelete = null;

            var newUploadTime = DateTime.UtcNow;

            using (var db = new ApplianceDbContext())
            {
                if (uploadModifiedOnly)
                {
                    // Load the last upload timestamp
                    var lastUploadTime = GetLastUploadTime();
                    Output.WriteLine(Output.Info, $"Uploading changes since last upload at {lastUploadTime.ToLocalTime().ToString()}");

                    partsToUpload = db.Parts
                                    .Where(p => EF.Property <DateTime>(p, "LastUpdated") > lastUploadTime)
                                    .ToList();

                    partsToDelete = db.Parts
                                    .IgnoreQueryFilters() // Normally items with IsDeleted = 1 aren't included in queries
                                    .Where(p => (EF.Property <bool>(p, "IsDeleted") && EF.Property <DateTime>(p, "LastUpdated") > lastUploadTime))
                                    .ToList();
                }
                else
                {
                    partsToUpload = db.Parts
                                    .ToList();

                    partsToDelete = db.Parts
                                    .IgnoreQueryFilters() // Normally items with IsDeleted = 1 aren't included in queries
                                    .Where(p => EF.Property <bool>(p, "IsDeleted"))
                                    .ToList();
                }
            }

            Output.WriteLine(Output.Info, $"Processing {partsToUpload.Count()} add/updates, {partsToDelete.Count()} deletes");
            bool success = true;

            foreach (var part in partsToUpload)
            {
                var newItem = new ExternalItem
                {
                    Id      = part.PartNumber.ToString(),
                    Content = new ExternalItemContent
                    {
                        // Need to set to null, service returns 400
                        // if @odata.type property is sent
                        ODataType = null,
                        Type      = ExternalItemContentType.Text,
                        Value     = part.Description
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType     = AccessType.Grant,
                            Type           = AclType.Everyone,
                            Value          = _tenantId,
                            IdentitySource = "Azure Active Directory"
                        }
                    },
                    Properties = part.AsExternalItemProperties()
                };

                try
                {
                    Output.Write(Output.Info, $"Uploading part number {part.PartNumber}...");
                    await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem);

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    success = false;
                    Output.WriteLine(Output.Error, "FAILED");
                    Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.PartNumber}");
                    Output.WriteLine(Output.Error, serviceException.Message);
                }
            }

            foreach (var part in partsToDelete)
            {
                try
                {
                    Output.Write(Output.Info, $"Deleting part number {part.PartNumber}...");
                    await _graphHelper.DeleteItem(_currentConnection.Id, part.PartNumber.ToString());

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    if (serviceException.StatusCode.Equals(System.Net.HttpStatusCode.NotFound))
                    {
                        Output.WriteLine(Output.Warning, "Not found");
                    }
                    else
                    {
                        success = false;
                        Output.WriteLine(Output.Error, "FAILED");
                        Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error deleting part {part.PartNumber}");
                        Output.WriteLine(Output.Error, serviceException.Message);
                    }
                }
            }

            // If no errors, update our last upload time
            if (success)
            {
                SaveLastUploadTime(newUploadTime);
            }
        }
Esempio n. 13
0
        private static async Task UpdateItems(bool uploadModifiedOnly)
        {
            if (_currentConnection == null)
            {
                Output.WriteLine(Output.Warning, "No connection selected. Please create a new connection or select an existing connection.");
                return;
            }

            List <AADGroup> groupsToUpload = null;
            List <AADGroup> groupsToDelete = null;

            var newUploadTime = DateTime.UtcNow;

            // Get all AAD Groups
            var groups = await _graphHelper.GetAllGroups();

            if (uploadModifiedOnly)
            {
                // Load the last upload timestamp
                var lastUploadTime = GetLastUploadTime();
                Output.WriteLine(Output.Info, $"Uploading changes since last upload at {lastUploadTime.ToLocalTime().ToString()}");

                groupsToUpload = groups
                                 .Where(p => p.DeletedDateTime == DateTime.MinValue && p.CreatedDateTime > lastUploadTime)
                                 .ToList();

                groupsToDelete = groups
                                 .Where(p => p.DeletedDateTime != DateTime.MinValue && p.DeletedDateTime > lastUploadTime)
                                 .ToList();
            }
            else
            {
                groupsToUpload = groups
                                 .Where(p => p.DeletedDateTime == DateTime.MinValue)
                                 .ToList();

                groupsToDelete = new List <AADGroup>();
            }

            Output.WriteLine(Output.Info, $"Processing {groupsToUpload.Count()} add/updates, {groupsToDelete.Count()} deletes");
            bool success = true;

            foreach (var group in groupsToUpload)
            {
                var newItem = new ExternalItem
                {
                    Id      = group.Id.ToString(),
                    Content = new ExternalItemContent
                    {
                        // Need to set to null, service returns 400
                        // if @odata.type property is sent
                        ODataType = null,
                        Type      = ExternalItemContentType.Text,
                        Value     = group.Description
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType     = AccessType.Grant,
                            Type           = AclType.Group,
                            Value          = group.Id,
                            IdentitySource = "Azure Active Directory"
                        }
                    },
                    Properties = group.AsExternalItemProperties()
                };

                try
                {
                    Output.Write(Output.Info, $"Uploading group {group.DisplayName} ({group.Id})...");
                    await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem);

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    success = false;
                    Output.WriteLine(Output.Error, "FAILED");
                    Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating group {group.Id}");
                    Output.WriteLine(Output.Error, serviceException.Message);
                }
            }

            foreach (var part in groupsToDelete)
            {
                try
                {
                    Output.Write(Output.Info, $"Deleting part number {part.Id}...");
                    await _graphHelper.DeleteItem(_currentConnection.Id, part.Id.ToString());

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    if (serviceException.StatusCode.Equals(System.Net.HttpStatusCode.NotFound))
                    {
                        Output.WriteLine(Output.Warning, "Not found");
                    }
                    else
                    {
                        success = false;
                        Output.WriteLine(Output.Error, "FAILED");
                        Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error deleting part {part.Id}");
                        Output.WriteLine(Output.Error, serviceException.Message);
                    }
                }
            }

            // If no errors, update our last upload time
            if (success)
            {
                SaveLastUploadTime(newUploadTime);
            }
        }
Esempio n. 14
0
        private static async Task UpdateItemsFromDatabase(bool uploadModifiedOnly)
        {
            if (_currentConnection == null)
            {
                Output.WriteLine(Output.Warning, "No connection selected. Please create a new connection or select an existing connection.");
                return;
            }

            List <AppliancePart> partsToUpload = CsvDataLoader.LoadPartsFromCsv("ApplianceParts.csv");
            List <AppliancePart> partsToDelete = new List <AppliancePart>();

            var newUploadTime = DateTime.UtcNow;

            Output.WriteLine(Output.Info, $"Processing {partsToUpload.Count()} add/updates, {partsToDelete.Count()} deletes");
            bool success = true;

            foreach (var part in partsToUpload)
            {
                var newItem = new ExternalItem
                {
                    Id      = part.Id.ToString(),
                    Content = new ExternalItemContent
                    {
                        // Need to set to null, service returns 400
                        // if @odata.type property is sent
                        ODataType = null,
                        Type      = ExternalItemContentType.Text,
                        Value     = part.Text
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType     = AccessType.Grant,
                            Type           = AclType.Everyone,
                            Value          = _tenantId,
                            IdentitySource = "Azure Active Directory"
                        }
                    },
                    Properties = part.AsExternalItemProperties()
                };

                try
                {
                    Output.Write(Output.Info, $"Uploading part number {part.Id}...");
                    await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem);

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    success = false;
                    Output.WriteLine(Output.Error, "FAILED");
                    Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.Id}");
                    Output.WriteLine(Output.Error, serviceException.Message);
                }
            }

            foreach (var part in partsToDelete)
            {
                try
                {
                    Output.Write(Output.Info, $"Deleting part number {part.Id}...");
                    await _graphHelper.DeleteItem(_currentConnection.Id, part.Id.ToString());

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    if (serviceException.StatusCode.Equals(System.Net.HttpStatusCode.NotFound))
                    {
                        Output.WriteLine(Output.Warning, "Not found");
                    }
                    else
                    {
                        success = false;
                        Output.WriteLine(Output.Error, "FAILED");
                        Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error deleting part {part.Id}");
                        Output.WriteLine(Output.Error, serviceException.Message);
                    }
                }
            }

            // If no errors, update our last upload time
            if (success)
            {
                SaveLastUploadTime(newUploadTime);
            }
        }