Example #1
0
        private void MatchTaxonomyField(SPListItem targetSPListItem, SPField targetSPField, string sourceValue)
        {
            // this is a managed metadata field
            TaxonomyField   managedField = targetSPField as TaxonomyField;
            TaxonomySession session      = new TaxonomySession(targetSPListItem.Web.Site);
            TermStore       termStore    = session.TermStores[managedField.SspId];
            TermSet         termSet      = termStore.GetTermSet(managedField.TermSetId);
            int             lcid         = CultureInfo.CurrentCulture.LCID;

            // TODO: this is Classification code; to be replaced with the one below!
            Term myTerm = termSet.GetTerms(this.SubstringBefore(sourceValue, "|"), false).FirstOrDefault();

            if (myTerm != null)
            {
                string termString =
                    string.Concat(myTerm.GetDefaultLabel(lcid), TaxonomyField.TaxonomyGuidLabelDelimiter, myTerm.Id);
                int[] ids =
                    TaxonomyField.GetWssIdsOfTerm(targetSPListItem.Web.Site, termStore.Id, termSet.Id, myTerm.Id, true, 1);

                // set the WssId (TaxonomyHiddenList ID) to -1 so that it is added to the TaxonomyHiddenList
                if (ids.Length == 0)
                {
                    termString = "-1;#" + termString;
                }
                else
                {
                    termString = ids[0] + ";#" + termString;
                }

                targetSPListItem[targetSPField.Id] = termString;
            }
        }
Example #2
0
        /// <summary>Fixes the SP field taxonomy.</summary>
        /// <param name="contextSpSite">The context sp site.</param>
        /// <param name="fileSPListItem">The file sp list item.</param>
        /// <param name="sourceValue">The source value.</param>
        /// <returns>The fix sp field taxonomy.</returns>
        private string FixSPFieldTaxonomy(SPSite contextSpSite, SPListItem fileSPListItem, string sourceValue)
        {
            string s    = "\t\tTaxonomy\n\t\t-" + contextSpSite.Url + ",\n\t\t-" + sourceValue + "\n";
            int    lcid = CultureInfo.CurrentCulture.LCID;

            // this is a managed metadata field
            TaxonomyField   taxonomyField   = this.ContextSPField as TaxonomyField;
            TaxonomySession taxonomySession = new TaxonomySession(contextSpSite);
            TermStore       termStore       = taxonomySession.TermStores[taxonomyField.SspId];
            TermSet         termSet         = termStore.GetTermSet(taxonomyField.TermSetId);

            s += "\t\tlcid:\t\t" + lcid + "\n\t\ttaxField:\t" + taxonomyField.Title + "\n";
            s += "\t\ttermstores:\t" + taxonomySession.TermStores.Count + "\n";
            s += "\t\ttermstore:\t" + termStore.Name + "\n\t\ttermset:\t" + termSet.Name + "\n";

            // this is Classification code; to be replaced with the one below!
            Term myTerm = termSet.GetTerms(this.SubStringBefore(sourceValue, "|"), false).FirstOrDefault();

            if (myTerm != null)
            {
                s += "\t\tmyTerm:\t\t" + myTerm.Name + "\n";
                s += "\t\twas:" + fileSPListItem[this.ContextSPField.Id] + "\n";
                string termString = String.Concat(myTerm.GetDefaultLabel(lcid),
                                                  TaxonomyField.TaxonomyGuidLabelDelimiter,
                                                  myTerm.Id);
                int[] ids = TaxonomyField.GetWssIdsOfTerm(
                    contextSpSite,
                    termStore.Id,
                    termSet.Id,
                    myTerm.Id,
                    true,
                    1);
                s += "\t\ttermString:\t" + termString + "\n\t\tids:\t\t" + ids.Length + "\n";

                // set the WssId (TaxonomyHiddenList ID) to -1 so that it is added to the TaxonomyHiddenList
                // fileSPListItem[this.ContextSPField.Id] = (ids.Length == 0) ? "-1;#" + termString : ids[0] + ";#" + termString;
                if (ids.Length == 0)
                {
                    termString = "-1;#" + termString;
                }
                else
                {
                    termString = ids[0] + ";#" + termString;
                }

                fileSPListItem[this.ContextSPField.Id] = termString;
            }

            s += "\t\tis_:" + fileSPListItem[this.ContextSPField.Id] + "\n";
            return(s);
        }
Example #3
0
        public static TaxonomyFieldValue GetTaxonomyFieldValue(SPSite site, TaxonomyField field, string value, out List <int> wssIds)
        {
            TaxonomySession taxonomySession = new TaxonomySession(site);
            TermStore       termStore       = taxonomySession.TermStores[field.SspId];
            TermSet         termSet         = termStore.GetTermSet(field.TermSetId);

            wssIds = new List <int>();
            TaxonomyFieldValue taxonomyFieldValue = TaxonomyFieldControl.GetTaxonomyValue(value);

            wssIds.AddRange(TaxonomyField.GetWssIdsOfTerm(site, termStore.Id, termSet.Id,
                                                          new Guid(taxonomyFieldValue.TermGuid), true, 100));

            return(taxonomyFieldValue);
        }
Example #4
0
 private string GetAllWssIdByTerm(SPList list, Term term, bool includeDescendants)
 {
     if (term != null)
     {
         // Get the lookup Ids of all taxonomy field values that point to this term or its decendants in the taxonomy hidden list
         var wssIds = TaxonomyField.GetWssIdsOfTerm(list.ParentWeb.Site, term.TermStore.Id, term.TermSet.Id, term.Id, includeDescendants, int.MaxValue);
         if (wssIds.Count() > 0)
         {
             // Filter over the taxonomy field with the proper SID lookup id to the taxonomy hidden list
             return(string.Join(string.Empty, wssIds.Select(wssId => "<Value Type=\"Integer\">" + wssId + "</Value>").ToArray()));
         }
         else
         {
             this._logger.Warn("Failed to find any item in the site collection that matches the term '" + term.Name + "'");
             throw new ArgumentException("No usage found for term with id " + term.Id);
         }
     }
     else
     {
         throw new ArgumentNullException("term");
     }
 }
Example #5
0
        public static TaxonomyFieldValueCollection GetTaxonomyFieldValues(SPSite site, TaxonomyField field, string values, out List <int> wssIds)
        {
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            TaxonomySession taxonomySession = new TaxonomySession(site);
            TermStore       termStore       = taxonomySession.TermStores[field.SspId];
            TermSet         termSet         = termStore.GetTermSet(field.TermSetId);

            TaxonomyFieldValueCollection termValues = new TaxonomyFieldValueCollection(field);

            termValues.AddRange(TaxonomyFieldControl.GetTaxonomyCollection(values));

            wssIds = new List <int>();
            foreach (TaxonomyFieldValue termValue in termValues)
            {
                wssIds.AddRange(TaxonomyField.GetWssIdsOfTerm(site, termStore.Id, termSet.Id,
                                                              new Guid(termValue.TermGuid), false, 500));
            }

            return(termValues);
        }
Example #6
0
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string validationReason;

            //if (taxJudge.Validate(out validationReason) && taxIssue.Validate(out validationReason) && taxJudge.Text != string.Empty && taxIssue.Text != string.Empty)
            if (lawIssue.Validate(out validationReason) && lawIssue.Text != string.Empty)
            {
                Microsoft.SharePoint.Taxonomy.TaxonomySession taxSession   = new Microsoft.SharePoint.Taxonomy.TaxonomySession(SPContext.Current.Web.Site);
                Microsoft.SharePoint.Taxonomy.TermStore       taxStore     = taxSession.TermStores[0];
                Microsoft.SharePoint.Taxonomy.Group           taxGroup     = GetTermStoreGroup(taxStore, ECASE_TERMS_GROUP_NAME);
                Microsoft.SharePoint.Taxonomy.TermSet         issueTermSet = null;
                Microsoft.SharePoint.Taxonomy.TermSet         judgeTermSet = null;

                //we need to attach our managed metadata UI controls to the term sets...
                IEnumerator <Microsoft.SharePoint.Taxonomy.TermSet> termsets = taxGroup.TermSets.GetEnumerator();
                while (termsets.MoveNext())
                {
                    Microsoft.SharePoint.Taxonomy.TermSet curTermset = termsets.Current;

                    if (curTermset.Name == ECASE_TERMS_LIL_TERMSET_NAME)
                    {
                        issueTermSet = curTermset;
                    }

                    if (curTermset.Name == ECASE_TERMS_JUDGE_TERMSET_NAME)
                    {
                        judgeTermSet = curTermset;
                    }
                }
                pnlResults.Visible = true;
                //Guid judgeTermId = new Guid(taxJudge.Text.Split('|')[1]);
                //int[] judgeIntCollection = TaxonomyField.GetWssIdsOfTerm(SPContext.Current.Web.Site, taxStore.Id,
                //                                                         judgeTermSet.Id, judgeTermId, false, 50);

                Guid  issueTermId        = new Guid(lawIssue.Text.Split('|')[1]);
                int[] issueIntCollection = TaxonomyField.GetWssIdsOfTerm(SPContext.Current.Web.Site, taxStore.Id,
                                                                         issueTermSet.Id, issueTermId, false, 50);

                //String CAML_QUERY =
                //    @"<Where><And><In><FieldRef LookupId=""True"" Name=""Judge"" /><Values>{0}</Values></In><In><FieldRef LookupId=""True"" Name=""UniformIssueList"" /><Values>{1}</Values></In></And></Where>";
                //String VALUES = @"<Value Type=""Integer"">{0}</Value>";

                String CAML_QUERY =
                    @"<Where><In><FieldRef LookupId=""True"" Name=""LawIssueList"" /><Values>{0}</Values></In></Where>";
                String VALUES = @"<Value Type=""Integer"">{0}</Value>";

                //StringBuilder strJudgeValues = new StringBuilder();
                StringBuilder strIssueValues = new StringBuilder();

                //foreach (int judgeInt in judgeIntCollection)
                //{
                //    strJudgeValues.AppendLine(String.Format(VALUES, judgeInt));
                //}

                foreach (int issueInt in issueIntCollection)
                {
                    strIssueValues.AppendLine(String.Format(VALUES, issueInt));
                }


                //String strQuery = String.Format(CAML_QUERY, strJudgeValues.ToString(), strIssueValues.ToString());
                String strQuery = String.Format(CAML_QUERY, strIssueValues.ToString());
                litIssue.Text = lawIssue.Text.Split('|')[0];
                //litJudge.Text = taxJudge.Text.Split('|')[0];

                //if (strIssueValues.Length == 0 || strJudgeValues.Length == 0)
                if (strIssueValues.Length == 0)
                {
                    litOwners.Text = "<b>There are no results</b>";
                }
                else
                {
                    SPQuery sPQuery = new SPQuery();
                    sPQuery.Query = strQuery;

                    SPListItemCollection results = null;
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (
                            SPSite site =
                                new SPSite(SPContext.Current.Web.Site.ID))
                        {
                            SPWeb web        = site.RootWeb;
                            SPList casesList = web.Lists["Cases"];
                            results          = casesList.GetItems(sPQuery);
                        }
                    });

                    litOwners.Text = string.Empty;
                    foreach (SPListItem result in results)
                    {
                        litOwners.Text += new SPFieldLookupValue(result["AssignedTo"].ToString()).LookupValue + "<br />";
                    }

                    if (litOwners.Text == string.Empty)
                    {
                        litOwners.Text = "<b>There are no results</b>";
                    }
                }
            }
            else
            {
                pnlResults.Visible = true;
                litOwners.Text     = "<b>The data entered in the Law Issues List fields is invalid.  Please ensure the field contains valid values.</b>";
            }
        }
Example #7
0
 private int[] GetWSSIds(Guid termId, Guid termSetId)
 {
     //Guid termsetId = new Guid("8803681f-9301-4578-9d7b-a5a0eecd10ea");
     return(TaxonomyField.GetWssIdsOfTerm(this._site, _TermStore.Id, termSetId, termId, false, 500));
     //return TaxonomyField.GetWssIdsOfTerm(this._site, _TermStore.Id, new Guid("f8fb708a-097d-4571-9a0e-5629206d7949"), new Guid("bcd32d39-6858-4000-bdfd-fcc7037285fa"), true, 500);
 }
Example #8
0
 public static int[] GetWssIdsOfTerm(SPSite site, WBTerm term, bool includeDescendants)
 {
     return(TaxonomyField.GetWssIdsOfTerm(site, term.Taxonomy.TermStore.Id, term.Taxonomy.TermSet.Id, term.Id, includeDescendants, 500));
 }
        protected void getItemsButton_Click(object sender, EventArgs e)
        {
            if ((searchForTextBox.Text != String.Empty) && (nameOfListTextbox.Text != String.Empty))
            {
                SPSite currentSite = SPContext.Current.Site;
                //We must create a new Taxonomy Session object
                TaxonomySession session = new TaxonomySession(currentSite);
                //Get the termstore
                TermStore termstore = session.TermStores["Managed Metadata Service"];

                //Now we can find the collection of terms that satisfy the user's request
                TermCollection terms = termstore.GetTerms(searchForTextBox.Text, true);

                if (terms.Count > 0)
                {
                    //For simplicity we'll just use the first term returned
                    Term firstTerm = terms[0];
                    resultLabel.Text = "Term Found. Term ID: " + firstTerm.Id + "<br /><br />";

                    //Now that we have a term object to search for, we must get the
                    //WssIds of that term in the current site collection. Note that a
                    //single term from a term store can have different WssIds in each
                    //site collection. Use the GetWssIdsOfTerm method to findout what
                    //they are. You can also see these in the Taxonomy Hidden List.
                    int[] WssIds = TaxonomyField.GetWssIdsOfTerm(currentSite,
                                                                 termstore.Id, firstTerm.TermSet.Id, firstTerm.Id, false, 500);

                    if (WssIds.GetLength(0) == 0)
                    {
                        //This tag is in the tagstore but not the Taxonomy Hidden List
                        //That usually means it hasn't been used in the current site
                        resultLabel.Text += "There are no entries for that term in the taxonomy hidden list. " +
                                            "Have you tagged an item in the site with the tag you search for?";
                    }
                    else
                    {
                        //Tell the user what the WSS IDs for the located term are
                        //and formulate a CAML query
                        string camlQuery = "<Where><In><FieldRef Name='Tags' LookupId='TRUE' /><Values>";
                        resultLabel.Text += "Here are the WssIds: <br />";
                        foreach (int WssId in WssIds)
                        {
                            camlQuery        += String.Format("<Value Type='Integer'>{0}</Value>", WssId);
                            resultLabel.Text += WssId.ToString() + "<br />";
                        }
                        //Complete the CAML query
                        camlQuery += @"</Values></In></Where>";

                        //Get the list and create the query
                        SPList  searchList = currentSite.RootWeb.Lists[nameOfListTextbox.Text];
                        SPQuery query      = new SPQuery();
                        query.Query = camlQuery;

                        if (searchList != null)
                        {
                            //Run the query
                            SPListItemCollection items = searchList.GetItems(query);
                            //Tell the user what tagged items were found
                            resultLabel.Text += "<br />Number of items with that tag: " + items.Count.ToString() + "<br /><br />";
                            foreach (SPItem currentItem in items)
                            {
                                resultLabel.Text += "Item Title: " + currentItem["Title"] + "<br />";
                                resultLabel.Text += "Item ID: " + currentItem.ID + "<br />";
                            }
                        }
                        else
                        {
                            //The list name the user entered did not match the name of a list
                            resultLabel.Text += "<br />There is no list by that name.";
                        }
                    }
                }
                else
                {
                    //The text the user entered did not match the name of a term in the termstore
                    resultLabel.Text = "There is no term in the termstore that matches your text";
                }
            }
        }