Snippet Fire

Beta
 Log In    |   Sign Up

.NET : Programmatically Read A Publishing Page in SharePoint

Added on Jan-30-2012 by admin
For .NET

Tags : sharepoint

This snippet will programmatically read the contents of a publishing page in SharePoint. In this sample snippet, the field in the PublishingPage holding the content is PublishingPageContent and the ListTemplateID of Pages Libraries is 850.

DOWNLOAD

SPSiteDataQuery queryObj = new SPSiteDataQuery();
 
    //query Pages libraries
    queryObj.Lists = "<Lists ServerTemplate='850' />";
    queryObj.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='FileRef' />";
    queryObj.Webs = "<Webs Scope='Recursive' />";
    DataTable dt = SPContext.Current.Web.GetSiteData(query);
 
    foreach (DataRow row in dt.Rows)
    {
        string content = string.Empty;
 
        using (SPWeb web = SPContext.Current.Site.OpenWeb(new Guid(row["WebID"].ToString())))
        {
            SPList pages = web.Lists[new Guid(row["ListID"].ToString())];
            SPListItem item = pages.GetItemById(int.Parse(row["ID"].ToString()));
 
            content = item["PublishingPageContent"].ToString();
            content = RemoveHTML(content);
 
            if (content.Length > 200)
            {
                content = content.Substring(0, 200);
            }
        }
    }


    

Report Snippet

blog comments powered by Disqus