Subscribe in a reader Report Publishing Page Checkout Status by using Recursion

Report Publishing Page Checkout Status by using Recursion

by JamieMcAllister 3/10/2008 3:38:00 PM

My client recently asked for a simple user control that would show which publishing pages were checked out. The idea being a one stop shop to find unpublished pages, and publish them! This gives me a good excuse to demonstrate a very powerful technique that can be applied to Sharepoint problems; that of recursion.  (As an aside, I've done another article that solves the same problem using CAML. You can check that out here http://www.the-north.com/sharepoint/post/Embed-CAML-Queries-into-the-Content-Query-Webpart---Finding-Draft-Publishing-Content.aspx but stick with this article on Recursion as it's a powerful technique you'll want to use often!).

Data in Sharepoint is stored in a hierarchical structure. It’s straightforward to write software that will navigate through that structure and find items of interest. I want to find all my publishing pages and check what state they are in. For this example I’m writing an ASP.NET user control that I can put on a page, and will interrogate every folder that has publishing pages in it. The page name will be displayed, along with a link to the page, and its checkout status.

This code is so simple that it doesn’t warrant a detailed explanation, see the full code listing lower down. I’ve added a bit of html formatting in the control for the output. I’m sure you can do a better job of that! 

One item of note is the SPFileLevel enumeration (SPListItem.File.Level). This has values of Published, Draft, or Checkout. So that’s the degree of granularity we can report on. OK, anyway, here’s the code; 

<script runat="server">    

StringBuilder sb = new StringBuilder();      

void Page_Load(object sender, EventArgs e)   

{      

     GetCheckedOutFiles(System.Web.HttpContext.Current.Request.Url.AbsoluteUri);   

}       

void GetCheckedOutFiles(string startingUrl)   

{       

     sb.Append("<table class='ms-informationbar'><tr><td>Page</td><td>Current Status</td></tr>");       

     using(SPSite site = new SPSite(startingUrl))       

     {           

          using(SPWeb web = site.OpenWeb())           

          {               

               RecursePublishingWebs(web);                                   

          }       

      }       

sb.Append("</table>");   

}    

void RecursePublishingWebs(SPWeb web)   

{       

     if (PublishingWeb.IsPublishingWeb(web))       

     {           

          IteratePublishingFiles(web);       

     }               

     foreach (SPWeb subWeb in web.Webs)       

     {           

          RecursePublishingWebs(subWeb);       

     }   

}    

void IteratePublishingFiles(SPWeb web)   

{       

     Guid pagesListId = PublishingWeb.GetPagesListId(web);        

     //Get the list by using the ID       

     SPList pagesList = web.Lists[pagesListId];       

     foreach (SPListItem item in pagesList.Items)        

     {              

          sb.Append("<tr><td>");              

          sb.Append("<a href='");              

          sb.Append(item.Web.ServerRelativeUrl);              

          sb.Append("/");              

          sb.Append(item.Url);              

          sb.Append("'>" + item.Name + "</a>");              

          sb.Append("</td><td>");              

          sb.Append(" [" + item.File.Level + "]");              

          sb.Append("</td></tr>");       

     }   

}

</script> 

<div style="overflow:auto;width:400px;height:200px">

<b>Checked Out Pages;</b> <br /><br />   

<%= sb.ToString() %>

</div>   

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

.NET | CMS | Sharepoint

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

11/20/2008 8:16:02 PM

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen

About the author

Jamie McAllister Jamie McAllister
Manchester (UK) Based Consultant for Northridge Solutions Ltd.


Jamie McAllister Linked In

        
        
        
        
        
        
        

Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in

Disclaimer: The software and source code on this website is provided "AS IS"
with no warranties of any kind. The entire risk arising out of the use or
performance of the software and source code is with you.