Retrieve SharePoint Wiki HTML

image Recently, I had to retrieve the HTML of SharePoint Wiki pages. The only example I could find   was Arpan shah’s blog post about Grabbing Wiki content.

Turns out that it is not difficult as I thought and Wiki is just a List in SharePoint and you can retrieve the HTML content using the following code:

 

 

//ListId : List ID of Wiki List

//SiteUrl: Current Site URL

//ItemId: Item ID of wiki page

public string GetWikiHTML(string ListId, string ItemId, string SiteUrl)

{    

    //Get Site, Web, List and Retrieve ListItem

    using (SPSite Site = new SPSite(SiteUrl))

    {

        using (SPWeb Web = Site.OpenWeb())

        {

            SPList List = Web.Lists[new Guid(ListId)];

            SPListItem ListItem = List.GetItemById((Int32.Parse(ItemId)));

            string wikiHTML = String.Format("<html><body>{0}</body></html>", 

            ListItem[ListItem.Fields.GetFieldByInternalName("ows_WikiField").Id].ToString());

        }

     }

}

 

If you have read my previous post you will notice that is it very easy to retrieve ListID, ItemID and SiteURL using SharePoint Custom Actions (List of URL Tokens for Custom Action) or for the toolbar – on the page retrieval you can use following piece of code:

SPContext SiteContext = null;

String ListId, ItemId, SiteUrl;

 

// Get current context

SiteContext = SPContext.GetContext(HttpContext.Current);

if (SiteContext != null)

{

    ListId = SiteContext.List.ID.ToString(); // Get Current Wiki List ID

        ItemId = SiteContext.ListItem.ID.ToString(); // Get current Wiki page Id

        SiteUrl = SiteContext.Web.Url.ToString(); // Get SiteUrl of Wiki Page

}

 

Hope this helps. If there is a better way of doing this – please leave me a comment.