I’m working on a project which is going to require the creation of about a hundred publishing pages which were going to have to be created by hand. I really didn’t want to have to sit there going through the UI to create all those pages so I threw together a new STSADM command that I could use via a script to create the pages: gl-createpublishingpage
.
Creating publishing pages via code is pretty simple – you just use the PublishingWeb
object’s GetPublishingPages()
method to return back the collection of pages and then call its Add
method passing in the page name and layout. From there it’s just a matter of setting the field properties.
1public static void CreatePage(string url, string pageName, string title, string layoutName, Dictionary<string, string> fieldDataCollection)
2{
3 using (SPSite site = new SPSite(url))
4 using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
5 {
6 if (!PublishingWeb.IsPublishingWeb(web))
7 throw new ArgumentException("The specified web is not a publishing web.");
8
9 PublishingWeb pubweb = PublishingWeb.GetPublishingWeb(web);
10 PageLayout layout = null;
11 string availableLayouts = string.Empty;
12 foreach (PageLayout lo in pubweb.GetAvailablePageLayouts())
13 {
14 availableLayouts += "\t" + lo.Name + "\r\n";
15 if (lo.Name.ToLowerInvariant() == layoutName.ToLowerInvariant())
16 {
17 layout = lo;
18 break;
19 }
20 }
21 if (layout == null)
22 throw new ArgumentException("The layout specified could not be found. Available layouts are:\r\n" + availableLayouts);
23
24 if (!pageName.ToLowerInvariant().EndsWith(".aspx"))
25 pageName += ".aspx";
26
27 PublishingPage page = pubweb.GetPublishingPages().Add(pageName, layout);
28 page.Title = title;
29 SPListItem item = page.ListItem;
30
31 foreach (string fieldName in fieldDataCollection.Keys)
32 {
33 string fieldData = fieldDataCollection[fieldName];
34
35 try
36 {
37 SPField field = item.Fields.GetFieldByInternalName(fieldName);
38
39 if (field.ReadOnlyField)
40 {
41 Console.WriteLine("Field '{0}' is read only and will not be updated.", field.InternalName);
42 continue;
43 }
44
45 if (field.Type == SPFieldType.Computed)
46 {
47 Console.WriteLine("Field '{0}' is a computed column and will not be updated.", field.InternalName);
48 continue;
49 }
50
51 if (field.Type == SPFieldType.URL)
52 item[field.Id] = new SPFieldUrlValue(fieldData);
53 else if (field.Type == SPFieldType.User)
54 AddListItem.SetUserField(web, item, field, fieldData);
55 else
56 item[field.Id] = fieldData;
57 }
58 catch (ArgumentException)
59 {
60 Console.WriteLine("WARNING: Could not set field {0} for item {1}.", fieldName, item.ID);
61 }
62 }
63
64 page.Update();
65 }
66}
The help for the command is shown below:
C:\>stsadm -help gl-createpublishingpage
stsadm -o gl-createpublishingpage
Creates a new publishing page.
Parameters:
-url <url to the publishing web within which to create the page>
-name <the filename of the page to create (do not include the extension)>
-title <the page title>
-layout <the filename of the page layout to use>
[-fielddata <semi-colon separated list of key value pairs: "Field1=Val1;Field2=Val2"> (use ';;' to escape semi-colons in data values)]
The following table summarizes the command and its various parameters:
Command Name | Availability | Build Date |
---|---|---|
gl-createpublishingpage | MOSS 2007 | Released: 9/4/2008 |
Parameter Name | Short Form | Required | Description | Example Usage |
---|---|---|---|---|
url | Yes | The URL to the publishing web within which to create the page. | -url http://portal | |
name | n | Yes | The filename of the page to create. It is not necessary to include the extension. Do not use special characters. | -name NewPage , -n NewPage |
title | t | Yes | The title of the page. Wrap within quotes if includes spaces. | -title "New Page" , -t "New Page" |
layout | l | Yes | The filename of the page layout to use. | -layout DefaultLayout.aspx , -l DefaultLayout.aspx |
fielddata | fd | No | Key/Value pairs of metadata to apply to the new page. Use the internal field name and separate multiple pairs with a semi-colon. Use the following format when setting data: Field1=Val1;Field2=Val2 . | -fielddata "PublishingContactEmail=user@domain.com;PublishingContactName=First Last" , -fd "PublishingContactEmail=user@domain.com;PublishingContactName=First Last" |
The following is an example of how to add a new publishing page:
stsadm -o gl-createpublishingpage -url http://portal -name NewPage -title "New Page" -layout DefaultLayout.aspx