Get PublishingPageContent using SharePoint Web Services


Well.. this was something interesting I was looking into for a few hours and came up with this quick solution.

We can easily get the PublishingPageContent of any Publishing Page using Publishing API but what if we want to achieve this using SharePoint Web Services?

Here is sample code.

I have  created a simple console application where I am trying to access SharePoint Pages List data using Lists.asmx.

We just need to pass the url of the site and the title of the page; this creates a local .htm file which has PublishingPageContents and Image if any.

Splitting code is basically done to refractor the urls of images since the file is getting saved locally.

I know that this code is somewhat dirty and can be much improved but that was just a quick solution I figured out and so I posted it.

I hope this helps someone (at least to try some trick) J

Reference : SharePoint Magazine

static void Main(string[] args)

{

  string siteUrl = string.Empty;

  string pageName = string.Empty;

  Console.WriteLine("Enter Site Name:");

  siteUrl = Console.ReadLine();

  Console.WriteLine("Enter Page Name:");

  pageName = Console.ReadLine();

  Uri _uri = new Uri(siteUrl);

 

  #region Get Publishing Page Content

 

  StringBuilder _contentImageBuilder = null;

 

  try

  {

    ListsSvc.Lists lsts = new ConsumeWebService.ListsSvc.Lists();

    lsts.UseDefaultCredentials = true;

    lsts.Url = siteUrl + @"/_vti_bin/Lists.asmx"; ;

 

    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

    System.Xml.XmlElement query = xmlDoc.CreateElement("Query");

    System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");

    System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");

 

    /*Use CAML query*/

 

    //Querying Pages with Required Page Name

    query.InnerXml = @"<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>" + pageName + "</Value></Contains></Where>";

 

    //ViewFields to be returned with Result

    viewFields.InnerXml = "<FieldRef Name=\"Title\" /><FieldRef Name=\"PublishingPageContent\" />";

 

    queryOptions.InnerXml = string.Empty;

 

   //Get Data 

   System.Xml.XmlNode nodes = lsts.GetListItems("Pages", string.Empty, query, viewFields, "500", null, null);

 

   foreach (System.Xml.XmlNode node in nodes)

   {

     if (node.Name == "rs:data")

     {

       for (int i = 0; i < node.ChildNodes.Count; i++)

       {

         if (node.ChildNodes[i].Name == "z:row")

         {

           if (node.ChildNodes[i].Attributes["ows_PublishingPageContent"] != null)

           {

             _contentImageBuilder = new StringBuilder();

 

             string[] tokens = node.ChildNodes[i].Attributes["ows_PublishingPageContent"].Value.Split('"');

             foreach (string token in tokens)

             {

              if (token.StartsWith("/"))

              {

_contentImageBuilder.AppendFormat("{0}://{1}{2}", _uri.Scheme, _uri.Host, token);

              }

              else

              {

                 _contentImageBuilder.Append(token);

              }

             }

            }

 

if (_contentImageBuilder != null && !string.IsNullOrEmpty(_contentImageBuilder.ToString()))

{

StreamWriter writer= new StreamWriter("C:\\contents.htm", true, Encoding.UTF8);                                                                        writer.Write(_contentImageBuilder.ToString());

  writer.Close();

}

 

}

}

}

}

}

catch (Exception ex)

{

  Console.WriteLine("Error " + ex.Message);

}

#endregion

}