Creating a Publishing page from code
In some cases it could be necessary to create a page from code. Because
the publishing page is the hardest way to create from code (checkin /
approval) the next code example will help you completing this job.
Notice that I skipped complete error handling.
/// <summary>
/// Create a Publishing page based on an excisting Page Layout
/// Remember that a SPSite is a top-level site (collection) and
/// a SPWeb is the current site
/// </summary>
/// <param name="site">Current Site</param>
/// <param name="pageName">Name of the new page to create including .aspx</param>
/// <param name="pageLayoutName">Name of the Page Layout</param>
private void CreatePublishingPage(SPWeb site, string pageName, string pageLayoutName)
{
PublishingSite pubSiteCollection = new PublishingSite(site.Site);
PublishingWeb pubSite = null;
if (pubSiteCollection != null)
{
// Assign an object to the pubSite variable
if (PublishingWeb.IsPublishingWeb(site))
{
pubSite = PublishingWeb.GetPublishingWeb(site);
}
}
// Search for the page layout for creating the new page
PageLayout currentPageLayout = FindPageLayout(pubSiteCollection, pageLayoutName);
// Check or the Page Layout could be found in the collection
// if not (== null, return because the page has to be based on
// an excisting Page Layout
if (currentPageLayout == null)
{
return;
}
PublishingPageCollection pages = pubSite.GetPublishingPages();
PublishingPage newPage = pages.Add(pageName, currentPageLayout);
newPage.Description = pageName.Replace(".aspx", "");
// Here you can set some properties like:
newPage.IncludeInCurrentNavigation = true;
newPage.IncludeInGlobalNavigation = true;
// End of setting properties
SPFile publishFile = newPage.ListItem.File;
publishFile.Update();
newPage.Update();
// Check the file in (a major version)
publishFile.CheckIn("Initial", SPCheckinType.MajorCheckIn);
publishFile.Publish("Initial");
// In case of content approval, approve the file
if (pubSite.PagesList.EnableModeration)
{
publishFile.Approve("Initial");
}
}
/// <summary>
/// Find a page layout in the layoutcollection based on the templatename
/// </summary>
/// <param name="pubSiteCollection">The toplevel publishing site</param>
/// <param name="templateName">Name of the pagelayout</param>
/// <returns>Pagelayout if excists or null</returns>
private PageLayout FindPageLayout(PublishingSite pubSiteCollection, string templateName)
{
PageLayoutCollection plCollection = pubSiteCollection.GetPageLayouts(true);
foreach (Microsoft.SharePoint.Publishing.PageLayout layout in plCollection)
{
// String Comparison based on the Page Layout Name
if (layout.Name.Equals(templateName, StringComparison.InvariantCultureIgnoreCase))
{
return layout;
}
}
return null;
}
[/code]
Event Receiver DLL in GAC
I noticed that it is not possible to place a dll in the BIN
directory of your Virtual Directory if you're registering event handlers in this
DLL. Event Handlers (also e.g. SharePoint Jobs) have to be placed in the GAC.
Generate an autonumber by using an eventhandler
You can generate an autonumber by using an eventhandler.
public class AutoNumberEventHandler : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
SPListItem item = properties.ListItem;
item["Item Number"] = DateTime.Now.ToString("yyyy") + "-" + item["ID"];
item.Update();
}
}
[/code]
MSDN Event Handlers
Using a console application to install an eventhandler
Of course a Feature is the smartest way to register an eventhandler. From the
other side it could be usefull to use a console application to intall an
eventhandler. The eventhandler dll has to be placed into the GAC
(Windows\Assembly). You can use the following code to register your
eventhandler.
namespace EventHandlerInstaller
{
class Program
{
static void Main(string[] args)
{
try
{
SPSite siteCollection = new SPSite("http://myurl/sites/mysite");
SPWeb site = siteCollection.AllWebs[0];
SPList myList;
foreach (SPList l in site.Lists)
{
Console.WriteLine(l.Title);
if (l.Title == "My List")
{
myList = l;
}
}
string assemblyName = "MyHandlers, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=6d2f9de54c53a11d";
string className = "MyHandlers.MyEventHandler";
myList.EventReceivers.Add
(SPEventReceiverType.ItemAdded, assemblyName, className);
SPEventReceiverDefinitionCollection registeredEvents =
myList.EventReceivers;
foreach (SPEventReceiverDefinition def1 in registeredEvents)
{
Console.WriteLine("Succesfully added eventhandler:" +
def1.Type.ToString());
}
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
[/code]