-1

I have an publishing site collection using the enterprise wiki site collection template inside my sharepoint server 2013. And inside the root site of my site collection i added a custom list named "customers", and i added an event receiver which will get fired when a new item is added inside the customers list. the event receiver will create 2 wiki pages as follow:-

 public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);

            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);

                ///First page
                PublishingPageCollection pPages = pWeb.GetPublishingPages();
                PublishingPage pPage1 = pPages.Add(properties.ListItem.Title + " - About Customer.aspx", pageLayout);
                SPListItem newpage1 = pPage1.ListItem;
                newpage1["Title"] = properties.ListItem.Title+" - About Customer.aspx";//"Page added programmatically";
                newpage1["PublishingPageContent"] = "<a href='/kb/CustomerServiceKB/Pages/" + properties.ListItem.Title + " - Main.aspx" + @"'>Back To Main Page</a> ";                           

                ///second Page
                PublishingPage pPage = pPages.Add(properties.ListItem.Title + " - Main.aspx", pageLayout);
                SPListItem newpage = pPage.ListItem;
                newpage["Title"] = properties.ListItem.Title + " - Main.aspx";//"Page added programmatically";
                newpage["PublishingPageContent"] = 
                @"<table class='ms-rteTable-default' cellspacing='0' style='width: 65%; height: 803px; text-align: left;'>   //code goes here";
                newpage.Update();
                newpage1.Update();

            }
        }


    }
}

now currently i test the above event receiver and it is working well. but as i know that based on MSDN recommendation it is not recommended to create new instances of SPSite & SPWeb inside event receiver https://msdn.microsoft.com/en-us/library/office/ee724407(v=office.14).aspx .

now in my above event receiver i am not sure about these 2 points:-

1) inside these 2 lines of code, am i violating the MSDN recommendations?? :-

PublishingSite pSite = new PublishingSite(properties.Site);
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(properties.Web);

since i am creating new instances of the PublishingSite & PublishingWeb?

2) now i try to do something as follow, to try to reference the current PublishingSite directly from the SPItemEventProperties :-

 PublishingSite pSite = properties.Site;

instead of

PublishingSite pSite = new PublishingSite(properties.Site);

but i got this error:-

Cannot implicitly convert type 'Microsoft.SharePoint.SPSite' to 'Microsoft.SharePoint.Publishing.PublishingSite'

so can anyone adivce on my above two points,, so i can make my event receiver compatible with the MSDN recommendation of not creating new instances of SPSite & SPWeb inside the event receivers??

Thanks

1 Answer 1

2

TL;DR;
1) No, you don't violate MSDN recommendations.
2) No need as per 1)

When you call new PublishingSite(properties.Site);, you don't create new SPSite or SPWeb objects. These 2 objects are "dangerous" because they hold the connections to the DB. However, PublishingSite is only a "business wrapper" arround SPSite: i.e. it uses SPSite to present a "publishing" API by requesting the underlying SPSite access to the data.
So, no worries here.

As per your second question:

a. It's now irrelevant :)

b. It fails because PublishingSite and SPSite are 2 different objects in a C#/.NET sense: they're not related and PublishingSite does not inherit from SPSite. The only relation they have is PublishingSite encapsulating an SPSite object.

2
  • thanks for the reply... just to make sure i got ur point correctly .. so when i do something as follow PublishingSite pSite = new PublishingSite(properties.Site); & PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(properties.Web); then i am still referencing the properties.Site & properties.Web ? so i am inline with the MSDN recommendations ?
    – John John
    Jan 30, 2017 at 17:08
  • Yes, you're correct.
    – Evariste
    Jan 30, 2017 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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