Creating pages using code
Every now and then, when I prepare my self to a lecture, or when I need to create a demo, I need to produce some “content” on my MOSS site.
Since it is quite a tedious task to produce 10-15 pages, I wrote a small code snippet that will do this task for me.
The basics – creating pages with code
Here is the basic C# code:
- using (SPSite site = new SPSite("http://joeysh-srv-pt:100"))
- {
- using (SPWeb web = site.OpenWeb())
- {
- //Get the publishing site, and publishing web
- PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
-
- //Get the content type for the page
- SPContentTypeId ctID = new SPContentTypeId("0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900B1281BE7EC1A304F977F3595ED435565");
-
- //Now let's get the page layout for that content type
- //Note: assuming only one page layout for that CT
- PageLayout layout = pubWeb.GetAvailablePageLayouts(ctID)[0];
-
- //Create the page
- //Note: assuming page doesn't exist yet
- PublishingPage newPage = pubWeb.GetPublishingPages().Add("newly_created_page.aspx", layout);
-
- //Add the title and description. Those are special fields which doesn't exists in the SPListItem for that page.
- newPage.Title = "Page created with code";
- newPage.Description = "This page was created solely with code";
-
- //Now set some fields' values
- newPage.ListItem["DetailedDescription"] = "This is a very good product description";
- newPage.ListItem["Price"] = "50.99";
- newPage.ListItem["IsOnSale"] = "Yes";
-
- //Update the item, check-in and publish
- newPage.ListItem.Update();
- newPage.ListItem.File.CheckIn("Created automatically");
- newPage.ListItem.File.Publish("Created automatically");
- }
- \
I think the code is really straight forward. After getting an instance of the desired site collection, I’m getting a publishing web object (line 6). This is done to get access to some of the publishing method, which the basic SPWeb doesn’t expose.
In line 20 – 31, I’m filling in some content in the page’s fields. Note that the “title” and “description” fields are getting different attention. The reason for that, is the they both comes from the base object, and not from the content type’s list item.
The last important part is to update the list item, and check in the new file. The update part is simple – a page is a list item, and hence it needs to be updated, just like any other list item.
The check-in and the publish, is unique to pages, and the reason for that is versioning. Unlike regular list items, pages can save versioning information, therefore you should check them out for editing and check them back in when you done. When you create a new page, it is automatically checked out on you.
I would suggest you leave the remark there, and/or add something more descriptive, just so you can recognize you pages later.
Creating multiply pages with code
This is actually a very small extension to the code above.
All I add is a function that will fetch some content from an Excel file, or XML or any other source, it might even be just another list inside your SharePoint. The point is that those data sources, are easier to edit and you can easily just drop as many rows as you like.
So, my code differs very little, first, I get a list of items from my data source:
- //Get some data from any data source
- //'SingleRow' is some data structure. It can be anything:
- //XML, DataRow, any class or object, etc.
- List<SingleRow> pagesToCreate = GetPagesContentFromSource();
-
And then I rap the page-creation-code in a loop, so it looks like that:
- //Now loop the data source, and create a page for each item
- foreach (SingleRow currentRow in pagesToCreate)
- {
- //Create the page
- //Note: assuming page doesn't exist yet
- //The page name is creates using the data source's title
- PublishingPage newPage = pubWeb.GetPublishingPages().Add(currentRow.Title + ".aspx", layout);
-
- //Add the title and description. Those are special fields which doesn't exists in the SPListItem for that page.
- newPage.Title = currentRow.Title;
- newPage.Description = currentRow.Description;
-
- //Now set some fields' values
- newPage.ListItem["DetailedDescription"] = currentRow.DetailedDescription;
- newPage.ListItem["Price"] = currentRow.Price;
- newPage.ListItem["IsOnSale"] = currentRow.IsOnSale;
-
- //Update the item, check-in and publish
- newPage.ListItem.Update();
- newPage.ListItem.File.CheckIn("Created automatically, page title: " + currentRow.Title);
- newPage.ListItem.File.Publish("Created automatically, page title: " + currentRow.Title);
- \
Note that I’m only wrapping the lines that do the actual creation. I don’t want to create instances of sites on each iteration, of course. Furthermore, since I’m creating pages of the same type, the Content Type and the Layout doesn’t change as well.
You can, of course, make the code even more sophisticate, and choose different pages’ layouts and different content types.