Exemplo n.º 1
0
        public PartialViewResult SearchAuctions(SearchAdvertisementViewModel searchData)
        {
            var model = new List <AdvertisementViewModel>();
            SharePointContext spContext = Session["SpContext"] as SharePointContext;

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                ListCollection listCol     = clientContext.Web.Lists;
                User           currentUser = clientContext.Web.CurrentUser;
                clientContext.Load(listCol, y => y.Where(x => x.Title == "Annonser"));
                clientContext.Load(currentUser);
                clientContext.ExecuteQuery();

                var list = listCol.FirstOrDefault();

                if (list != null)
                {
                    CamlQuery cQuery = new CamlQuery();

                    cQuery.ViewXml = CamlSearchQueryBuilder(searchData.SearchText, searchData.SelectedCategory);
                    var listItems = list.GetItems(cQuery);
                    clientContext.Load(listItems);
                    clientContext.ExecuteQuery();

                    foreach (ListItem listItem in listItems)
                    {
                        AdvertisementViewModel tempObj = new AdvertisementViewModel
                        {
                            Title      = listItem["Rubrik"].ToString(),
                            Text       = listItem["Text"].ToString(),
                            Price      = int.Parse(listItem["Pris"].ToString()),
                            Date       = DateTime.Parse(listItem["Datum"].ToString()),
                            User       = listItem["Author"] as FieldUserValue,
                            Category   = listItem["Kategori"] as TaxonomyFieldValue,
                            ListItemId = listItem["ID"].ToString()
                        };
                        model.Add(tempObj);
                    }
                }
            }
            if (searchData.SelectedFilter == "PriceAsc")
            {
                model.OrderBy(x => x.Price);
            }
            else if (searchData.SelectedFilter == "PriceDesc")
            {
                model.OrderByDescending(x => x.Price);
            }
            else if (searchData.SelectedFilter == "DateAsc")
            {
                model.OrderBy(x => x.Date);
            }
            else if (searchData.SelectedFilter == "DateDesc")
            {
                model.OrderByDescending(x => x.Date);
            }
            return(PartialView("_AuctionSearchPartial", model));
        }
Exemplo n.º 2
0
        public ActionResult BuyAuctions()
        {
            var model = new SearchAdvertisementViewModel();

            model.FilterList.Add(new SelectListItem {
                Value = "PriceAsc", Text = "Pris (Stigande)"
            });
            model.FilterList.Add(new SelectListItem {
                Value = "PriceDesc", Text = "Pris (Fallande)"
            });
            model.FilterList.Add(new SelectListItem {
                Value = "DateAsc", Text = "Datum (Stigande)"
            });
            model.FilterList.Add(new SelectListItem {
                Value = "DateDesc", Text = "Datum (Fallande)"
            });


            SharePointContext spContext = Session["SpContext"] as SharePointContext;

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);

                if (taxonomySession != null)
                {
                    TermStore           termStore    = taxonomySession.GetDefaultSiteCollectionTermStore();
                    TermGroupCollection termGroupCol = termStore.Groups;
                    clientContext.Load(termGroupCol, t => t.Where(y => y.Name == "Advertisements"));
                    clientContext.ExecuteQuery();

                    TermGroup termGroup = termGroupCol.FirstOrDefault();
                    if (termGroup != null)
                    {
                        TermSet        termSet = termGroup.TermSets.GetByName("Categories");
                        TermCollection terms   = termSet.GetAllTerms();
                        clientContext.Load(termSet);
                        clientContext.Load(terms);
                        clientContext.ExecuteQuery();

                        foreach (Term term in terms)
                        {
                            SelectListItem newItem = new SelectListItem {
                                Value = term.Name, Text = term.Name
                            };
                            model.CategoryList.Add(newItem);
                        }
                    }
                }
            }

            model.CategoryList.OrderBy(x => x.Text);
            model.CategoryList.Insert(0, new SelectListItem {
                Value = "Alla", Text = "Alla"
            });
            return(View(model));
        }