Exemplo n.º 1
0
        // Private Methods (4) 

        private void GetAssociatedItems(List <NavLink> links)
        {
            Guid parentWebId  = Guid.Empty;
            Guid parentListId = Guid.Empty;
            int  parentItemId = 0;

            ParentWebListItemId(ref parentWebId, ref parentListId, ref parentItemId);

            if (parentWebId == Guid.Empty || parentListId == Guid.Empty || parentItemId <= 0)
            {
                return;
            }

            links.Add(new NavLink
            {
                Title = @"Associated Items<span id=""epm-nav-asoitems-info"" class=""icon-info-2"" style=""padding-left: 8px;"" title=""Since this Workspace was created from an Item in the parent site, you have access to these associated parent Lists and Libraries."" data-delay=""100"" data-placement=""right"" data-toggle=""tooltip""></span>",
                Url   = "Header"
            });

            using (var spSite = new SPSite(SiteId, GetUserToken()))
            {
                using (SPWeb spWeb = spSite.OpenWeb(parentWebId))
                {
                    SPList spList = null;

                    try
                    {
                        spList = spWeb.Lists.GetList(parentListId, false);
                    }
                    catch { }

                    if (spList == null)
                    {
                        return;
                    }

                    NavLink navLink = GetLink(spList, parentItemId);

                    if (navLink != null)
                    {
                        links.Add(navLink);

                        links.AddRange(from object associatedList in ListCommands.GetAssociatedLists(spList)
                                       where associatedList != null
                                       select(AssociatedListInfo) associatedList
                                       into listInfo
                                       select GetLink(listInfo, spList, navLink.Title)
                                       into link
                                       where link != null
                                       select link);
                    }
                }
            }
        }
        public void GetAssociatedLists_NotFound_ReturnsArrayList()
        {
            // Arrange
            var list = new ShimSPList()
            {
                IDGet        = () => ListId,
                ParentWebGet = () => new ShimSPWeb()
                {
                    ListsGet = () => new ShimSPListCollection().Bind(
                        new SPList[]
                    {
                        new ShimSPList()
                        {
                            FieldsGet = () => new ShimSPFieldCollection().Bind(
                                new SPField[]
                            {
                                new ShimSPField(
                                    new ShimSPFieldLookup()
                                {
                                    LookupListGet = () => $"{{{ListId}}}"
                                })
                                {
                                    TypeGet         = () => SPFieldType.Lookup,
                                    InternalNameGet = () => DummyInternalName
                                }
                            }),
                            ImageUrlGet = () => DummyImageUrl,
                            IDGet       = () => DummyGuid,
                            TitleGet    = () => DummyTitle
                        }
                    })
                }
            };

            ShimGridGanttSettings.ConstructorSPList = (sender, listParam) => sender.AssociatedItems = true;

            // Act
            var actualResult = ListCommands.GetAssociatedLists(list);

            // Assert
            var associatedListInfo = (AssociatedListInfo)actualResult[0];

            this.ShouldSatisfyAllConditions(
                () => actualResult.Count.ShouldBe(1),
                () => actualResult[0].ShouldBeOfType <AssociatedListInfo>(),
                () => associatedListInfo.Title.ShouldBe(DummyTitle),
                () => associatedListInfo.LinkedField.ShouldBe(DummyInternalName),
                () => associatedListInfo.ListId.ShouldBe(DummyGuid),
                () => associatedListInfo.icon.ShouldBe(DummyImageUrl));
        }
        /// <summary>
        /// Processes all associated lists.
        /// </summary>
        /// <param name="elevatedWeb">The elevated web.</param>
        /// <param name="baseList">The base projects list.</param>
        /// <param name="level">The logging level.</param>
        private void ProcessAssociatedLists(SPWeb elevatedWeb, SPList baseList, int level)
        {
            var associated = ListCommands.GetAssociatedLists(baseList).Cast <AssociatedListInfo>().ToArray();

            if (associated.Length > 0)
            {
                LogInfo($"Updating associated lists for {baseList.Title}.", level);
                foreach (var associatedListInfo in associated)
                {
                    UpdateAssociatedList(elevatedWeb, associatedListInfo, level + 1);
                }
            }
            else
            {
                LogItemSkipped($"Associated lists are not configured for list {baseList.Title}.", level);
            }
        }
        /// <summary>
        /// Updates the project status.
        /// </summary>
        /// <param name="projectListItem">The project list item.</param>
        /// <param name="status">if set to <c>true</c> project will be archived, otherwise restored.</param>
        private void UpdateProjectStatus(SPListItem projectListItem, bool status)
        {
            if (projectListItem == null)
            {
                throw new ArgumentNullException();
            }

            var web = projectListItem.Web;

            web.AllowUnsafeUpdates = true;

            try
            {
                var action = status ? ArchiveProjectAction : RestoreProjectAction;
                LogMessage(web, $"Updating project '{projectListItem.ID}' - Action: '{action}'", LogKind.Info);

                projectListItem[ArchivedColumn] = status;

                if (projectListItem.Fields.ContainsFieldWithInternalName(TimesheetColumn))
                {
                    projectListItem[TimesheetColumn] = !status;
                }

                projectListItem.Update();

                // when associated lists configured - move to archive all of them
                SPSecurity.RunWithElevatedPrivileges(
                    () =>
                {
                    using (var elevatedSite = new SPSite(web.Site.ID))
                    {
                        using (var elevatedWeb = elevatedSite.OpenWeb(web.ID))
                        {
                            elevatedWeb.AllowUnsafeUpdates = true;

                            if (ConnectionProvider.AllowDatabaseConnections(elevatedWeb))
                            {
                                var pfeProjectRepository = new ProjectRepository();
                                pfeProjectRepository.UpdateArchivedStatus(web, projectListItem.ParentList.ID, projectListItem.ID, status);
                            }

                            try
                            {
                                var elevatedProjectList = elevatedWeb.Lists.GetList(
                                    projectListItem.ParentList.ID,
                                    false);
                                var settings = ListCommands.GetGridGanttSettings(elevatedProjectList);
                                if (settings.AssociatedItems)
                                {
                                    var associated = ListCommands.GetAssociatedLists(elevatedProjectList)
                                                     .Cast <AssociatedListInfo>().ToArray();
                                    UpdateAssociatedLists(elevatedWeb, projectListItem.ID, status, associated);
                                }
                            }
                            finally
                            {
                                elevatedWeb.AllowUnsafeUpdates = false;
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                LogError(web, ex);
                throw;
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
            }
        }