Exemple #1
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));
        }
Exemple #2
0
        public ActionResult CreateAuction()
        {
            var model     = new AdvertisementViewModel();
            var 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.Id.ToString(), Text = term.Name
                            };
                            model.CategoryList.Add(newItem);
                        }
                    }
                }
            }
            ModelState.Clear();
            return(View(model));
        }
Exemple #3
0
        public ActionResult Index()
        {
            var model     = new IndexAdvertisementViewModel();
            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            if (spContext != null)
            {
                Session["SpContext"] = spContext;
                Session["SpHostUrl"] = spContext.SPHostUrl;
            }
            else
            {
                spContext = Session["SpContext"] as SharePointContext;
            }
            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    ListCollection  listCol         = clientContext.Web.Lists;
                    TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
                    User            currentUser     = clientContext.Web.CurrentUser;
                    clientContext.Load(taxonomySession);
                    clientContext.Load(listCol, y => y.Where(x => x.Title == "Annonser"));
                    clientContext.Load(currentUser);
                    clientContext.ExecuteQuery();
                    //List list = clientContext.Web.Lists.GetByTitle("Annonser");     Fungerar utan lista??

                    if (taxonomySession != null)
                    {
                        #region Create Category Taxonomy
                        Session["TaxSession"] = taxonomySession;
                        TermStore           termStore    = taxonomySession.GetDefaultSiteCollectionTermStore();
                        TermGroupCollection termGroupCol = termStore.Groups;
                        clientContext.Load(termGroupCol, t => t.Where(y => y.Name == "Advertisements"));
                        clientContext.ExecuteQuery();

                        var termGroup = termGroupCol.FirstOrDefault();
                        if (termGroup == null)
                        {
                            TermGroup annonsKategorierGroup = termStore.CreateGroup("Advertisements", Guid.NewGuid());
                            clientContext.ExecuteQuery();
                            TermSet annonsKateGorierTermSet = annonsKategorierGroup.CreateTermSet("Categories", Guid.NewGuid(), 1033);
                            clientContext.ExecuteQuery();

                            annonsKateGorierTermSet.CreateTerm("Electronics", 1033, Guid.NewGuid());
                            annonsKateGorierTermSet.CreateTerm("Appliances", 1033, Guid.NewGuid());
                            annonsKateGorierTermSet.CreateTerm("Clothing", 1033, Guid.NewGuid());
                            annonsKateGorierTermSet.CreateTerm("Books", 1033, Guid.NewGuid());
                            annonsKateGorierTermSet.CreateTerm("Office", 1033, Guid.NewGuid());
                            annonsKateGorierTermSet.CreateTerm("Other", 1033, Guid.NewGuid());
                            clientContext.ExecuteQuery();
                            termGroup = annonsKategorierGroup;
                        }
                        #endregion

                        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.Id.ToString(), Text = term.Name
                                };
                                model.Categories.Add(newItem);
                            }
                        }
                    }

                    var list = listCol.FirstOrDefault();

                    if (list == null)
                    {
                        #region Create Advertisement List
                        ListCreationInformation listCreationInfo = new ListCreationInformation();
                        listCreationInfo.Title        = "Annonser";
                        listCreationInfo.TemplateType = (int)ListTemplateType.GenericList;

                        var             newList  = clientContext.Web.Lists.Add(listCreationInfo);
                        FieldCollection fieldCol = newList.Fields;


                        Field defaultTitleField = fieldCol.GetByTitle("Title");
                        clientContext.Load(fieldCol);
                        clientContext.Load(defaultTitleField);
                        clientContext.ExecuteQuery();
                        defaultTitleField.Hidden = true;
                        defaultTitleField.SetShowInDisplayForm(false);
                        defaultTitleField.SetShowInEditForm(false);
                        defaultTitleField.SetShowInNewForm(false);
                        defaultTitleField.Required = false;
                        defaultTitleField.Update();

                        Field rubrikField = newList.Fields.AddFieldAsXml("<Field DisplayName='Rubrik' Type='Text' Name='Rubrik' Required='TRUE' />", true, AddFieldOptions.AddFieldToDefaultView);
                        Field textField   = newList.Fields.AddFieldAsXml("<Field DisplayName='Text' Type='Text' Name='Text' Required='TRUE' />", true, AddFieldOptions.AddFieldToDefaultView);
                        Field prisField   = newList.Fields.AddFieldAsXml("<Field DisplayName='Pris' Type='Number' Name='Pris' Required='TRUE' />", true, AddFieldOptions.AddFieldToDefaultView);
                        Field datumField  = newList.Fields.AddFieldAsXml("<Field DisplayName='Datum' Type='DateTime' Name='Datum' Required='TRUE' />", true, AddFieldOptions.AddFieldToDefaultView);
                        //Field anvandareField = newList.Fields.AddFieldAsXml("<Field DisplayName='Användare' Type='User' Name='Anvandare' StaticName='Anvandare' Required='TRUE' />", true, AddFieldOptions.AddFieldToDefaultView);
                        Field kategoriField = newList.Fields.AddFieldAsXml("<Field DisplayName='Kategori' Type='TaxonomyFieldType' Name='Kategori' Required='TRUE' />", true, AddFieldOptions.AddFieldToDefaultView);

                        FieldNumber rubrikFieldNumber = clientContext.CastTo <FieldNumber>(rubrikField);
                        FieldNumber textFieldNumber   = clientContext.CastTo <FieldNumber>(textField);
                        FieldNumber prisFieldNumber   = clientContext.CastTo <FieldNumber>(prisField);
                        FieldNumber datumFieldNumber  = clientContext.CastTo <FieldNumber>(datumField);
                        //FieldNumber anvandareFieldNumber = clientContext.CastTo<FieldNumber>(anvandareField);
                        //FieldNumber kategoryFieldNumber = clientContext.CastTo<FieldNumber>(anvandareField);
                        Guid termStoreId = Guid.Empty;
                        Guid termSetId   = Guid.Empty;
                        GetTaxonomyFieldInfo(clientContext, out termStoreId, out termSetId, "Categories");
                        TaxonomyField kategoryFieldNumber = clientContext.CastTo <TaxonomyField>(kategoriField);
                        kategoryFieldNumber.SspId          = termStoreId;
                        kategoryFieldNumber.TermSetId      = termSetId;
                        kategoryFieldNumber.TargetTemplate = String.Empty;
                        kategoryFieldNumber.AnchorId       = Guid.Empty;

                        rubrikFieldNumber.Update();
                        textFieldNumber.Update();
                        prisFieldNumber.Update();
                        datumFieldNumber.Update();
                        //anvandareFieldNumber.Update();
                        kategoryFieldNumber.Update();

                        View view = newList.Views.GetByTitle("All Items");
                        clientContext.Load(view);
                        clientContext.ExecuteQuery();
                        ViewFieldCollection viewFields = view.ViewFields;
                        viewFields.Remove("LinkTitle");
                        view.Update();

                        clientContext.ExecuteQuery();

                        list = newList;
                        #endregion
                    }
                    CamlQuery cQuery = new CamlQuery();
                    cQuery.ViewXml = @"<View>
                                        <Query>
                                        <Where>
                                        <Eq>
                                        <FieldRef Name='Author' LookupId='True'/>
                                        <Value Type='Lookup'>" + currentUser.Id + @"</Value>
                                        </Eq>
                                        </Where>
                                        </Query>
                                        </View>";
                    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.Advertisements.Add(tempObj);
                    }
                }
                return(View(model));
            }
        }