Get PublishingPageContent using SharePoint Web Services

n

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

n

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

n

nHere is sample code,

n

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

n

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

n

nSplitting code is basically done to refractor the urls of images because file is getting saved locally

n

n

n

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

n

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

n

nReference : SharePoint Magazine

n

n

n

nstatic void Main(string[] args)

n

n{

n

n  string siteUrl = string.Empty;

n

n  string pageName = string.Empty;

n

n  Console.WriteLine(“Enter Site Name:”);

n

n  siteUrl = Console.ReadLine();

n

n  Console.WriteLine(“Enter Page Name:”);

n

n  pageName = Console.ReadLine();

n

n  Uri _uri = new Uri(siteUrl);

n

n

n

n  #region Get Publishing Page Content

n

n

n

n  StringBuilder _contentImageBuilder = null;

n

n

n

n  try

n

n  {

n

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

n

n    lsts.UseDefaultCredentials = true;

n

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

n

n

n

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

n

n    System.Xml.XmlElement query = xmlDoc.CreateElement(“Query”);

n

n    System.Xml.XmlElement viewFields = xmlDoc.CreateElement(“ViewFields”);

n

n    System.Xml.XmlElement queryOptions = xmlDoc.CreateElement(“QueryOptions”);

n

n

n

n    /*Use CAML query*/

n

n

n

n    //Querying Pages with Required Page Name

n

n    query.InnerXml = @”<Where><Contains><FieldRef Name=’Title’ /><Value Type=’Text’>” + pageName + “</Value></Contains></Where>”;

n

n

n

n    //ViewFields to be returned with Result

n

n    viewFields.InnerXml = “<FieldRef Name=\”Title\” /><FieldRef Name=\”PublishingPageContent\” />”;

n

n

n

n    queryOptions.InnerXml = string.Empty;

n

n

n

n   //Get Data 

n

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

n

n

n

n   foreach (System.Xml.XmlNode node in nodes)

n

n   {

n

n     if (node.Name == “rs:data”)

n

n     {

n

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

n

n       {

n

n         if (node.ChildNodes[i].Name == “z:row”)

n

n         {

n

n           if (node.ChildNodes[i].Attributes[“ows_PublishingPageContent”] != null)

n

n           {

n

n             _contentImageBuilder = new StringBuilder();

n

n

n

n             string[] tokens = node.ChildNodes[i].Attributes[“ows_PublishingPageContent”].Value.Split(‘”‘);

n

n             foreach (string token in tokens)

n

n             {

n

n              if (token.StartsWith(“/”))

n

n              {

n

n_contentImageBuilder.AppendFormat(“{0}://{1}{2}”, _uri.Scheme, _uri.Host, token);

n

n              }

n

n              else

n

n              {

n

n                 _contentImageBuilder.Append(token);

n

n              }

n

n             }

n

n            }

n

n

n

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

n

n{

n

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

n

n  writer.Close();

n

n}

n

n

n

n}

n

n}

n

n}

n

n}

n

n}

n

ncatch (Exception ex)

n

n{

n

n  Console.WriteLine(“Error “ + ex.Message);

n

n}

n

n#endregion

n

n}

n

n

n

Leave a Comment