1

I am working on an event receiver inside my Enterprise wiki site collection inside SharePoint 2013. Now inside my event receiver I am trying to do the following:-

  1. Find specific wiki page (by title, Name or UL).

  2. For this page get its HTML Source code. Similar to the HTML source code I get when I edit a wiki page, click on “Edit Source” button.

enter image description here

Now I have the following code inside my event receiver, where I am able to get all the wiki pages, but seems trying to get a wiki page by title or by name is not working using the indexer,, as the PublishingPage pp variable inside my code will always be null..

public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            SPWeb spCurrentSite = properties.OpenWeb();
            if (properties.List.Title.ToLower() == "customers")
            {



                PublishingSite pSite = new PublishingSite(properties.Site);
                SPContentType ctype = pSite.ContentTypes["Enterprise Wiki Page"];
                PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true);
                PageLayout pageLayout = pageLayouts["EnterpriseWiki.aspx"];
                PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(properties.Web);

                PublishingPageCollection pPages = pWeb.GetPublishingPages();
                //
                int t = pPages.Count;
                PublishingPage pp = pPages["Test - Main"];// I have a wiki page with title= “Test – Main”

so can anyone adivce if there is a way to get a specific wiki page by title,name or url and for this wiki page to get its HTML source?

Thanks

2

Yes, one way to get it by server relative url by using code below:

public static string GetWikiPageContent(Web web, string serverRelativePageUrl)
{

    var file = web.GetFileByServerRelativeUrl(serverRelativePageUrl);

    web.Context.Load(file, f => f.ListItemAllFields);

    web.Context.ExecuteQuery();

    return file.ListItemAllFields["WikiField"] as string;
}  

If you are using PnP package, you can just write web.GetWikiPageContent(<page server relative url>)

  • thanks for the reply ,, but when i try something such as string r = file.ListItemAllFields["WikiField"] as string i will get the following exception at runtime Invalid field name. {c33527b4-****-45024d00068a} .. so instead i use the field internalname to get the page content using something as follow wikiItem["PublishingPageContent"] is this fine ? – john Gu Jan 31 '17 at 13:41
  • Are you sure that's wiki page? From your comments I understand that this is publishing page and not wiki. Wiki page always has WikiField – Sergei Sergeev Jan 31 '17 at 13:43
  • yes it is a wiki page inside an enterprise wiki site collection !! and ts content type is the defualt "Enterprise Wiki Page" – john Gu Jan 31 '17 at 14:35
  • can you please advice why you mentioned that my page might not be a wiki page ?? as it is a page inside the "Pages" library inside an enterprise wiki site collection root web ,, and it have the "Enterprise Wiki Page" content type and "enterprisewiki.aspx" page layout and it is linked to the defualt wiki category column .. – john Gu Jan 31 '17 at 14:51
1

Try the below code, you need to pass the server relative url of the wiki page.

public override void ItemAdded(SPItemEventProperties properties)
{
    base.ItemAdded(properties);
    SPWeb spCurrentSite = properties.OpenWeb();
    if (properties.List.Title.ToLower() == "customers")
    {

        string wikiPageUrl = "server relative url of the page";
        SPFile wikiPage = spCurrentSite.GetFile(wikiPageUrl);
        SPListItem wikiItem = wikiPage.Item; 
        wikiItem[SPBuiltInFieldId.WikiField] = "My Wiki list item updated"; 
        wikiItem.Update();
    }
}
  • thanks for the reply,, but i am unable to reference the list item field as follow wikiItem[SPBuiltInFieldId.WikiField] i will get a run-time exception that the column might have been deleted... so instead i use wikiItem["PublishingPageContent"] is this fine ??now in my case i have an Enterprise wiki site collection .. could the reason for the error i got is that the SPBuiltInFieldId.WikiField is not part of the enterprise wiki page ?? – john Gu Jan 31 '17 at 13:40
  • I think its ok, but then this is not a wiki page. Its a publishing page. What's the url of the page ? Is it in sitepages or pages library? – Gautam Sheth Jan 31 '17 at 13:47
  • no it is an enterprise wiki page inside an enterprise wiki site collection , the page url is something such as "/Pages/latest- About.aspx" and the page content type is the built-in "Enterprise Wiki Page" content type !! and the page have the built-in wiki category managed metadata column.. i also created a brand new test enterprise wiki site collection , and the pages library have the "PublishingPageContent" field also.. so why you assume that my page is not a wiki page – john Gu Jan 31 '17 at 14:37
  • My bad, I assumed it as soon as you mentioned publishing page content. I now realize that this field is also associated with wiki pages in pages library. – Gautam Sheth Jan 31 '17 at 16:29
  • @Gaitam can you please advice what you mean exactly in your last comment ? – john Gu Feb 1 '17 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.