internal void DeleteTitle(ClientContext cContext, SharePointList spList) { try { LogVerbose("Start DeleteTitle: " + spList.Name); Web wWeb = cContext.Web; List lList = wWeb.Lists.GetByTitle(spList.Name); FieldCollection collField = lList.Fields; Field oneField = collField.GetByInternalNameOrTitle("Title"); oneField.Hidden = true; oneField.Required = false; oneField.SetShowInDisplayForm(false); oneField.SetShowInEditForm(false); oneField.SetShowInNewForm(false); View view = lList.Views.GetByTitle("All Items"); ViewFieldCollection viewFields = view.ViewFields; viewFields.Remove("LinkTitle"); view.Update(); oneField.Update(); cContext.ExecuteQuery(); LogVerbose("Complete DeleteTitle: " + spList.Name); } catch (Exception ex) { LogError(ex, ex.Message); } }
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)); } }