public bool Validate(TermGroupCollection sourceCollection, TermGroupCollection targetCollection, TokenParser tokenParser) { // Convert object collections to XML List<SerializedTermGroupInstance> sourceTermGroups = new List<SerializedTermGroupInstance>(); List<SerializedTermGroupInstance> targetTermGroups = new List<SerializedTermGroupInstance>(); foreach (TermGroup termGroup in sourceCollection) { ProvisioningTemplate pt = new ProvisioningTemplate(); pt.TermGroups.Add(termGroup); sourceTermGroups.Add(new SerializedTermGroupInstance() { SchemaXml = ExtractElementXml(pt) }); } foreach (TermGroup termGroup in targetCollection) { ProvisioningTemplate pt = new ProvisioningTemplate(); pt.TermGroups.Add(termGroup); targetTermGroups.Add(new SerializedTermGroupInstance() { SchemaXml = ExtractElementXml(pt) }); } // Use XML validation logic to compare source and target Dictionary<string, string[]> parserSettings = new Dictionary<string, string[]>(); parserSettings.Add("SchemaXml", null); bool isTermGroupsMatch = ValidateObjectsXML(sourceTermGroups, targetTermGroups, "SchemaXml", new List<string> { "Name" }, tokenParser, parserSettings); Console.WriteLine("-- Term group validation " + isTermGroupsMatch); return isTermGroupsMatch; }
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)); }
public bool Validate(TermGroupCollection sourceCollection, TermGroupCollection targetCollection, TokenParser tokenParser) { // Convert object collections to XML List <SerializedTermGroupInstance> sourceTermGroups = new List <SerializedTermGroupInstance>(); List <SerializedTermGroupInstance> targetTermGroups = new List <SerializedTermGroupInstance>(); foreach (TermGroup termGroup in sourceCollection) { ProvisioningTemplate pt = new ProvisioningTemplate(); pt.TermGroups.Add(termGroup); sourceTermGroups.Add(new SerializedTermGroupInstance() { SchemaXml = ExtractElementXml(pt) }); } foreach (TermGroup termGroup in targetCollection) { ProvisioningTemplate pt = new ProvisioningTemplate(); pt.TermGroups.Add(termGroup); targetTermGroups.Add(new SerializedTermGroupInstance() { SchemaXml = ExtractElementXml(pt) }); } // Use XML validation logic to compare source and target Dictionary <string, string[]> parserSettings = new Dictionary <string, string[]> { { "SchemaXml", null } }; bool isTermGroupsMatch = ValidateObjectsXML(sourceTermGroups, targetTermGroups, "SchemaXml", new List <string> { "Name" }, tokenParser, parserSettings); Console.WriteLine("-- Term group validation " + isTermGroupsMatch); return(isTermGroupsMatch); }
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)); }
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)); } }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); log.Info(ConfigurationManager.AppSettings["clientID"]); string authenticationToken = await helper.getSharePointToken(log, ConfigurationManager.AppSettings["tenantURL"]); try { using (var clientContext = helper.GetClientContext(ConfigurationManager.AppSettings["tenantURL"], authenticationToken)) { TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext); //taxonomySession.UpdateCache(); TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); clientContext.Load(termStore, store => store.Name, store => store.Groups.Where(g => g.IsSystemGroup == false && g.IsSiteCollectionGroup == false).Include(group => group.Id, group => group.Name, group => group.Description, group => group.IsSiteCollectionGroup, group => group.IsSystemGroup, group => group.TermSets.Include(termSet => termSet.Id, termSet => termSet.Name, termSet => termSet.Description, termSet => termSet.CustomProperties, termSet => termSet.Terms.Include(t => t.Id, t => t.Description, t => t.Name, t => t.IsDeprecated, t => t.Parent, t => t.Labels, t => t.LocalCustomProperties, t => t.IsSourceTerm, t => t.IsRoot, t => t.IsKeyword, t => t.TermsCount, t => t.CustomProperties, t => t.Terms.Include(t1 => t1.Id, t1 => t1.Description, t1 => t1.Name, t1 => t1.IsDeprecated, t1 => t1.Parent, t1 => t1.Labels, t1 => t1.LocalCustomProperties, t1 => t1.IsSourceTerm, t1 => t1.IsRoot, t1 => t1.IsKeyword, t1 => t1.TermsCount, t1 => t1.CustomProperties, t1 => t1.Terms.Include(t2 => t2.Id, t2 => t2.Description, t2 => t2.Name, t2 => t2.IsDeprecated, t2 => t2.Parent, t2 => t2.Labels, t2 => t2.LocalCustomProperties, t2 => t2.IsSourceTerm, t2 => t2.IsRoot, t2 => t2.IsKeyword, t2 => t2.TermsCount, t2 => t2.CustomProperties, t2 => t2.Terms.Include(t3 => t3.Id, t3 => t3.Description, t3 => t3.Name, t3 => t3.IsDeprecated, t3 => t3.Parent, t3 => t3.Labels, t3 => t3.LocalCustomProperties, t3 => t3.IsSourceTerm, t3 => t3.IsRoot, t3 => t3.IsKeyword, t3 => t3.TermsCount, t3 => t3.CustomProperties, t3 => t3.Terms.Include(t4 => t4.Id, t4 => t4.Description, t4 => t4.Name, t4 => t4.IsDeprecated, t4 => t4.Parent, t4 => t4.Labels, t4 => t4.LocalCustomProperties, t4 => t4.IsSourceTerm, t4 => t4.IsRoot, t4 => t4.IsKeyword, t4 => t4.TermsCount, t4 => t4.CustomProperties)))) )))); clientContext.ExecuteQuery(); ITATermStore itaTermStore = new ITATermStore(); itaTermStore.ITATermGroupList = new List <ITATermGroup>(); TermGroupCollection allGroups = termStore.Groups; log.Info("Total Groups " + allGroups.Count.ToString()); foreach (var termGroup in allGroups) { log.Info("Group Name " + termGroup.Name); ITATermGroup grp = new ITATermGroup(); grp.Id = termGroup.Id; grp.Name = termGroup.Name != null ? termGroup.Name : ""; //TermGroup specific logic goes here grp.TermSets = new List <ITATermSets>(); foreach (var termSet in termGroup.TermSets) { log.Info("Termset Name : " + termSet.Name); ITATermSets itaTermSet = new ITATermSets(); itaTermSet.Id = termSet.Id != null ? termSet.Id : new Guid(); itaTermSet.Name = termSet.Name != null ? termSet.Name : ""; itaTermSet.Type = termSet.GetType().ToString(); if (termSet.CustomProperties != null) { itaTermSet.CustomProps = new List <ITATermProperty>(); foreach (var customProps in termSet.CustomProperties) { itaTermSet.CustomProps.Add(new ITATermProperty() { key = customProps.Key, value = customProps.Value }); } } itaTermSet.Terms = new List <ITATerms>(); //TermSet specific logic goes here foreach (var term in termSet.Terms) { log.Info("Term Name " + term.Name); ITATerms itaTerm = new ITATerms(); itaTerm.Name = term.Name != null ? term.Name : ""; itaTerm.Id = term.Id != null ? term.Id : new Guid(); //Microsoft.SharePoint.Client.Publishing.Navigation.NavigationTerm abc = term; itaTerm.Type = term.GetType().ToString(); if (term.Labels.Count > 0) { itaTerm.Labels = new List <string>(); foreach (var label in term.Labels) { itaTerm.Labels.Add(label.Value); } } if (term.CustomProperties.Count > 0) { itaTerm.CustomProps = new List <ITATermProperty>(); foreach (var prop in term.CustomProperties) { itaTerm.CustomProps.Add(new ITATermProperty() { key = prop.Key, value = prop.Value }); } } getSubTerms(term, itaTerm, log); itaTermSet.Terms.Add(itaTerm); } grp.TermSets.Add(itaTermSet); } itaTermStore.ITATermGroupList.Add(grp); } string output = JsonConvert.SerializeObject(itaTermStore); var response = req.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(output, Encoding.UTF8, "application/json"); return(response); } } catch (Exception ex) { return(req.CreateResponse(HttpStatusCode.OK, "Errorx : " + ex.Message)); } }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Enter your user name"); // Console.ForegroundColor = defaultForeground; string userName = Console.ReadLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Enter your password."); // Console.ForegroundColor = defaultForeground; SecureString password = GetPasswordFromConsoleInput(); using (context = new ClientContext("https://microsoft.sharepoint.com/teams/USDXISVCJ/")) { context.Credentials = new SharePointOnlineCredentials(userName, password); Web currentWeb = context.Web; context.Load(currentWeb); context.ExecuteQuery(); TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(currentWeb.Context); TermStoreCollection termStores = taxonomySession.TermStores; currentWeb.Context.Load(termStores); currentWeb.Context.ExecuteQuery(); TermStore termStore = termStores[0]; currentWeb.Context.Load(termStore); currentWeb.Context.ExecuteQuery(); TermGroupCollection termGroups = termStore.Groups; currentWeb.Context.Load(termGroups); currentWeb.Context.ExecuteQuery(); TermGroup group = termGroups.GetByName("Site Collection - microsoft.sharepoint.com-teams-USDXISVCJ"); TermSetCollection termSets = group.TermSets; currentWeb.Context.Load(termSets); currentWeb.Context.ExecuteQuery(); TermCollection terms = termSets.GetByName("Workload").Terms; currentWeb.Context.Load(terms); currentWeb.Context.ExecuteQuery(); Console.WriteLine(terms.Count); foreach (Term term in terms) { Console.WriteLine(term.Name); } } }