0

I need to group the elements in the xml by Article_x0020_Tags . Here I get the items from the sharepoint pages library , which has a custom column called "article Tags" which is a managed metadata column which allows multiple values. I want to get the count of all the terms used in this manages metadata column used in this column for this library. I am doing this by getting all the items from the pages library and getting the count by using groupby and then split the multiple terms and get the overall count of the number of times the individual terms that has been added. I have written a linq query using groupby. But I am stuck in getting the count of in dividual groups. How do IO get the count?

using (SPSite site = new SPSite(SPSiteId))
        {
            using (SPWeb web = site.OpenWeb(SPWebId))
            {
                try
                {
                    Siteurl = site.Url;                   
                    string listURL = web.Url.TrimEnd('/') + "/"+ ListName;
                    ArticlePages = web.GetList(listURL);
                    if (ArticlePages != null)
                    {
                        SPQuery oQuery = new SPQuery();
                        oQuery.ViewFields = "<FieldRef Name='Article_x0020_Tags'/>";                                 
                        SPListItemCollection objArticlesCollection = ArticlePages.GetItems(oQuery);
                       string articleTagItemXML = objArticlesCollection.Xml;
                             XDocument xdoc = XDocument.Parse(articleTagItemXML);
                            XNamespace rs = "urn:schemas-microsoft-com:rowset";
                            XNamespace z = "#RowsetSchema";
                  var result = xdoc.Elements(rs + "data")
      .Elements(z + "row").GroupBy(item => item.Attribute("ows_Article_x0020_Tags").Value);

}
}

And i am not sure if I am using the right approach to achieve this. Please suggest if I could use any better approach without looping.

0

I used the following and got the count of tags

var tags = xdoc.Descendants(z + "row")
                                        .Select(r => (string)r.Attribute("ows_Article_x0020_Tags"));
                        // Join all the tags to form a string
                         string result = String.Join("", tags);
                        //Remove id's of the tags from the string
                         result = Regex.Replace(result, @"\d", "");   
                        //remove special characters from the string to get a list of tags               
                        var  tagTokens = result.Split(new string[]{";#"},StringSplitOptions.RemoveEmptyEntries).ToList();
                        //Get the count of each tag being used in the list
                        var tokenGroups = tagTokens.GroupBy(token => token).ToDictionary(token=> token.Key, tokenval=>tokenval.Count());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.