locked
How to retrieve content from Last Published version of PublishingPage? RRS feed

  • Question


  • Hi Everybody,

    I have a publishing page library configured with two content types. I have created the pages based on the content type. Some of the pages are in published mode and some of the page are in draft mode. I want to retrieve content of particular content type field from the published version of each page present in Page library(even if the page is in draft mode). Please let me know about how to do it?

    Regards,
    Prakash

    www.aristonsoft.com - .net 3.5 hosting provider in India
    Wednesday, January 12, 2011 12:32 PM

Answers

  • Hi Brain,

    Thanks for your help. Instead of CAML query I have used foreach loop to iterate web and publishing pages.

        void ProcessAllWebs()
        {
          using (SPSite siteColl = new SPSite(SPContext.Current.Web.Url))
          {
            foreach (SPWeb spWeb in siteColl.AllWebs)
            {
              PublishingWeb pubSite = PublishingWeb.GetPublishingWeb(spWeb);
              foreach (PublishingPage pubPage in pubSite.GetPublishingPages())
              {
                string mycontent = string.Empty;
                SPListItemVersion pubversion = GetPublishedVersion(pubPage.ListItem);
                if (pubversion != null)
                {
                  mycontent = pubversion["MyContentFiled"].ToString();
                }
                else
                {
                  mycontent = pubPage.ListItem["MyContentFiled"].ToString();
                }
              }
              spWeb.Dispose();
            }
          }
        }
    		
        private static SPListItemVersion GetPublishedVersion(SPListItem item)
        {
          SPListItemVersion result = null;
          if (item.Level == SPFileLevel.Published)
            result = item.Versions[0];
          else if (item.Versions.Count > 0)
          {
            for (int i = 1; i < item.Versions.Count; i++)
            {
              if (item.Versions[i].Level == SPFileLevel.Published)
              {
                result = item.Versions[i];
                break;
              }
            }
          }
          return result;
        } 
    
    
    

    Regards,
    Prakash

    • Marked as answer by Mike Walsh FIN Thursday, January 13, 2011 6:23 AM
    Thursday, January 13, 2011 4:34 AM

All replies

  • Ok I did some digging: here is the object you need PublishingPage.ListItem.ListItem.Versions[0].Level == SPFileLevel.Published

    The information you need is buried in the SPListItemVersion history.  A PublishingPage is just a specialized SPListItem and so it has version history.

    So I don't think its possbile to write a single CAML query to do all this filtering. Because I don't know of a CAML query that can read version history.

    1. I would write my first CAML query to get all PublishingPages with a specific content type. Again its just a listitem and it has a contenttypeid field you can search against.

    2. Then I would create two loops. First being each listitem from query 1.  The second being the version collection.  You'll need to start at index 0 being most recent and increment until you find the first one with Level of Published.

    3. Aggregate your results.

    You should practice this using Powershell.  It's a good skill to know.


    Brian Bedard, MCTS - pioneeringsharepoint.blogspot.com - Twitter:@tigertoy

    Wednesday, January 12, 2011 2:24 PM
  • Hi Brain,

    Thanks for your help. Instead of CAML query I have used foreach loop to iterate web and publishing pages.

        void ProcessAllWebs()
        {
          using (SPSite siteColl = new SPSite(SPContext.Current.Web.Url))
          {
            foreach (SPWeb spWeb in siteColl.AllWebs)
            {
              PublishingWeb pubSite = PublishingWeb.GetPublishingWeb(spWeb);
              foreach (PublishingPage pubPage in pubSite.GetPublishingPages())
              {
                string mycontent = string.Empty;
                SPListItemVersion pubversion = GetPublishedVersion(pubPage.ListItem);
                if (pubversion != null)
                {
                  mycontent = pubversion["MyContentFiled"].ToString();
                }
                else
                {
                  mycontent = pubPage.ListItem["MyContentFiled"].ToString();
                }
              }
              spWeb.Dispose();
            }
          }
        }
    		
        private static SPListItemVersion GetPublishedVersion(SPListItem item)
        {
          SPListItemVersion result = null;
          if (item.Level == SPFileLevel.Published)
            result = item.Versions[0];
          else if (item.Versions.Count > 0)
          {
            for (int i = 1; i < item.Versions.Count; i++)
            {
              if (item.Versions[i].Level == SPFileLevel.Published)
              {
                result = item.Versions[i];
                break;
              }
            }
          }
          return result;
        } 
    
    
    

    Regards,
    Prakash

    • Marked as answer by Mike Walsh FIN Thursday, January 13, 2011 6:23 AM
    Thursday, January 13, 2011 4:34 AM