Example #1
0
        public static void ImportDocument(ClientContext ctx)
        {
            string    pathToXML = AppDomain.CurrentDomain.BaseDirectory + "ImportantIssueImport.xml";
            XDocument doc       = XDocument.Load(pathToXML);

            List <XElement> items = doc.Root.Elements("item").ToList();

            List          importantIssueList = ctx.Web.GetListByUrl("lists/importantissues");
            TaxonomyField issueCatTaxField   = importantIssueList.GetFieldById <TaxonomyField>("{ED88551E-8F2D-4A2E-8DB0-3D9616C012B3}".ToGuid());


            foreach (XElement item in items)
            {
                string title         = item.Element("Title").Value;
                string date          = item.Element("Date").Value;
                string responsible   = item.Element("Responsible").Value;
                string relatedIssue  = item.Element("RelatedIssue").Value;
                string issueCategory = item.Element("IssueCategory").Value;
                string notes         = item.Element("Notes").Value;

                User userResponsible = ctx.Web.EnsureUser(responsible);
                ctx.Load(userResponsible, u => u.Id);
                ctx.ExecuteQuery();

                int? relatedIssueId = getRelatedIssues(ctx, relatedIssue, importantIssueList);
                Term termIssueCat   = GetTermByName(ctx, issueCategory);

                ListItem newItem = importantIssueList.AddItem(new ListItemCreationInformation());

                newItem["Title"]           = title;
                newItem["OD1_IssueDate"]   = DateTime.Parse(date);
                newItem["OD1_Responsible"] = userResponsible.Id;
                if (relatedIssueId != null)
                {
                    // newItem["OD1_RelatedIssue"] = relatedIssueId.Value;
                }

                issueCatTaxField.SetFieldValueByTerm(newItem, termIssueCat, 1033);

                newItem["OD1_Notes"] = notes;

                newItem.Update();
                ctx.ExecuteQuery();

                // pnp does an execute query. screw up if above newitem.update()
                //if (termIssueCat != null)
                //{
                //    newItem.SetTaxonomyFieldValue("{ED88551E-8F2D-4A2E-8DB0-3D9616C012B3}".ToGuid(), termIssueCat.Name, termIssueCat.Id);
                //}
            }
        }
Example #2
0
        internal static void SaveStudent(ClientContext ctx, Student student)
        {
            TermStore store = ctx.Site.GetDefaultKeywordsTermStore();
            Term      term  = store.GetTerm(student.FavColorid.ToGuid());

            ctx.Load(term);
            ctx.ExecuteQuery();

            List          list  = ctx.Web.GetListByTitle("Students");
            TaxonomyField field = list.GetFieldById <TaxonomyField>("{6D9CF114-04FB-4C91-BF6E-C45770B48A2A}".ToGuid());
            ListItem      item  = list.AddItem(new ListItemCreationInformation());

            //item.SetTaxonomyFieldValue("{6D9CF114-04FB-4C91-BF6E-C45770B48A2A}".ToGuid(), term.Name, term.Id);
            item["Title"]          = student.Title;
            item["School_Address"] = student.Address;
            item["School_School"]  = student.SchoolId;
            field.SetFieldValueByTerm(item, term, 1033);
            item.Update();
            ctx.ExecuteQuery();
        }
        /// <summary>
        /// Helper Method to set a Taxonomy Field on a list item
        /// </summary>
        /// <param name="ctx">The Authenticated ClientContext</param>
        /// <param name="listItem">The listitem to modify</param>
        /// <param name="model">Domain Object of key/value pairs of the taxonomy field & value</param>
        public static void SetTaxonomyFields(ClientContext ctx, ListItem listItem, string FileContent, string ListId, string url)
        {
            FieldCollection _fields = listItem.ParentList.Fields;

            ctx.Load(ctx.Web.AllProperties);
            ctx.Load(_fields);
            ctx.ExecuteQuery();

            AppWebHelper         hlp      = new AppWebHelper(url, false);
            List <GlobalSetting> settings = GetGlobalConfig(hlp);

            LogHelper.Log(settings.Count.ToString());
            var enabled = settings.Where(s => s.key == Constants.EnableKeywordCreation).SingleOrDefault();

            bool KeywordCreationEnabled = Convert.ToBoolean(
                settings.Where(s => s.key == Constants.EnableKeywordCreation).SingleOrDefault().value);
            int KeywordRecognitionTreshold = Convert.ToInt32(
                settings.Where(s => s.key == Constants.KeywordRecognitionTreshold).SingleOrDefault().value);
            int KeywordCreationTreshold = Convert.ToInt32(
                settings.Where(s => s.key == Constants.KeywordCreationTreshold).SingleOrDefault().value);

            List <string> ConfiguredFields = hlp.ListTaxFields(ListId);

            foreach (var _f in _fields)
            {
                if (ConfiguredFields.Contains(_f.Id.ToString()))
                {
                    TaxonomyField _field = ctx.CastTo <TaxonomyField>(_fields.GetById(_f.Id));
                    if (_f.InternalName != Constants.TaxFieldInternalName)
                    {
                        ctx.Load(_field);
                        ctx.ExecuteQuery();
                        Collection <Term> MatchingTerms = null;
                        MatchingTerms = AutoTaggingHelper.MatchingTerms(FileContent, ctx, _field.TermSetId, _field.AnchorId);

                        if (MatchingTerms.Count > 0)
                        {
                            LogHelper.Log("Updating taxfield " + _field.Title);
                            if (_field.AllowMultipleValues)
                            {
                                _field.SetFieldValueByCollection(listItem, MatchingTerms, 1033);
                            }
                            else
                            {
                                _field.SetFieldValueByTerm(listItem, MatchingTerms.First(), 1033);
                            }

                            listItem.Update();
                            ctx.ExecuteQuery();
                        }
                    }
                    else
                    {
                        TaxonomyTerms tt           = new TaxonomyTerms(ctx);
                        string        TextLanguage =
                            AutoTaggingHelper.LanguageIdentifier.Identify(FileContent).FirstOrDefault().Item1.Iso639_3;
                        StringBuilder            EntKeyWordsValue = new StringBuilder();
                        Dictionary <string, int> tokens           =
                            Tokenize(FileContent,
                                     KeywordRecognitionTreshold,
                                     TextLanguage);
                        StringBuilder TokenString = new StringBuilder();
                        foreach (KeyValuePair <string, int> token in tokens)
                        {
                            Guid KeywordId = TaxonomyTerms.GetKeyword(token.Key);
                            TokenString.AppendFormat("{0}|", token.Key);
                            if (KeywordId != Guid.Empty)
                            {
                                EntKeyWordsValue.AppendFormat("-1;#{0}|{1};", token.Key, KeywordId);
                            }
                            else
                            {
                                if (KeywordCreationEnabled && token.Value >= KeywordCreationTreshold &&
                                    !AutoTaggingHelper.IsEmptyWord(token.Key.ToLowerInvariant(), TextLanguage, hlp))
                                {
                                    Guid g = AddKeyWord(token.Key, ctx);
                                    if (g != Guid.Empty)
                                    {
                                        EntKeyWordsValue.AppendFormat("-1;#{0}|{1};", token.Key, g);
                                    }
                                }
                            }
                        }
                        LogHelper.Log(TokenString.ToString());
                        if (EntKeyWordsValue.ToString().Length > 0)
                        {
                            LogHelper.Log("keyword value " + EntKeyWordsValue.ToString(), LogSeverity.Error);

                            TaxonomyFieldValueCollection col = new TaxonomyFieldValueCollection(ctx, string.Empty, _field);
                            col.PopulateFromLabelGuidPairs(EntKeyWordsValue.ToString());
                            _field.SetFieldValueByValueCollection(listItem, col);
                            listItem.Update();
                            ctx.ExecuteQuery();
                        }
                    }
                }
            }
        }