Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

I need to create a web part page programmatically in SP 2013.

$
0
0

I have added the following code (I have found on the internet) to the feature activation event receiver, but the page is not available when I deploy the project to a site.

Perhaps something else, beside the code below needs to be done to complete the page creation and I’m missing those steps…

I did debugging on the code, the debugging goes through every line of the code without errors, value in the variables looks as expected, but the page is not created in SitePages library…

 

publicoverridevoid FeatureActivated(SPFeatureReceiverProperties properties)

        {          

            try

            {               

                using (SPSite site = (SPSite)properties.Feature.Parent)

                {

                   using (SPWeb oWebsite = site.OpenWeb())

                    {

                       string xml = GetCreateWebPartPage("SitePages","SamplePage", 2);

                        oWebsite.ProcessBatchData(xml);

                    }

                }

            }

           catch (Exception ex)

            {

               Log.Error(ex);

            }

           finally

            {

                 pmoWeb.AllowUnsafeUpdates = false;

            }

 

            UpdateVersioningOnExistingSubSites(pmoSite);

        }

       privatestring GetCreateWebPartPage(String list, string pageTitle,int layoutTemplate)

        {

           conststring newItemTemplate ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+

                                          "<Batch>"+

                                          "<Method ID=\"0,NewWebPage\">"+

                                           "<SetList Scope=\"Request\">{0}</SetList>"+

                                           "<SetVar Name=\"Cmd\">NewWebPage</SetVar>"+

                                           "<SetVar Name=\"ID\">New</SetVar>"+

                                           "<SetVar Name=\"Type\">WebPartPage</SetVar>"+

                                           "<SetVar Name=\"WebPartPageTemplate\">{2}</SetVar>"+

                                           "<SetVar Name=\"Overwrite\">true</SetVar>"+

                                           "<SetVar Name=\"Title\">{1}</SetVar>"+

                                           "</Method>"+

                                            "</Batch>";

           var newItemXml =string.Format(newItemTemplate, list, pageTitle, layoutTemplate);

 

           return newItemXml;

        }

I did debugging on the code, the debugging goes through every line of the code without errors, value in the variables looks as expected, but the page is not created in SitePages library where I expect it to be…

 

 


Also, that’s what I got from the logs:

 

System.InvalidCastException: Unable to cast object of type 'Microsoft.SharePoint.SPWeb' to type 'Microsoft.SharePoint.SPSite'.

   at LB.Intranet.PMO.Features.PMO_Home_Page.PMO_Home_PageEventReceiver.FeatureActivated(SPFeatureReceiverProperties properties)

 

ANY HELP WILL BE GREATLY APPRECIATED - I GOT COMPLETELY STUCK HERE....


Viewing all articles
Browse latest Browse all 11571

Trending Articles