0

I am trying to create a list of all the values that are being used for the site column "Classification" across all sites of a site collection. The idea is to build a dictionary to add to the term store. As a first step I am checking all the document libraries to see which values the documents are using for the the field "Classification". Then I will check other lists as well.

I can recurse all the document libraries, but in order to check the metadata of the items of the document libraries it seems like I need to use some kind of CAML query? But what should I write as query? Or can this be done in another way?

       static void GetSites(string siteUrl) {

        try
        {
            ClientContext context = new ClientContext(siteUrl);
            Web oWebsite = context.Web;
            context.Load(oWebsite, website => website.Webs, website => website.Title, website => website.Created);
            context.ExecuteQuery();
            foreach (Web orWebsite in oWebsite.Webs)
            {
                Console.WriteLine("**********************************************************************************");
                Console.WriteLine(orWebsite.Created + " " + orWebsite.Title + " " + orWebsite.Url);
                GetLists(orWebsite.Url);
                GetSites(orWebsite.Url);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        } 
    }

        static void GetLists(string siteUrl) {
        try
        { 

            ClientContext context = new ClientContext(siteUrl);

            Web web = context.Web;  

            // Retrieve all lists from the server. 
            context.Load(web.Lists,
                         lists => lists.Include(list => list.Title, // For each list, retrieve Title and Id. 
                                                list => list.Id, list => list.LastItemModifiedDate, list => list.BaseType));

            // Execute query. 
            context.ExecuteQuery();

            // Enumerate the web.Lists. 
                            foreach (List list in web.Lists)
            {

                if (list.BaseType.ToString().Equals("DocumentLibrary"))
                {


                    Console.WriteLine("* {0}", list.Title);

                        CamlQuery query = CamlQuery.CreateAllItemsQuery(100);

                        ListItemCollection listItems = list.GetItems(query);
                        context.Load(listItems);
                        context.ExecuteQuery();

                        foreach (ListItem i in listItems)
                        {
                                Console.WriteLine("{0} ", i.FieldValues["xxx_Classification"].ToString());
                        }


                }

            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

Your Answer

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

Browse other questions tagged or ask your own question.