Wednesday, March 21, 2012

Programmatically create Sharepoint Pages

Props to Laura: http://www.sharepoint911.com/blogs/laura/Lists/Posts/Post.aspx?List=daba3a3b%2Dc338%2D41d8%2Dbf52%2Dcd897d000cf3&ID=67&Web=dbb90e85%2Db54c%2D49f4%2D8e97%2D6d8258116ca0

Open your site in SharePoint Designer, and click File –> New
–> ASPX
Click the Format menu, choose Master Page,
and click Attach Master Page
Click OK for the Default Master Page.
Click the PlaceHolderMain section in the middle of the page, click the
little chevron (the little gray button with an arrow on it), and click
Create Custom Content.
Put your cursor inside of the PlaceHolderMain section. On the
Insert Menu, choose SharePoint Controls, and
click Web Part Zone

View Source, copy/paste into the Resource.


string siteUrl = "http://localhost";
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb("/"))
{
SPDocumentLibrary lib = (SPDocumentLibrary)web.Lists.TryGetList("Site Pages");
string filename = string.Format("test{0}.aspx", DateTime.Now.ToString("yyyymmddhhmmss"));
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] page = encoding.GetBytes(Resource1.ASPXTemplate);
SPFile file = lib.RootFolder.Files.Add(filename, page, true);
}
}

No comments:

Post a Comment