Ejemplo n.º 1
0
        //Improvement
        public List <SPListItem> MissingListItemsV1()
        {
            var results = new List <SPListItem>();

            foreach (var list in this.GetExistingLists)
            {
                try
                {
                    //get source list items
                    IEnumerable <SPListItem> sourceListItems = null;
                    if (SourceSiteCreds.SiteType == SiteType.WSS)
                    {
                        sourceListItems = this.SharePointRepository2007.GetListItems(list);
                    }
                    else
                    {
                        sourceListItems = this.SharePointRepository.GetListItems(SourceClientContext, list);
                    }

                    //get target list items
                    List <SPListItem> targetListItems = this.SharePointRepository.GetListItems(TargetClientContext, list);

                    //key is hashcode, while value is the actual list item
                    Dictionary <int, SPListItem> hashedListItems = new Dictionary <int, SPListItem>();

                    bool useDefaultHashCode = false;
                    //go retrieve all target list items and add it to the dictionary
                    try
                    {
                        foreach (var listItem in targetListItems)
                        {
                            hashedListItems.Add(listItem.GetListItemHashCode(), listItem);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message == "An item with the same key has already been added.")
                        {
                            //then we have a problem
                            useDefaultHashCode = true;
                            hashedListItems    = new Dictionary <int, SPListItem>();
                            foreach (var listItem in targetListItems)
                            {
                                hashedListItems.Add(listItem.GetListItemDefaultHashCode(), listItem);
                            }
                        }
                    }
                    //get all list items, transform it and then compare key.
                    foreach (var listItem in sourceListItems)
                    {
                        //string manipulation - convert source list items to its target list equivalent items and store it in a temp object
                        SPListItem transformSourceListItem = new SPListItem()
                        {
                            FileDirRef    = ConvertToTargetUrl(listItem.FileDirRef),
                            FileRef       = ConvertToTargetUrl(listItem.FileRef),
                            ID            = listItem.ID,
                            Title         = listItem.Title,
                            ModifiedDate  = listItem.ModifiedDate,
                            ListBaseType  = listItem.ListBaseType,
                            EncodedAbsUrl = listItem.EncodedAbsUrl,
                            Name          = listItem.Name
                        };
                        //HashSet comparision - faster than List<> -- comparision based on the hashcode
                        int targetKeyHashCode = (useDefaultHashCode) ? transformSourceListItem.GetListItemDefaultHashCode() : transformSourceListItem.GetListItemHashCode();
                        if (!hashedListItems.ContainsKey(targetKeyHashCode))
                        {
                            logger.Log(LogLevel.Info, $"Missing ListItem:{listItem.FileRef}");
                            results.Add(listItem);
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Log(LogLevel.Error, ex);
                }
            }
            return(results);
        }