Recurse Through SharePoint Using OM
I figured that today I would write a quick blog on how to recurse through the sharepoint sites and pages with minimal code so here goes:
SPSite SPSSite; public void DoWork() { string TopLevelSite = ConfigurationSettings.AppSettings["Site"]; Console.WriteLine("**** RUN DATE: " + System.DateTime.Now); Console.WriteLine("**** CONNECTING TO SITE: " + TopLevelSite); SPSSite = new SPSite(TopLevelSite); Console.WriteLine("**** CONNECTION SUCCESS"); ArrayList newList;
try { foreach (SPWeb sPWeb in SPSSite.AllWebs) { if (sPWeb.Url.ToLower().StartsWith(TopLevelSite.ToLower() + "/vso")) { try {
SPList sPList = sPWeb.Lists["Pages"]; Console.Write("Processing Site: " + sPWeb.Name + "\n"); ProcessPages(sPList, sPWeb); } catch (Exception ex) { Console.WriteLine(sPWeb.Name + ": " + ex.ToString()); } } } } catch (Exception ex) { Console.Write ("Error DoWork: " + ex.Message); } }
#region private void ProcessPages(SPList PagesList, SPWeb sp) private void ProcessPages(SPList PagesList, SPWeb sp) { try { //Set Hidden Field string dName = "Pages"; SPFolder dFol = sp.Folders[dName]; SPDocumentLibrary docLib = (SPDocumentLibrary)sp.Lists[dName]; SPView dv = docLib.Views["All Documents"]; SPListItemCollection dItems = docLib.GetItemsInFolder(dv, dFol);
foreach (SPListItem it in dItems) { try { Console.WriteLine("Pages: " + it.Name) } catch (Exception ex) { Console.WriteLine("Page Enumeration Error " + it.Name.ToString() + ": " + ex.ToString()); } } } catch (Exception ex) { Console.Write("Error ProcessPages: " + ex.Message); } } |
Stick this code into a quick console app and include the Microsoft.SharePoint.dll as a reference and there you go.
I know not much but hey, that was the point right?