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.
1: public 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



13 comments:
When I use a page using gl-createpublishingpage it creates a page as a draft version. Is there any way to create the page and publish it too suing a command?
You could use the gl-publishitems command to publish the page after creating.
Hi Gary
when I am creating a page with page layout, facing a problem and error message is "No Constructor parameter "
When I am creating a page, commands throws the error "No Paremeterless constructor is defined for this object".
Kindly let me know how can I remove this error.
Thanks in advance.
Regards
Rao
Could you provide the exact syntax you are using?
I am running following command:
STSADM -o gl-createpublishingpage -url http://intranet -name myDefault.aspx -title "Default Page1" -layout CustomerFrontPageLayout.aspx
Please let me know if I am doing anything wrong.
Regards
Rao
I'm curious what version of MOSS you are using. The error suggest that you have an assembly that doesn't have a constructor that takes no parameters which would only be possible if you had a different version of MOSS than I do. Do you at least have SP1 installed?
I am trying to create a page and also I am filling BIG html content for two publishing fields:
stsadm -o gl-createpublishingpage -url "http://www.test.com" -name Home -title Home.aspx -layout Welcome.aspx -fielddata "PageHeaderContent=;PublishingPageContent="
I am not able to paste html to show in above command because this blog doesn't allow to write html in query....
Sometimes it works and sometimes it throws following error:
The file "http://www.test.com/Pages/Home.aspx" is not checked out. You m
ust first check out this document before making changes.
Any idea if this issue is due to bug HTML blobs I am pushing in content fields? Is there any other way to fill html in these two fields if I just create a blank page via this command?
Thanks,
I'd recommend using PowerShell to set the fields if you have a lot of complex/large data. My intention with the fields parameters was to set simple metadata, not actual page content.
Hi Gary. With a few amendments, could a command like this create Basic Pages within a document library?
Short answer, yes.
Cheers for this Gary
Really helpful - cheers Gary!
Post a Comment