Friday 25 November 2016

remove a term from managed metadata column in Sharepoint




Hi All,

 Today I will share you the code for removing a single term from a Managed Metadata (- multivalue) column in a list.

Name of the List - "Todo List"
Name of the Managed metadata column - "TodoColumn"
Values it is holding at present - "Lesson 1" , "Lesson 2" , "Lesson 3" , "Lesson 4"
Value to be removed - "Lesson 1"

Now we will see the code :


   SPList list = oweb.Lists["Todo List"];
                    SPQuery oQuery = new SPQuery();
                    oQuery.Query = " <Where><Eq><FieldRef Name='TodoColumn' /><Value Type='TaxonomyFieldTypeMulti'>Lesson 1</Value></Eq></Where>";
                    SPListItemCollection collListItems = list.GetItems(oQuery);
                    foreach (SPListItem oListItem in collListItems)
                    {
                        TaxonomyFieldValueCollection tfc = oListItem["TodoColumn"] as TaxonomyFieldValueCollection;
                        tfc.Remove(tfc.Where(i => i.Label.Equals("Lesson 1")).FirstOrDefault());
                        oListItem["TodoColumn"] = tfc;
                        oListItem.Update();
                    }
                    list.Update()


Thats's it :)

Have a great day... Happy coding :))))

Friday 4 November 2016

SharePoint - Programmatically get Friendly URL of Publishing pages

Hi Guys - After so long time, I am posting this. I got so busy in my project. Finally I am getting some time to post this. I got his requirement of finding the Friendly URL of Publishing pages.

Code is given below. Hope this helps someone.



using (SPSite osite = new SPSite("site url"))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    SPList olist = oweb.Lists["Pages"];
                    foreach (SPListItem item in olist.Items)
                    {
                        var associatedTerms = TaxonomyNavigation.GetFriendlyUrlsForListItem(item, false);

                        string url = string.Empty;
                        if (associatedTerms.Count > 0)
                        {
                            url = associatedTerms[0].GetResolvedDisplayUrl(string.Empty);
                        }
                    }
                }
            }


Happy Coding :)