Wednesday 2 December 2015

Read selected Metadata tagging across SharePoint SiteCollection

Couple of months back I was asked to generate the report of one Metadata Field present in SharePoint Online site i.e where and all that column is used along with the tagged value, item url, list name and blah blah blah..
Initially I thought of using SharePoint Search API but due to following drawbacks i stepped back:-

  • If any List or Library are selected not display records in search, then i wont be able to get it
  • Item level permission.
Hence i decided to write a CSOM code, as report has to be submitted in couple of hours. Code is quite simple and easy to understand, it would have become reusable if I would have made it recursive. But in may case it was not required as there wasn't any sub site under any site ;)


static void Main(string[] args)  
     {  
       Console.WriteLine("Enter Online Site URL");  
       string siteUrl = Console.ReadLine();  
       Console.WriteLine("Enter User Name");  
       string userName = Console.ReadLine();  
       Console.WriteLine("Enter Password");  
       string password = Console.ReadLine();  
       Console.WriteLine("Enter Metadata Column's Internal Name");  
       string column = Console.ReadLine();  
       SecureString securePass = new SecureString();  
       foreach (char c in password)  
       {  
         securePass.AppendChar(c);  
       }  
       StringBuilder strBldr = new StringBuilder();  
       strBldr.AppendLine("SiteName,ItemUrl,Tags");  
       using (ClientContext ctx = new ClientContext(siteUrl))  
       {  
         ctx.Credentials = new SharePointOnlineCredentials(userName,  
           securePass);  
         Web oweb = ctx.Site.RootWeb;  
         ctx.Load(oweb);  
         ctx.Load(oweb.Webs);  
         ctx.Load(oweb.Lists);  
         ctx.ExecuteQuery();  
         #region Read root level lists  
         //This can be made recursive also  
         foreach (List olist in oweb.Lists)  
         {  
           ctx.Load(olist);  
           ctx.Load(olist, l => l.DefaultViewUrl);  
           if (olist.ContainsField(column))  
           {  
             ListItemCollection ocoll = olist.GetItems(CamlQuery.CreateAllItemsQuery());  
             ctx.Load(ocoll);  
             ctx.ExecuteQuery();  
             foreach (ListItem oitem in ocoll)  
             {  
               TaxonomyFieldValueCollection taxFieldValueColl = oitem[column] as TaxonomyFieldValueCollection;  
               string tags = string.Empty;  
               // Loop through all the taxonomy field values  
               foreach (TaxonomyFieldValue taxFieldValue in taxFieldValueColl)  
               {  
                 // Display the taxonomy field value  
                 Console.WriteLine(taxFieldValue.Label);  
                 tags += RemoveComma(taxFieldValue.Label, "-") + ";";  
               }  
               if (!string.IsNullOrEmpty(tags))  
               {  
                 strBldr.AppendLine(RemoveComma(oweb.Title, "-") +  
                   "," + RemoveComma(Convert.ToString(oitem.FieldValues["FileRef"]), "%2C") + "," + tags);  
               }  
             }  
             Console.WriteLine("Site Title - " + oweb.Title + ", List Title - " + olist.Title + " Item Count - " + olist.ItemCount);  
           }  
         }  
         #endregion  
         Console.WriteLine("Root Web Title - " + oweb.Title);  
         //read each site and list  
         foreach (Web web in oweb.Webs)  
         {  
           ctx.Load(web);  
           ctx.Load(web.Lists);  
           ctx.ExecuteQuery();  
           foreach (List lis in web.Lists)  
           {  
             ctx.Load(lis);  
             ctx.Load(lis, k => k.DefaultViewUrl);  
             if (lis.ContainsField(column))  
             {  
               Console.WriteLine("Site Title " + web.Title + ", List Title - " + lis.Title + " Item Count - " + lis.ItemCount);  
               ListItemCollection ocoll = lis.GetItems(CamlQuery.CreateAllItemsQuery());  
               ctx.Load(ocoll);  
               ctx.ExecuteQuery();  
               foreach (ListItem oitem in ocoll)  
               {  
                 TaxonomyFieldValueCollection taxFieldValueColl = oitem[column] as TaxonomyFieldValueCollection;  
                 string tags = string.Empty;  
                 // Loop through all the taxonomy field values  
                 foreach (TaxonomyFieldValue taxFieldValue in taxFieldValueColl)  
                 {  
                   // Display the taxonomy field value  
                   Console.WriteLine(taxFieldValue.Label);  
                   tags += RemoveComma(taxFieldValue.Label, "-") + ";";  
                 }  
                 if (!string.IsNullOrEmpty(tags))  
                 {  
                   strBldr.AppendLine(RemoveComma(web.Title, "-") +  
                     "," + RemoveComma(Convert.ToString(oitem.FieldValues["FileRef"]), "%2C") + "," + tags);  
                 }  
               }  
             }  
           }  
         }  
         System.IO.File.WriteAllText(@"D:\" + column + ".csv", strBldr.ToString());  
       }  
     }  
     /// <summary>  
     /// This method will replace comma in the given string with given charcter  
     /// </summary>  
     /// <param name="inputValue"></param>  
     /// <param name="replace"></param>  
     /// <returns></returns>  
     private static string RemoveComma(string inputValue, string replace)  
     {  
       string tempInputValue = inputValue.Replace(",", replace);  
       return tempInputValue;  
     } 

No comments:

Post a Comment